using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

[RequireComponent(typeof(ConditionManager))]
class MissionConditionDataManager : SceneBaseDataManager<IndexValuePair<string, List<MissionConditionData>>>
{
    protected override string OBJECT_FOLDER_NAME { get { return "MissionCondition"; } }

    protected override string OBJECT_LIST_NAME { get { return "MissionsConditionsList"; } }

    [SerializeField]
    public List<IndexValuePair<string, List<MissionConditionData>>> MissionConditionDataList;

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

            // moved from start beacuse we need to prepare env before NpcMissionmanager start registering and uipdating mission model
            Debug.Log("Start Mission manager");

            DynamicDataList = new MissionConditionDataListManager();

            DataLoader = new MissionConditionDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);

            base.Start(); // (afterStart invoking - to load saved data)
        }
        else
        {
            Destroy(gameObject);
        }
    }

    // ZMIANA KONCEPCJI
    // ZMIAST TRZYMAC SUROWE OBIEKTY TYM RAZEM ZAPISUJMY modele Datai updatujmy obiket w NPC Managerze 

    // nadpisujemy by nir duplikowac wywoaƂania i nadpisyuwanie MissionDataList z Awake
    public override void Start()
    {

    }


    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 override void UseDefaultSettings()
    {
        DynamicDataList.SetList(new List<IndexValuePair<string, List<MissionConditionData>>>());

        MissionConditionDataList = DynamicDataList.GetList();

        gameObject.GetComponent<ConditionManager>().Conditions = SaveMissionConditionManager.ConvertListOfDataModelsToListOfObject(MissionConditionDataList);
        // 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();
        }

        MissionConditionDataList = DynamicDataList.GetList();

        // map data to model
        gameObject.GetComponent<ConditionManager>().Conditions = SaveMissionConditionManager.ConvertListOfDataModelsToListOfObject(MissionConditionDataList);
    }




/*    public MissionConditionData? GetMissionCondition(MissionCondition newMission)
    {
        return DynamicDataList.GetList().Where(mission => mission.SpeakerName == newMission.SpeakerName && mission.MissionName == newMission.MissionName).FirstOrDefault();
    }*/

    public override bool SaveDynamicData()
    {
        // fill local list by elements from ConditionManager list
        MissionConditionDataList.Clear();

        MissionConditionDataList = SaveMissionConditionManager.ConvertObjectsListToListOfDataModels(ConditionManager.Instance.Conditions);

        return SaveData(MissionConditionDataList, SceneElementTypeEnum.Dynamic);
    }


    public List<MissionConditionData> FindMissionConditionByMissionName(string missionName)
    {
        return MissionConditionDataList
            .Where(el => el.Key == missionName)
            .ToList()
            .SelectMany(el => el.Value)
            .Where(el => el.Type == MissionTypeEnum.Collect)
            .ToList()
        ;
    }
}