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

[Serializable]
public class ChestUIManager : UIBaseManager<Chest>
{
    public new static ChestUIManager Instance { get; private set; }

    public const string ITEM_LOCALIZATION = "UiPanels/";
    public const string PANEL_NAME = "ChestPanel";
    public int SLOTS_NUMBER => 48;

    public string CurrentChestName = null;

    public void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

    public override bool OpenPanel()
    {
        if (CurrentChestName == null || CurrentChestName == "")
        {
            Debug.Log($"You re not in collision with any chest");
               
            return false;
        }

        // Open additiona panels
        if (!EquipmentUIManager.Instance.GetPanelStatus()) EquipmentUIManager.Instance.OpenPanel();

        if (!InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.OpenPanel();

        return base.OpenPanel();
    }

    public override bool ClosePanel()
    {
        base.ClosePanel();

        ChestContentUIManager.Instance.DynamicPanel = null;

        return true;
    }

    public override void SetupPanel()
    {
        base.SetupPanel();

        var chest = FindChestInCollection(CurrentChestName);
        if (chest == null)
            throw new Exception($"Chest {CurrentChestName} not found");

        ChestContentUIManager.Instance.SetList(chest.GetContent());
        ChestContentUIManager.Instance.DynamicPanel = DynamicPanel;
        ChestContentUIManager.Instance.SetupPanel();

        // setup models list
        //DynamicPanel.GetComponent<ChestPanelController>().SetUp(chest.GetContent());
    }

    public override void UpdateList()
    {
        if (CurrentChestName == null || CurrentChestName == "")
            throw new Exception($"You re not in collision with any chest");


        var chest = FindChestInCollection(CurrentChestName);
        if (chest == null)
            throw new Exception($"Chest {CurrentChestName} not found");

        ChestContentUIManager.Instance.SetList(chest.GetContent());
        ChestContentUIManager.Instance.DynamicPanel = DynamicPanel;
        ChestContentUIManager.Instance.UpdateList();

        //DynamicPanel.GetComponent<ChestPanelController>().BuildPanelContent(chest.GetContent());
    }

    public void UpdateChestContent(string chestName, List<IndexValuePair<int, EquippableItem>> chestContent)
    {
        Elements.Where(chest => chest.name == chestName).ToList().ForEach(chest => chest.SetContent(chestContent));
    }
    
    protected override GameObject GetTemplatePanel()
    {
        // Resources = default path - Asset/Resources ... .obj
        return Resources.Load(ITEM_LOCALIZATION + PANEL_NAME) as GameObject;
    }

    private Chest FindChestInCollection(string _chestName)
    {
        return Elements.Find(chest => chest.Name == _chestName);
    }
}