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

class MissionDataManager : SceneBaseDataManager<MissionData>
{
    protected override string OBJECT_FOLDER_NAME { get { return "Mission"; } }
    protected override string OBJECT_LIST_NAME { get { return "MissionsList"; } }

    [SerializeField]
    public List<MissionData> MissionDataList;

    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 MissionDataListManager();

            DataLoader = new MissionDataLoader(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 SceneBaseDataManager<MissionData> GetObjectType()
    {
        return GameObject.FindObjectOfType<MissionDataManager>();
    }

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



    protected override void UseDefaultSettings()
    {
        DynamicDataList.SetList(new List<MissionData>());

        MissionDataList = DynamicDataList.GetList();

        // 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();
        }

        MissionDataList = DynamicDataList.GetList();
    }


    public void RegisterMission(Mission newMission)
    {
        if (DynamicDataList.GetList().Where(mission => mission.SpeakerName == newMission.SpeakerName && mission.MissionName == newMission.MissionName).Any())
        {
            // if dial is already registered - update it
            UpdateMission(newMission);
        }
        else
        {
            var misssionData = SaveMissionManager.ConvertObjectToDataModel(newMission);

            //DynamicDataList.AddElementToList(multiDialogueData); - nie dodwaca bo modyfikacja jednej duplikuje
            MissionDataList.Add(misssionData);
        }
    }

    // update proggress in dialogue - BUT FUNCTION BELOW SYNCH THIS ALREADY righhr???
    public void UpdateMission(Mission newMission)
    {
        if (!DynamicDataList.GetList().Where(mission => mission.SpeakerName == newMission.SpeakerName && mission.MissionName == newMission.MissionName).Any())
            Debug.LogError($"There is no registered dialoge like {newMission.SpeakerName}");
        else
        {
            var missionData = SaveMissionManager.ConvertObjectToDataModel(newMission);


            MissionDataList.RemoveAll(mission => mission.SpeakerName == newMission.SpeakerName && mission.MissionName == newMission.MissionName);

            // duplicated from registerDialogue
            MissionDataList.Add(missionData);
        }
    }

    public MissionData? GetMission(Mission newMission)
    {
        return DynamicDataList.GetList().Where(mission => mission.SpeakerName == newMission.SpeakerName && mission.MissionName == newMission.MissionName).FirstOrDefault();
    }

    public override bool SaveDynamicData()
    {
        return SaveData(MissionDataList, SceneElementTypeEnum.Dynamic);
    }
}