using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

[ExecuteAlways]
public class InventoryItem : MonoBehaviour
{
    
    public GameObject itemPrefab;

    private int _itemId;

    private GameObject _childPrefab;

    public int Count { get; private set; }

    public TMP_Text itemCounterText;

    private ItemData _itemData;
    

    private void OnEnable()
    {
        if (itemPrefab == null)
        {
            enabled = false;
            //Debug.LogError("Item has no attached prefab");
            return;
        }

        enabled = true;
        
        if (itemCounterText == null) itemCounterText = GetComponentInChildren<TMP_Text>();

        _childPrefab = GetComponentInChildren<ItemData>()?.gameObject;
        //Only attaches the prefab if it doesn't already have one ([ExecuteAlways] causes one to be spawned in editor).
        if (_childPrefab != null) return;

        _childPrefab = Instantiate(itemPrefab, transform);
        _childPrefab.transform.localPosition = new Vector3(0, -0.45f, 0);
        _childPrefab.transform.localScale = new Vector3(2, 2, 2);
        _childPrefab.transform.rotation = quaternion.identity;
        
        //Disable prefab components that arent relevant. 
        var colliderComponent = _childPrefab.GetComponent<Collider>();
        if (colliderComponent != null) colliderComponent.enabled = false;
        
        var rigidbodyComponent = _childPrefab.GetComponent<Rigidbody>();
        if (rigidbodyComponent != null)
        {
            rigidbodyComponent.isKinematic = true;
            rigidbodyComponent.useGravity = false;
        }

        var xrGrabInteractableComponent = _childPrefab.GetComponent<XRGrabInteractable>();
        if (xrGrabInteractableComponent != null) xrGrabInteractableComponent.enabled = false;
        
    }

    private void Start()
    {
        _itemData = _childPrefab.GetComponent<ItemData>();
        
        if (_itemData == null) Debug.LogError("Item prefab has no attached ItemData", _childPrefab);
        
        
        _itemId = _itemData.itemId;
        if (Count == 0) Count = 1;
        UpdateText();
    }

    private void UpdateText()
    {
        itemCounterText.text = _itemData.canStack ? Count.ToString() : "";
    }

    /**
     * Changes how many items are in this itemcontainer.
     * Returns false if attempted to remove more items than exists in the container.
     */
    public bool ChangeCount(int amount)
    {
        if (Count + amount < 0) return false;
        
        Count += amount;
        
        if (Count == 0)
        {
            //Destroys this gameobject if the count is 0 or lower 
            Destroy(gameObject);
            return true;
        }
        UpdateText();
        

        return true;
    }

    public void PopItem()
    {
        GameObject item = Instantiate(_itemData.prefab, transform.position + transform.forward, Quaternion.identity);

        item.transform.localScale = Vector3.one;
        item.transform.name = _itemData.name;

        var colliderComponent = item.GetComponent<Collider>();
        if (colliderComponent != null) colliderComponent.enabled = true;

        var rigidbodyComponent = item.GetComponent<Rigidbody>();
        if (rigidbodyComponent != null)
        {
            rigidbodyComponent.isKinematic = false;
            rigidbodyComponent.useGravity = true;
        }

        var xrGrabInteractableComponent = item.GetComponent<XRGrabInteractable>();
        if (xrGrabInteractableComponent != null) xrGrabInteractableComponent.enabled = true;


        ChangeCount(-1);
    }

    public int GetItemid()
    {
        return _itemId;
    }
    
    

    public override string ToString()
    {
        return $"{base.ToString()}, Count: {Count}, ItemData: {_itemData}";
    }
}