using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;

public class CharacterControl : MonoBehaviour
{
    [SerializeField]
    Transform playerCamera;

    [SerializeField]
    float seeDistance;
    Interactable curretInteractable;

    public GunPickable CurrentGun;
    public GameObject holdingGun;
    public RaycastHit hitInfo;
    public static bool handsFull;

    [SerializeField]
    private GameObject hand;
    public TextMeshProUGUI tooltips;
    private Pickable newPickable;
    public bool active;

    void Update()
    {
        if (active)
        {
            bool Raycast = Physics.Raycast(
                playerCamera.position,
                playerCamera.TransformDirection(Vector3.forward),
                out hitInfo,
                seeDistance
            );
            Debug.DrawRay(
                playerCamera.position,
                playerCamera.TransformDirection(Vector3.forward) * seeDistance,
                Color.green
            );
            if (Raycast)
            {
                if (tooltips == null)
                {
                    tooltips = GameObject.Find("toolTips").GetComponent<TextMeshProUGUI>();
                }
                Debug.Log(hitInfo.transform.name);
                if (hitInfo.transform.TryGetComponent<Interactable>(out curretInteractable))
                {
                    if (curretInteractable.tag == "door")
                    {
                        if (curretInteractable.name=="doorSlide"&&!GameManager.instance.petrolCollected
                                && !GameManager.instance.gotGenerator)
                        {
                                tooltips.text = "get the Generator and Petrol to power the ship";
                        }
                        else
                        {
                            tooltips.text = "Press [e] to open door";
                        }
                    }
                    else if (curretInteractable.name == "petrol")
                    {
                        if (!GameManager.instance.petrolCollected)
                        {
                            tooltips.text = "Press [e] to pump petrol";
                        }
                        else
                        {
                            tooltips.text = "petrol Collected";
                        }
                    }
                    else if (curretInteractable.TryGetComponent<Pickable>(out newPickable))
                    {
                        if (handsFull)
                        {
                            tooltips.text = "hands are full drop item using [g]";
                        }
                        else
                        {
                            tooltips.text = "Press [e] to Pick up";
                        }
                    }
                    else if (curretInteractable.tag == "collectable")
                    {
                        tooltips.text = "press [e] to collect";
                    }
                }
                else
                {
                    curretInteractable = null;
                    CurrentGun = null;
                    tooltips.text = "";
                }
            }
            else
            {
                curretInteractable = null;
                CurrentGun = null;

                tooltips.text = "";
            }
        }
    }

    void OnInteract()
    {
        if (curretInteractable != null)
        {
            if (!handsFull && curretInteractable.TryGetComponent<GunPickable>(out CurrentGun))
            {
                holdingGun = CurrentGun.gameObject;
                handsFull = true;
                curretInteractable.Interact();
            }
            else if (!curretInteractable.TryGetComponent<GunPickable>(out CurrentGun))
            {
                curretInteractable.Interact();
            }
        }
    }

    void OnShoot()
    {
        Debug.Log("Shoot");
        if (holdingGun != null)
        {
            holdingGun.GetComponent<Gun>().Shoot();
        }
    }

    void OnReload()
    {
        if (holdingGun != null)
        {
            holdingGun.GetComponent<Gun>().Reload();
        }
    }

    void OnDrop()
    {
        holdingGun.transform.GetComponent<Pickable>().Drop();
        holdingGun = null;
        handsFull = false;
    }

    void OnPause()
    {
        GameManager.instance.Pause();
    }
}
