using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;



public class PlayerInteraction : MonoBehaviour
{
    public float interactRange = 3f;
    private int blueberryCount = 0;
    public List<InventoryItem> inventory = new List<InventoryItem>();

    [SerializeField] InputActionReference InteractAction;

    void Start()
    {
        // initialize the QuestLoader
        /*
        QuestLoader questLoader = FindFirstObjectByType<QuestLoader>();
        if (questLoader == null)
        {
            Debug.LogError("QuestLoader not found in the scene.");
            return;
        }
        quest = questLoader.allQuests[questIndex];
        Debug.Log("Loaded quest: " + quest.name);
*/

        InteractAction.action.Enable();
        InteractAction.action.performed += OnInteract;
    }

    void OnInteract(InputAction.CallbackContext context)
    {
        if (context.performed)
        {
            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, interactRange))
            {
                if (hit.collider.CompareTag("Interactable"))
                {
                    Debug.Log("Interacted with: " + hit.collider.name);
                    // You can add more actions here
                    hit.collider.GetComponent<QuestGiver>().Talk();
                    // 
                }
            }
        }
    }

    void Update()
    {

    }

    public void CollectBlueberry(int newBlueberries)
    {
        blueberryCount += newBlueberries;
        Debug.Log("Collected " + blueberryCount + " blueberries. Total: " + blueberryCount);
    }

    public void AddToInventory(InventoryItem reward)
    {
        inventory.Add(reward);
        Debug.Log("Added " + reward.name + " to inventory. Total items: " + inventory.Count);
    }

    public void RemoveFromInventory(InventoryItem reward)
    {
        Debug.Log("Attempting to remove " + reward.name + " from inventory.");
        if (inventory.Contains(reward))
        {
            inventory.Remove(reward);
            Debug.Log("Removed " + reward.name + " from inventory. Total items: " + inventory.Count);
        }
        else
        {
            Debug.LogWarning("Item not found in inventory: " + reward.name);
        }
    }

    public int GetItemQuantity(string itemName)
    {
        InventoryItem item = inventory.Find(i => i.itemName == itemName);
        return item != null ? item.quantity : 0;
    }
}
