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

public class Player : BoardEntity
{
    public System.Action DeathAction;

    [SerializeField] Sprite m_NormalSprite;
    [SerializeField] Sprite m_HoldSprite;
    [SerializeField] AudioClip[] m_StepSounds;
    [SerializeField] PlayerDeathAnimator m_DeathPrefab;

    bool m_MovementLock = false;
    private InventoryManager m_Inventory;
    private Equipment m_CurrentHeldItem = Equipment.NONE;
    private SpriteRenderer m_HeldItemRenderer;
    private PlayerState m_State = PlayerState.Alive;
    public enum PlayerState
    {
        Alive,
        Locked,
        Dialogue,
        Dead
    }

    protected override void OnAwake()
    {
        PlayerPrefs.SetInt("PP_DeathCount", 0);
        m_Inventory = GetComponent<InventoryManager>();
        m_HeldItemRenderer = transform.GetChild(0).GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (m_State == PlayerState.Alive)
        {
            Vector2Int moveDir = new Vector2Int(Mathf.RoundToInt(Input.GetAxisRaw("Horizontal")), Mathf.RoundToInt(Input.GetAxisRaw("Vertical")));
            if (moveDir.sqrMagnitude > 0)
            {
                if (!m_MovementLock)
                {
                    if (Move(moveDir))
                    {
                        m_CurrentHeldItem = Equipment.NONE;
                        UpdateSprite();
                        SoundManager.PlayOneShot(m_StepSounds[Random.Range(0, m_StepSounds.Length)]);
                    }
                    else
                    {
                        BoardEntity b = BoardManager.Talk(BoardPosition + moveDir);
                        if (b != null)
                        {
                            print("Talk");
                            DialogueObject d = b.GetComponent<DialogueEntity>().GetDialogue();
                            if (d != null)
                            {
                                m_State = PlayerState.Dialogue;
                                UI_TextWindow.Complete += ResetState;
                                UI_TextWindow.SetTextWindow(d);
                            }
                        }
                        else if (BoardManager.Interact(BoardPosition + moveDir, m_CurrentHeldItem))
                        {
                            m_CurrentHeldItem = Equipment.NONE;
                            UpdateSprite();
                        }
                    }
                    m_MovementLock = true;
                }
            }
            else
            {
                m_MovementLock = false;
            }
            if (Input.GetKeyDown(KeyCode.LeftControl))
            {
                Die();
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                BoardManager.Instance.SpawnNumbers(BoardPosition);
                SoundManager.PlayOneShot(m_Inventory.m_Inventory[0].m_UseClip);
                /*if (!m_Inventory.CurrentEquipment.m_IsLimited || m_Inventory.CurrentEquipment.m_Count > 0)
                {
                    switch (m_Inventory.CurrentEquipment.m_Type)
                    {
                        case Equipment.CrystalBall:
                            BoardManager.Instance.SpawnNumbers(BoardPosition);
                            break;

                        case Equipment.Candle:
                            m_Inventory.UseEquipment();
                            m_CurrentHeldItem = m_Inventory.CurrentEquipment.m_Type;
                            m_HeldItemRenderer.sprite = m_Inventory.CurrentEquipment.m_ItemSprite;
                            break;

                        case Equipment.Gravestone:
                            m_Inventory.UseEquipment();
                            m_CurrentHeldItem = m_Inventory.CurrentEquipment.m_Type;
                            m_HeldItemRenderer.sprite = m_Inventory.CurrentEquipment.m_ItemSprite;
                            break;
                    }
                    SoundManager.PlayOneShot(m_Inventory.CurrentEquipment.m_UseClip);
                }*/
                UpdateSprite();
            }

            if (Input.GetKeyDown(KeyCode.Q))
            {
                m_Inventory.UseEquipment();
                m_CurrentHeldItem = m_Inventory.m_Inventory[1].m_Type;
                m_HeldItemRenderer.sprite = m_Inventory.m_Inventory[1].m_ItemSprite;
                UpdateSprite();
            }
            if (Input.GetKeyDown(KeyCode.E))
            {
                m_Inventory.UseEquipment();
                m_CurrentHeldItem = m_Inventory.m_Inventory[2].m_Type;
                m_HeldItemRenderer.sprite = m_Inventory.m_Inventory[2].m_ItemSprite;
                UpdateSprite();
            }
        }
        if (m_State == PlayerState.Dialogue)
        {
            if (Input.anyKeyDown)
            {
                if (UI_TextWindow.Advance())
                    m_State = PlayerState.Alive;                
            }
        }
    }

    private void ResetState()
    {
        m_State = PlayerState.Alive;
    }

    private void UpdateSprite()
    {
        if(m_CurrentHeldItem == Equipment.NONE)
        {
            m_SpriteRenderer.sprite = m_NormalSprite;
            m_HeldItemRenderer.sprite = null;
        }
        else
        {
            m_SpriteRenderer.sprite = m_HoldSprite;
        }
    }

    private void EquipItem(Equipment equipType)
    {

    }

    public void Spawn(Vector2Int position)
    {
        SetPosition(position);
        m_State = PlayerState.Alive;
        m_MovementLock = false;
        m_SpriteRenderer.enabled = true;
    }

    public void Die()
    {
        PlayerPrefs.SetInt("PP_DeathCount", PlayerPrefs.GetInt("PP_DeathCount", 0) + 1);
        m_State = PlayerState.Dead;
        m_SpriteRenderer.enabled = false;
        (Instantiate<PlayerDeathAnimator>(m_DeathPrefab, transform, false)).AnimationComplete += DeathCleanup;
    }

    private void DeathCleanup(PlayerDeathAnimator pda)
    {
        pda.AnimationComplete = null;
        Destroy(pda.gameObject);
        if (DeathAction != null)
            DeathAction();
    }
}
