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

[System.Serializable]
public class EquipmentManager : BaseWarehouseController
{
    public static int MAX_ITEMS = 0;

    [SerializeField] public EquippableItem _helmet;
    [SerializeField] public EquippableItem _chest;
    [SerializeField] public EquippableItem _boots;
    [SerializeField] public EquippableItem _weapon;
    [SerializeField] public EquippableItem _potion_one;
    [SerializeField] public EquippableItem _potion_two;
    [SerializeField] public EquippableItem _potion_three;    
    [SerializeField] public EquippableItem _potion_four;
    [SerializeField] public EquippableItem _potion_five;

    public static EquipmentManager Instance;

    Dictionary<int, Item> equipment;

    private void Awake()
    {
        if(Instance == null)
        {
            Instance = this;

            equipment = new Dictionary<int, Item>() {
                { 0, _helmet },
                { 1, _chest },
                { 2, _boots },
                { 3, _weapon },
                { 4, _potion_one },
                { 5, _potion_two },
                { 6, _potion_three },
                { 7, _potion_four },
                { 8, _potion_five }
            };
        }else if (Instance != this)
        {
            Destroy(gameObject);
        }     
    }

    protected override void SetupPanel()
    {
        if(this.dynamicPanel)
        {
            this.dynamicPanel.GetComponent<EquipmentPanelController>().Setup(gameObject, equipment);
        }
    }

    // Handle in  Manager items local list
    public override void SetItemOnPosition(int _keyPosition, Item _item)
    {
        // in qeuippment panel its case where we drop drop on empty slot  

        if(_item == null) // if we move eg. form chest to eq Drop work on Eq Panel where dropitemSlot.Item is Null
            return;

        _keyPosition = MapItemTypeToSlotNumber((EquippableItem)_item);
        this.equipment[_keyPosition] = _item;
        
        this.ApplyEquipmentObject(true, (EquippableItem)_item);

        base.SetItemOnPosition(_keyPosition, _item);
    }

    // Remove from  Manager items local list
    public override void RemoveItemFromPosition(int _keyPosition)
    {
        this.equipment.Remove(_keyPosition);

        this.ApplyEquipmentObject(_keyPosition);

        base.RemoveItemFromPosition(_keyPosition);
    }

    /*
    * Function decide in which slot item should be handled
    * Mach item to dictionary key based on items equippment type
    * return: dictionary key
    */
    private int MapItemTypeToSlotNumber(EquippableItem _item)
    {
        int key=-1;

        if(_item == null)
        {
            Debug.LogError("Ten item w ogóle nie powinien być rozpatrywany jako dodany do ekwipunku - nigdy nie był typu EquippableItem");
        }

        switch(_item.EquipmentType)
        {
            case EquipmentType.Helmet:
            {
                key = 0;
                break;
            }
            case EquipmentType.Chest:
            {
                key = 1;
                break;
            }
            case EquipmentType.Boots:
            {
                key = 2;
                break;
            }
            case EquipmentType.Weapon:
            {
                key = 3;
                break;
            }
            case EquipmentType.Potion:
            {
                key = 4;
                break;
            }
            case EquipmentType.Bracelet:
            {
                key = 5;   
                break;
            }
            case EquipmentType.Necklet:
            {
                key = 6;     
                break;
            }
            case EquipmentType.Ring:
            {
                if(!equipment.ContainsKey(7) || equipment[7] == null)
                {
                    key = 7;
                }
                if(!equipment.ContainsKey(8) || equipment[8] == null)
                {
                    key = 8;
                }
                    
                break;
            }
            default:
            {
                Debug.Log("Can't mach number to item type");
                break;
            }
        }

        return key;
    }

    /*
    * applu EquippableItem to be able to see actual equipment status - mapped with 'qeuipment' Dictioanry which is not showed in amnager panel :/
    */
    public void ApplyEquipmentObject(bool put, EquippableItem _item = null)
    {
        switch(_item.EquipmentType)
        {
            case EquipmentType.Helmet:
            {
                _helmet = put ? _item : null;
                break;
            }
            case EquipmentType.Chest:
            {
                _chest = put ? _item : null;
                break;
            }
            case EquipmentType.Boots:
            {
                _boots = put ? _item : null;
                break;
            }
            case EquipmentType.Weapon:
            {
                _weapon = put ? _item : null;
                break;
            }
            case EquipmentType.Potion:
            {
                _potion_one = put ? _item : null;
                break;
            }
            case EquipmentType.Bracelet:
            {
                _potion_two  = put ? _item : null;
                break;
            }
            case EquipmentType.Necklet:
            {
                _potion_three = put ? _item : null;
                break;
            }
            case EquipmentType.Ring:
            {
                if(!equipment.ContainsKey(7) || equipment[7] == null)
                {
                    _potion_four = put ? _item : null;
                }
                if(!equipment.ContainsKey(8) || equipment[8] == null)
                {
                    _potion_five  = put ? _item : null;
                }
                    
                break;
            }
            default:
            {
                Debug.Log("Can't mach number to item type");
                break;
            }
        }
    } 

    public void ApplyEquipmentObject(int _keyPosition)
    {
        switch(_keyPosition)
        {
            case 0:
            {
                _helmet = null;
                break;
            }
            case 1:
            {
                _chest = null;
                break;
            }
            case 2:
            {
                _boots = null;
                break;
            }
            case 3:
            {
                _weapon = null;
                break;
            }
            case 4:
            {
                _potion_one = null;
                break;
            }
            case 5:
            {
                _potion_two  = null;
                break;
            }
            case 6:
            {
                _potion_three = null;
                break;
            }
            case 7:
            {
                _potion_four = null;
                break;
            }
            case 9:
            {
                _potion_five  = null;
                break;
            }
        }
    } 
}