using Cinemachine;
using UnityEngine;

public class CameraSlotClickDetect : MonoBehaviour
{
    [SerializeField] private TowerCam towerCam;

    public Camera mainCamera;
    public LayerMask layerMask;
    public LayerMask selectLayer;

    private SlotManager PrevHoveredSlot;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ShootClickRay();
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            GameManager.Instance.SelectedTower = null;
            if (towerCam != null)
                towerCam.ChangeToTarget(null);
            else
                Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
        }

        ShootHoverRay();
    }

    void ShootClickRay()
    {
        Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        RaycastHit selectHit;
        if (!GameManager.Instance.IsBuildMode && Physics.Raycast(ray, out selectHit, Mathf.Infinity, selectLayer))
        {
            var tower = selectHit.collider.gameObject.GetComponentInChildren<Tower>();
            if (tower != null)
            {
                GameManager.Instance.SelectedTower = tower;
                
                if (towerCam != null)
                    towerCam.ChangeToTarget(tower.gameObject);
                else
                    Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
            }
        }
        else if (GameManager.Instance.IsBuildMode && Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
        {
            var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
            slotInfo.OnClick();
            GameManager.Instance.SelectedTower = null;

            if (towerCam != null)
                towerCam.ChangeToTarget(null);
            else
                Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
        }
        /* else
        {
            GameManager.Instance.SelectedTower = null;
            if (towerCam != null)
                towerCam.ChangeToTarget(null);
            else
                Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
        } */
    }

    void ShootHoverRay()
    {
        Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (GameManager.Instance.IsBuildMode)
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
            {
                var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();

                if (PrevHoveredSlot != null && PrevHoveredSlot != slotInfo)
                {
                    PrevHoveredSlot.OnUnHovered();
                }

                slotInfo.OnHovered();
                PrevHoveredSlot = slotInfo;
            }
            else
            {
                if (PrevHoveredSlot != null)
                    PrevHoveredSlot.OnUnHovered();
                PrevHoveredSlot = null;
            }
    }
}