using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class Item : MonoBehaviour
{
    // All of this could've been done more efficiently with override functions and inheritance and whatnot
    // but this is a jam game so being lazy about these things just this once is fine.

    public string itemName = "";

    [SerializeField]
    private string interactMessage = "";

    [SerializeField]
    private bool _makesItem = false;

    [SerializeField]
    private bool _canBePickedUp = true;

    [SerializeField]
    private bool m_removesSelf = false;

    [SerializeField]
    private bool m_deletesSelf = false;

    //[SerializeField]
    //private bool m_causesScreenFlash = false;

    [SerializeField]
    private AudioClip m_pickUpSound;

    [SerializeField]
    private AudioClip m_interactSound;

    [SerializeField]
    private bool m_reactsToItem = false;

    [SerializeField]
    private string m_itemNeededForReaction = "";

    [SerializeField]
    private string reactionMessage = "";

    [SerializeField]
    private string alternateMessage = "";

    public Sprite itemSprite;

    [SerializeField]
    private bool m_disablesMovement;
    [SerializeField]
    private bool m_enablesMovement;


    [SerializeField]
    private bool _changesLocation = false;

    [SerializeField]
    private bool _unlocksOnReaction = false;

    [SerializeField]
    private Transform locationToChangeTo;

    [SerializeField]
    private bool m_isBossLimb = false;

    private AudioSource _sfxManager;

    public void OnMouseEnter()
    {
        GameObject itemManager = GameObject.FindGameObjectWithTag("ItemManager");

        Cursor.SetCursor(itemManager.GetComponent<ItemManager>().m_selectedCursor, Vector2.zero, CursorMode.Auto);

        itemManager.GetComponent<ItemManager>()._hoveringOverItem = true;
        itemManager.GetComponent<ItemManager>()._itemHoveredOver = gameObject;
    }

    public void OnMouseExit()
    {
        GameObject itemManager = GameObject.FindGameObjectWithTag("ItemManager");

        Cursor.SetCursor(itemManager.GetComponent<ItemManager>().m_normalCursor, Vector2.zero, CursorMode.Auto);

        itemManager.GetComponent<ItemManager>()._hoveringOverItem = false;
        itemManager.GetComponent<ItemManager>()._itemHoveredOver = null;
    }

    void OnMouseDown()
    {
        GameObject itemManager = GameObject.FindGameObjectWithTag("ItemManager");

        itemManager.GetComponent<ItemManager>()._hoveringOverItem = false;
        itemManager.GetComponent<ItemManager>()._itemHoveredOver = null;


        if (m_reactsToItem)
        {
            if (itemManager.GetComponent<ItemManager>()._currentlyHolding == m_itemNeededForReaction)
            {
                if (m_interactSound != null)
                {
                    _sfxManager = GameObject.FindGameObjectWithTag("SFXManager").GetComponent<AudioSource>();

                    _sfxManager.clip = m_interactSound;

                    _sfxManager.Play();
                }

                if (_canBePickedUp)
                {
                    Cursor.SetCursor(itemManager.GetComponent<ItemManager>().m_normalCursor, Vector2.zero, CursorMode.Auto);
                    gameObject.SetActive(false);
                }

                if (m_removesSelf)
                {
                    transform.GetChild(0).gameObject.SetActive(false);
                }

                if (_unlocksOnReaction)
                {
                    _changesLocation = true;
                    interactMessage = alternateMessage;
                }

                if (_makesItem)
                {
                    itemManager.GetComponent<ItemManager>().RemoveItem(m_itemNeededForReaction);
                    interactMessage = alternateMessage;
                    itemManager.GetComponent<ItemManager>().PickUpItem(gameObject);
                }

                itemManager.GetComponent<ItemManager>().RemoveItem(m_itemNeededForReaction);

                GameObject hudManager = GameObject.FindGameObjectWithTag("HudManager");
                hudManager.GetComponent<HUDManager>().DisplayText(reactionMessage);


                if (m_isBossLimb)
                {
                    GameObject bossLimbsCounter = GameObject.FindGameObjectWithTag("BossLimbsCounter");
                    bossLimbsCounter.GetComponent<BossLimbsCounter>().BossLimbCut();

                    gameObject.SetActive(false);
                }

                if (m_deletesSelf)
                {
                    gameObject.SetActive(false);
                }

                return;
            }

        }

        if (m_pickUpSound != null)
        {
            _sfxManager = GameObject.FindGameObjectWithTag("SFXManager").GetComponent<AudioSource>();

            _sfxManager.clip = m_pickUpSound;
            _sfxManager.Play();
        }


        if (_canBePickedUp)
        {
            Debug.Log("Picked up " + itemName);

            Cursor.SetCursor(itemManager.GetComponent<ItemManager>().m_normalCursor, Vector2.zero, CursorMode.Auto);
            if (itemManager.GetComponent<ItemManager>().inventoryFull) { return; }
            itemManager.GetComponent<ItemManager>().PickUpItem(gameObject);
            gameObject.SetActive(false);
        }

        if (interactMessage != "") 
        {
            GameObject hudManager = GameObject.FindGameObjectWithTag("HudManager");
            hudManager.GetComponent<HUDManager>().DisplayText(interactMessage);
        }

        if (m_disablesMovement && _changesLocation)
        {
            GameObject locationManager = GameObject.FindGameObjectWithTag("LocationManager");
            locationManager.GetComponent<LocationManager>().canMove = false;
        }

        if (m_enablesMovement && _changesLocation)
        {
            GameObject locationManager = GameObject.FindGameObjectWithTag("LocationManager");
            locationManager.GetComponent<LocationManager>().canMove = true;
        }

        if (_changesLocation)
        {
            Cursor.SetCursor(itemManager.GetComponent<ItemManager>().m_normalCursor, Vector2.zero, CursorMode.Auto);
            GameObject locationManager = GameObject.FindGameObjectWithTag("LocationManager");
            locationManager.GetComponent<LocationManager>().ChangeLocation(locationToChangeTo);
        }
    }
}
