using FairyGUI;
using System.Linq;
using BingoBrain.Core;
using UnityEngine;
using BingoBrain.HotFix;
using FGUI.JTodo;
using FGUI.ACommon;
using DG.Tweening;

namespace BingoBrain
{
    public class BasptUI : BaseUI
    {
        private BasptUICtrl ctrl;
        private BasptModel model;
        private com_todo ui;

        private float time = 0;

        public BasptUI(BasptUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.BasptUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "JTodo";
            uiInfo.assetName = "com_todo";
            uiInfo.layerType = UILayerType.Normal;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = false;
            uiInfo.isTickUpdate = true;
        }

        #region 生命周期

        public override void OnUpdate()
        {
            time += Time.deltaTime;
            if (time > 1)
            {
                GameDispatcher.Instance.Dispatch(BingoInfo.UpdateTodoView);
                time = 0;
            }
        }

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

        protected override void OnClose()
        {
            GameDispatcher.Instance.RemoveListener(BingoInfo.GetReward, SetMakeup);
        }

        protected override void OnBind()
        {
            ui = baseUI as com_todo;
        }

        protected override void OnOpenBefore(object args)
        {
            GameDispatcher.Instance.AddListener(BingoInfo.GetReward, SetMakeup);
            if (!GameHelper.IsGiftSwitch())
            {
                ui.btn_cash.visible = false;
                ui.btn_coin.visible = false;
                ui.settings.visible=false;
            }
            else
            {
                (ui.btn_cash as com_102).gift.selectedIndex = 1;
            }
            if (Screen.safeArea.y != 0)
            {//刘海屏
                ui.group_.y += Screen.safeArea.y;
            }
            InitView();
            SetMakeup();
        }

        protected override void OnOpen(object args)
        {
        }

        #endregion
        void SetMakeup(object a = null)
        {

            com_102 cash = ui.btn_cash as com_102;
            com_101 gold = ui.btn_coin as com_101;
            DOVirtual.Float(0, (float)PreferencesMgr.Instance.Currency102, 0.5f,
value => { cash.title.text = value.ToString("0.00"); });
            DOVirtual.Float(0, PreferencesMgr.Instance.Currency101, 0.5f,
value => { gold.title.text = ((int)value).ToString(); });
        }
        private void InitView()
        {

            // ui.list_todo.itemRenderer = OnTodoItemRenderer;
            // ui.list_todo.numItems = 3;
            initBtn();
            ui.settings.SetClick(() =>
            {
                UICtrlDispatcher.Instance.Dispatch(SkinInfo.JThinkUI_Open);
            });
        }
        private void initBtn()
        {
            FGUI.JTodo.btn_todo[] btn_arr = new FGUI.JTodo.btn_todo[] { ui.btn_0, ui.btn_1, ui.btn_2 };
            for (int i = 0; i < btn_arr.Length; i++)
            {
                OnTodoItemRenderer(i, btn_arr[i]);
                if (GameHelper.IsGiftSwitch()) btn_arr[i].gift.selectedIndex = 1;
            }
        }
        private void OnTodoItemRenderer(int index, GObject item)
        {
            if (item is FGUI.JTodo.btn_todo todoItem)
            {
                todoItem.cont_type.selectedIndex = index;
                todoItem.SetClick(() =>
                {
                    switch (index)
                    {
                        case 0:
                            OnClickSignIn();
                            break;
                        case 1:
                            OnClickLuckyWheel();
                            break;
                        case 2:
                            OnClickDailyTask();
                            break;
                    }
                });

                todoItem.cont_red.selectedIndex = index switch
                {
                    0 => IsCanSignIn() ? FGUI.JTodo.btn_todo.Red_red : FGUI.JTodo.btn_todo.Red_none,
                    1 => IsCanLuckyWheel() ? FGUI.JTodo.btn_todo.Red_red : FGUI.JTodo.btn_todo.Red_none,
                    2 => IsCanDailyTask() ? FGUI.JTodo.btn_todo.Red_red : FGUI.JTodo.btn_todo.Red_none,
                    _ => todoItem.cont_red.selectedIndex
                };
            }
        }

        private void OnClickSignIn()
        {
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.JTodUI_Open);
        }

        private void OnClickLuckyWheel()
        {
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoToyUI_Open);
        }

        private void OnClickDailyTask()
        {
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.MissionUI_Open);
        }

        private bool IsCanSignIn()
        {
            return GameHelper.IsCanSignInToday();
        }

        private bool IsCanLuckyWheel()
        {
            return GameHelper.GetNowTime() >= PreferencesMgr.Instance.NextOpenWheelStampTime;
        }

        internal bool IsCanDailyTask()
        {
            bool hasActiveMission = false;
            for (int i = 0; i < PreferencesMgr.Instance.ActiveMissions.Count; i++)
            {
                if (!PreferencesMgr.Instance.ActiveMissions[i].IsReward && PreferencesMgr.Instance.ActiveMissions[i].Progress >= PreferencesMgr.Instance.ActiveMissions[i].MaxProgress)
                {
                    hasActiveMission = true;
                    break;
                }
            }
            return hasActiveMission;


        }

        public void OnRefreshView()
        {
            InitView();
        }
    }
}