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

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

    public const string ITEM_LOCALIZATION = "UiPanels/Shop/";
    public const string PANEL_NAME = "ShopPanel";
    //public int SLOTS_NUMBER => 24;

    /// <summary>
    /// Keys pair to detect chest
    /// </summary>
    [Space]
    [Header("Current shop unique keys")]
    public string CurrentShopOwnerName = null;   // OR NPC NAME

    public override void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Debug.Log("Destroy " + gameObject);

            Destroy(gameObject);
        }
    }
    
    public override bool OpenPanel()
    {
        if (CurrentShopOwnerName == null || CurrentShopOwnerName == "")
        {
            Debug.Log($"You re not in range of any shop");
               
            return false;
        }

        
        
        // Open additionals panels
        if (!InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.OpenPanel();

        return base.OpenPanel();
    }

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

        ShopContentUIManager.Instance.DynamicPanel = null;

        CurrentShopOwnerName = "";

        // Close additionals panels
        if (InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.ClosePanel();

        return true;
    }

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

        var shop = FindShopInCollection(SceneManager.GetActiveScene().name, CurrentShopOwnerName);
        if (shop == null)
            throw new Exception($"Shop {CurrentShopOwnerName} not found");


        ShopContentUIManager.Instance.SetList(shop.GetContent());
        ShopContentUIManager.Instance.DynamicPanel = DynamicPanel;
        ShopContentUIManager.Instance.SetupPanel();
    }

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


        var shop = FindShopInCollection(SceneManager.GetActiveScene().name, CurrentShopOwnerName);
        if (shop == null)
            throw new Exception($"Chest {CurrentShopOwnerName} not found");

        ShopContentUIManager.Instance.SetList(shop.GetContent());
        ShopContentUIManager.Instance.DynamicPanel = DynamicPanel;
        ShopContentUIManager.Instance.UpdateList();
    }

    public void UpdateShopContent(string mapName, string shopOwnerName, List<IndexValuePair<int, EquippableItem>> chestContent)
    {
        Elements.Where(shop => shop.Map == mapName && shop.Npc == shopOwnerName).ToList().ForEach(shop => shop.SetContent(chestContent));
    }
    
    protected override GameObject GetTemplatePanel()
    {
        // Resources = default path - Asset/Resources ... .obj
        return Resources.Load(ITEM_LOCALIZATION + PANEL_NAME) as GameObject;
    }

    private Shop FindShopInCollection(string _mapName, string _shopOwnerName)
    {
        return Elements.Find(shop => shop.Map == _mapName && shop.Npc == _shopOwnerName);
    }
}