using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VIDE_Data;

public class NPC : MonoBehaviour
{
    public bool wantsItem;
    public GameObject itemToGive;
    public int dialogueHasItem;
    public int dialogueDoesntHaveItem;
    public int dialogueHasSpace;
    public int dialogueNoSpace;
    public int dialogueFriendHasGotItem;
    public int dialogueFriendHasNotGotItem;
    public NPC friendToCheck;
    private Inventory inventory;
    public KeyCode npcDiaglougeInteract;

    [Header("Only use for generic NPC's")] 
    public bool isGenericNpc;
    public SimonsFileCreator.TypeOfFile typeOfFile;
    public VIDE_Assign videAssign;
    
    // Start is called before the first frame update
    void Start()
    {
        videAssign = this.GetComponent<VIDE_Assign>();
        wantsItem = false;
        inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    }

    public bool CheckIfHaveFreeSpace()
    {
        foreach (Slots slot in inventory.playerSlots)
        {
            if (slot.isFull == true)
            {
                Debug.Log("Has Space");
                VD.SetNode(dialogueHasSpace);
                return true;
            }
        }
        Debug.Log("No Space");
        VD.SetNode(dialogueNoSpace);
        return false;
    }

    public void GiveItem()
    {
        inventory.AddItem(itemToGive);
    }

    public bool CheckIfHaveItem(string itemName)
    {
        foreach (Slots slot in inventory.playerSlots)
        {
            Debug.Log(slot.isFull);
            if (slot.isFull == true)
            {
                if (slot.itemPrefab.GetComponent<PickUp>().itemScriptable.itemName == itemName)
                {
                    Debug.Log("Has Item");
                    VD.SetNode(dialogueHasItem);
                    return true;
                }
            }
        }
        Debug.Log("Doesn't have it");
        VD.SetNode(dialogueDoesntHaveItem);
        return false;
    }

    public void TakeItem(string itemName)
    {
        foreach (Slots slot in inventory.playerSlots)
        {
            if (slot.itemPrefab.GetComponent<PickUp>().itemScriptable.itemName == itemName)
            {
                wantsItem = true;
                slot.DestroyItem();
                return;
            }
        }
    }

    public void HasFriendGotItem()
    {
        if (friendToCheck.wantsItem)
        {
            VD.SetNode(dialogueFriendHasGotItem);
        }
        else
        {
            VD.SetNode(dialogueFriendHasNotGotItem);
        }
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            if (Input.GetKeyUp(npcDiaglougeInteract))
            {
                if (isGenericNpc == true)
                {
                    UIManager.Instance.Begin(videAssign, 
                        SimonsFileCreator.instance.ChooseDialogueLine(typeOfFile).ToString());
                }
                else
                {
                    UIManager.Instance.Begin(videAssign);
                }
                
                
                GetComponent<InteractPopUp>().CanvasActive(false);
            }
        }
    }
}
