using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.ProBuilder.Shapes;
using Valve.VR.InteractionSystem;

public class CatalogElement : Throwable
{
    public GameObject largeFurnitureModel;
    private GameObject smallFurnitureModel;
    private Vector3 returnPosition;
    // Start is called before the first frame update
    void Start()
    {
        returnPosition = transform.localPosition;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    protected override void OnAttachedToHand(Hand hand)
    {
        //Debug.Log("<b>[SteamVR Interaction]</b> Pickup: " + hand.GetGrabStarting().ToString());

        hadInterpolation = this.rigidbody.interpolation;

        attached = true;

        onPickUp.Invoke();

        hand.HoverLock(null);

        rigidbody.interpolation = RigidbodyInterpolation.None;

        attachTime = Time.time;
        attachPosition = transform.position;
        attachRotation = transform.rotation;

        Catalog.Instance.OnGrabSphere(this, hand);
        //hand.GetComponent<FurnitureMover>().Grabbing = largeFurnitureModel.GetComponent<MovableFurniture>();
}


    //-------------------------------------------------
    protected override void OnDetachedFromHand(Hand hand)
    {
        attached = false;

        onDetachFromHand.Invoke();

        hand.HoverUnlock(null);

        rigidbody.interpolation = hadInterpolation;

        Catalog.Instance.OnReleaseSphere(this, hand);
        Destroy(gameObject);
    }
 

    public void SetFurniture(GameObject furniture, float objectScale)
    {
        largeFurnitureModel = furniture;
        smallFurnitureModel = Instantiate(furniture);
        var throwables = smallFurnitureModel.GetComponentsInChildren<Throwable>();
        foreach (Throwable throwable in throwables)
        {
            Destroy(throwable);
        };
        var rigidbodies = smallFurnitureModel.GetComponentsInChildren<Rigidbody>();
        foreach (Rigidbody rigidbody in rigidbodies)
        {
            Destroy(rigidbody);
        };
        smallFurnitureModel.transform.parent = transform;
        Destroy(smallFurnitureModel.GetComponent<MovableFurniture>());
        Furniture objectBounds = smallFurnitureModel.AddComponent<Furniture>();
        objectBounds.CalculateBounds();
        var colldiers = smallFurnitureModel.GetComponentsInChildren<Collider>();
        foreach (Collider collider in colldiers)
        {
            Destroy(collider);
        };
        float rescale = (objectScale / 1.4f) / objectBounds.combinedBounds.size.magnitude;
        smallFurnitureModel.transform.localScale *= rescale;
        smallFurnitureModel.transform.localPosition = Vector3.down * 0.2f; 

        StartCoroutine(KeepFurnitureAligned());
    }
    private IEnumerator ReturnSphere()
    {
        float time = 0;
        Vector3 startPosition = transform.localPosition;
        while (time <= 1)
        {
            transform.localPosition = Vector3.Lerp(startPosition, returnPosition, time);
            time += Time.deltaTime*4;
            yield return null;
        }
        yield break;
    }

    private IEnumerator KeepFurnitureAligned() {
        while (true)
        {
            smallFurnitureModel.transform.eulerAngles = new Vector3(0, smallFurnitureModel.transform.eulerAngles.y-(20*Time.deltaTime), 0);
            yield return null;
        }
    }
}
