using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;
using UnityEngine.ProBuilder.Shapes;
using UnityEngine.XR;
using Valve.VR.InteractionSystem;

public class Catalog : MonoBehaviour
{
    public static Catalog Instance;
    private Player player = null;
    public int angle = 180;
    public float distance = .5f;
    public float objectScale = .1f;

    private GameObject spherePrefab;

    private GameObject currentSphere;
    public List<GameObject> Furnitures;
    private int maxShownObjects;
    // Start is called before the first frame update
    void Start()
    {
        Instance = this;
        maxShownObjects = Furnitures.Count;
        spherePrefab = transform.Find("Sphere").gameObject;
        player = Player.instance;
        if (player == null)
        {
            Debug.LogError("<b>[SteamVR Interaction]</b> Teleport: No Player instance found in map.", this);
            Destroy(this.gameObject);
            return;
        }
        ShowCatalog();
    }

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

    private void ShowCatalog()
    {
        gameObject.transform.SetParent(player.hands[0].transform);
        gameObject.transform.localPosition = Vector3.zero;
        float radians = angle * Mathf.Deg2Rad;
        float angleIncrement = radians / (maxShownObjects - 1);

        for (int i = 0; i < maxShownObjects; i++)
        {
            var j = i * angleIncrement - radians / 2;
            var sphere = Instantiate(spherePrefab);
            sphere.transform.parent = transform;
            sphere.transform.localScale = Vector3.one * objectScale;
            sphere.transform.localPosition = new Vector3(Mathf.Sin(j), 0, Mathf.Cos(j)) * distance;
            sphere.SetActive(true);
            sphere.GetComponent<CatalogElement>().SetFurniture(Furnitures[i], objectScale);
        }
        StartCoroutine(CatalogCoroutine());
    }

    private IEnumerator CatalogCoroutine()
    {
        var hand = player.hands;
        while (true)
        {

            transform.rotation = Quaternion.LookRotation(transform.position - player.hmdTransform.position);
            transform.rotation = new Quaternion(0, transform.rotation.y, 0, transform.rotation.w);
            yield return null;
        }
    }

    public void GrabObject(GameObject furniture, Valve.VR.InteractionSystem.Hand hand)
    {
        var sphere = Instantiate(spherePrefab);
        sphere.transform.localScale = Vector3.one * objectScale;
        sphere.SetActive(true);
        sphere.GetComponent<CatalogElement>().SetFurniture(furniture, objectScale);
        hand.AttachObject(sphere, GrabTypes.Grip);
        currentSphere = sphere;
    }

    public void ReleaseObject(GameObject furniture, Valve.VR.InteractionSystem.Hand hand)
    {
        hand.DetachObject(currentSphere);

        currentSphere = null;

    }

    private void OnDrawGizmosSelected()
    {
        maxShownObjects = Furnitures.Count;
        float radians = angle * Mathf.Deg2Rad;
        float angleIncrement = radians / (maxShownObjects - 1);
        for (float i = 0 / 2; i < maxShownObjects; i++)
        {
            var j = i * angleIncrement - radians / 2;
            Gizmos.DrawWireSphere(transform.position + new Vector3(Mathf.Sin(j), 0, Mathf.Cos(j)) * distance, objectScale / 2);
        }
    }

    public void OnGrabSphere(CatalogElement sphere, Valve.VR.InteractionSystem.Hand hand)
    {
        MovableFurniture grab = hand.GetComponent<FurnitureMover>().Grabbing;
        if (grab != null)
        {
            return;
        }
        GameObject furniture = Instantiate(sphere.largeFurnitureModel);
        MovableFurniture script = furniture.GetComponent<MovableFurniture>() ;
        if (!furniture.GetComponent<MovableFurniture>())
        {
            script = furniture.AddComponent<MovableFurniture>();
        }
        script.CalculateBounds();
        hand.GetComponent<FurnitureMover>().Grabbing = script;
    }

    public void OnReleaseSphere(CatalogElement sphere, Valve.VR.InteractionSystem.Hand hand)
    {
        hand.GetComponent<FurnitureMover>().Grabbing = null;
    }


}
