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

[System.Serializable]
public class SceneShopDataManager : SceneBaseDataManager<Shop>
{
    protected override string OBJECT_FOLDER_NAME { get { return "Shop"; } }
    protected override string OBJECT_LIST_NAME { get { return "ShopList"; } }

    [SerializeField] protected new ShopUIManager UiManager;

    // Instead of build elements on scene we collect shops distracted on scene from npc's
    //[SerializeField] List<Shop> ElementsToBuildOnSceneList = new List<Shop>();


    public override void Awake()
    {
        if (Instance == null)
        {
            Instance = this;

            //Start();
        }
        else
        {
            Destroy(gameObject);
        }
    }

    public override void Start()
    {
        Debug.Log("Start SceneShopData manager");

        //TaskUIManager.FindOrCreateInstance();
        UiManager = ShopUIManager.Instance;

        if (UiManager == null)
            throw new NullReferenceException("ShopUIManager not found!!!");

        DynamicDataList = (new ShopDataListManager()).SetUiManager(ref UiManager);

        DataLoader = new SceneShopDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);

        base.Start();
    }

    // TODO - save ChestPrefabAsset (we need to save coords...) but in manager handle Chest data.. 
    // 
    protected override void AfterStart()
    {
        if(OnMapAppearanceMethod.GameStatus == GameStatus.NewGame)
        {
            if (OnMapAppearanceMethod.Gateway != OnMapAppearanceMethodEnum.NewGame)
                UseDynamicSettings(); // if there is nothing saved we will use UseDefaultSettings
            else
                UseDefaultSettings();
        }else
        {
            UseDynamicSettings();
        }
    }


    protected SceneBaseDataManager<ChestPrefabAsset> GetObjectType()
    {
        return GameObject.FindObjectOfType<SceneChestDataManager>();
    }

    protected SceneBaseDataManager<ChestPrefabAsset> CreateInstance(ref GameObject managerGameObject)
    {
        return managerGameObject.AddComponent<SceneChestDataManager>();
    }



    protected override void UseDefaultSettings()
    {
        DynamicDataList.SetList(new List<Shop>());
        UiManager.SetList(new List<Shop>());
        // we dont need to convert anything
        // Instead of build elements on scene we collect shops distracted on scene from npc's
    }

    protected override void UseDynamicSettings()
    {
        // when chest detect player in near arrea and player press "c"
        // open panel -> chest controller handle Open panel in Scene Chest Manager passing info about what Chest Palyer want to open 
        // Manager Build panel and pass info about chest content 

        LoadDynamicData();

        // VERy VERY provisional SOLUTION
        if (DynamicDataList.GetList().Count == 0)
        {
            UseDefaultSettings();
            return;
        }

        UiManager.SetList(DynamicDataList.GetList());
    }

    // TODO SHOP API
    public void RegisterShop(Shop newShop)
    {
        Debug.Log(DynamicDataList);
        if (DynamicDataList.GetList().Where(shop => shop.Npc == newShop.Npc && shop.Map == SceneManager.GetActiveScene().name).Any())
            Debug.LogWarning("In Shop Manager there is register this shop already");
        else
        {
            DynamicDataList.AddElementToList(newShop);

            UiManager.SetList(DynamicDataList.GetList());
        }
    }



    // TODO manager api
    // open panel extend function
    // close panel extend function


    // TODO chest api
    // add to chest - find chest by its name np
    // remove from chest - select chest by its name - 
    // chesk item in chest - check chest by tis name


    public override bool SaveDynamicData()
    {
        // TODO 
        // how to get list
        // 1. List in UI manager should be synchronized with list in this manager
        Debug.Log(UiManager);
        foreach (Shop shop in UiManager.GetList())
        {
            DynamicDataList.GetList().Where(el => el.Map == shop.Map && el.Npc == shop.Npc).ToList().ForEach(el => el = shop);
        }

        //DynamicDataList.SetList(tmp);

        // approach:
        // 1. get from outside, update local list && us it
        // 2. Handle list synchronized all the time & pass local list
        return SaveData(DynamicDataList?.GetList(), SceneElementTypeEnum.Dynamic);
    }
}