using Photon.Pun;
using System;
using UnityEngine;

namespace Kryz.CharacterStats.Examples
{
    public class EquipmentPanel : MonoBehaviour
    {
        [SerializeField] Transform equipmentSlotsParent;
        [SerializeField] EquipmentSlot[] equipmentSlots;

        [SerializeField] GameEventListener onJoinedRoom;

        public UnityEvent_Item OnItemRightClickedEvent = new UnityEvent_Item();

        EquipmentData equipmentData = new EquipmentData();

        private void Awake()
        {
            onJoinedRoom.Response.AddListener(LoadEquipment);
        }

        private void Start()
        {
            for (int i = 0; i < equipmentSlots.Length; i++)
            {
                equipmentSlots[i].OnRightClickEvent.AddListener((X) => OnItemRightClickedEvent.Invoke(X));
            }
        }

        private void OnValidate()
        {
            if (equipmentSlotsParent != null)
                equipmentSlots = equipmentSlotsParent.GetComponentsInChildren<EquipmentSlot>();
        }

        public bool AddItem(EquippableItem item, out EquippableItem previousItem)
        {
            for (int i = 0; i < equipmentSlots.Length; i++)
            {
                if (equipmentSlots[i].EquipmentType == item.EquipmentType)
                {
                    previousItem = (EquippableItem)equipmentSlots[i].Item;
                    equipmentSlots[i].Item = item;

                    equipmentData.equippedItems[i] = ItemIndexer.Instance.Items.IndexOf(item);
                    PlayerDataHandler.Instance.SaveCharacterEquipmentData(PlayerDataHandler.Instance.currentPlayerName.Value, PhotonNetwork.NickName, equipmentData);
                    return true;
                }
            }
            previousItem = null;
            return false;
        }

        public bool RemoveItem(EquippableItem item)
        {
            for (int i = 0; i < equipmentSlots.Length; i++)
            {
                if (equipmentSlots[i].Item == item)
                {
                    equipmentSlots[i].Item = null;
                    equipmentData.equippedItems[i] = -1;
                    PlayerDataHandler.Instance.SaveCharacterEquipmentData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value, equipmentData);
                    return true;
                }
            }
            return false;
        }

        public EquipmentSlot[] GetCurrentSlots()
        {
            return equipmentSlots;
        }

        public void LoadEquipment()
        {
            equipmentData = PlayerDataHandler.Instance.LoadCharacterEquipmentData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value);

            if (equipmentData != null)
            {
                Debug.Log("Success Loading Equipment");
                for (int i = 0; i < equipmentData.equippedItems.Length; i++)
                {
                    if (equipmentData.equippedItems[i] < 0)
                    {
                        equipmentSlots[i].Item = null;
                    }
                    else
                    {
                        equipmentSlots[i].Item = ItemIndexer.Instance.Items[equipmentData.equippedItems[i]];
                    }
                }
            }
            else
                equipmentData = new EquipmentData();
        }

    }
}
