using System.Linq;
using UnityEngine;
using BingoBrain.Core;
using BingoBrain.HotFix;

namespace BingoBrain
{
    public class SomsionCtrl : BaseCtrl
    {
        public static SomsionCtrl Instance { get; private set; }

        private SomsionModel model;

        #region 生命周期

        protected override void OnInit()
        {
            Instance = this;
            model = ModuleBoardk.GetModel(ModelConst.SomsionModel) as SomsionModel;
        }

        protected override void OnDispose()
        {
            Instance = null;
        }

        #endregion

        #region 消息

        protected override void AddListener()
        {
            ctrlDispatcher.AddListener(CtrlMsg.GameNewDays, RestEventDayMissionsProgress);
            gameDispatcher.AddListener(BingoInfo.ShowGameOver, AddBingGoNum);
        }

        protected override void RemoveListener()
        {
            ctrlDispatcher.RemoveListener(CtrlMsg.GameNewDays, RestEventDayMissionsProgress);
            gameDispatcher.RemoveListener(BingoInfo.ShowGameOver, AddBingGoNum);
        }

        #endregion

        /// <summary>
        /// 重置刷新任务
        /// </summary>
        public void RestEventDayMissionsProgress(object obj)
        {
            SetActiveMissionsLst();
        }


        /// <summary>
        /// 设置一次活跃任务
        /// </summary>
        protected void SetActiveMissionsLst()
        {
            PreferencesMgr.Instance.ActiveMissions.Clear();
            var dataList = GameHelper.GetConfig<TaskListModel>().GetDataList();
            // dataList.Select(item => new Task(item.id, item.taskNum))
            //         .ToList()
            //         .ForEach(task =>
            //         {
            //             task.InitData();
            //             PreferencesMgr.Instance.ActiveMissions.Add(task);
            //         });
            for (int i = 0; i < dataList.Count; i++)
            {
                var item = dataList[i];
                var task = new Task(item.id, item.taskNum);
                task.InitData();
                PreferencesMgr.Instance.ActiveMissions.Add(task);
            }

            PreferencesMgr.Instance.SaveActiveMissions();
        }

        /// <summary>
        /// 保存日常任务数据 累加
        /// </summary>
        /// <param name="id">任务的id </param>
        /// <param name="value">值</param>
        public void AddTaskVal(int id, int value = 1)
        {
            // var task = PreferencesMgr.Instance.ActiveMissions.Find((e) => e.Id == id);
            Task task = null; // 假设 TaskType 是任务的类型
            for (int i = 0; i < PreferencesMgr.Instance.ActiveMissions.Count; i++)
            {
                if (PreferencesMgr.Instance.ActiveMissions[i].Id == id)
                {
                    task = PreferencesMgr.Instance.ActiveMissions[i];
                    break;
                }
            }

            if (value > 0 && task is { IsReward: false })
            {
                task.Progress += value;
                task.Progress = Mathf.Clamp(task.Progress, 0, task.MaxProgress);
                PreferencesMgr.Instance.SaveActiveMissions();
            }
        }

        private void AddBingGoNum(object o)
        {
            Instance.AddTaskVal(1);
            Instance.AddTaskVal(2);
            Instance.AddTaskVal(3);
            Instance.AddTaskVal(4);
            Instance.AddTaskVal(5);
        }
    }
}