using System;
using FairyGUI;
using UnityEngine;
using BingoBrain.Core;
using FGUI.JSign;
using System.Collections.Generic;
using DontConfuse;

namespace BingoBrain
{
    public class JTodUI : BaseUI
    {
        private JTodUICtrl ctrl;
        private JTodModel model;
        private com_sign ui;
        private List<GObject> itemLst = new();

        private Action CloseCb;

        public JTodUI(JTodUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.JTodUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "JSign";
            uiInfo.assetName = "com_sign";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = true;
            uiInfo.isNeedCloseAnim = true;
            uiInfo.isNeedUIMask = true;
        }

        #region 生命周期

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

        protected override void OnClose()
        {
            ui?.FadeOut();
            Hall.Instance.UpdateSecondEvent -= UpdateTime;
            WebviewManager.Instance.SetDarkThough(true);
            CloseCb?.Invoke();
        }

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

        protected override void OnOpenBefore(object args)
        {
            WebviewManager.Instance.SetDarkThough(false);
            InitView();
        }

        protected override void OnOpen(object args)
        {
            ui?.FadeIn();
            Hall.Instance.UpdateSecondEvent += UpdateTime;
        }

        #endregion

        #region 消息

        protected override void AddListener()
        {
        }

        protected override void RemoveListener()
        {
        }

        #endregion

        private void InitView()
        {
            ui.fairyBatching = true;
            Audio.Instance.PlayDynamicEffect("pop_open");
            FX.Instance.SetFx<ParticleSystem>(ui.gp_bg, Fx_Type.fx_tanchuang_glow, null, CloseCb);
            ui.closeButton.SetClick(CtrlCloseUI);
            var signDailyRewardModel = GameHelper.GetConfig<SignDailyModel>();
            for (var i = 0; i < signDailyRewardModel.GetCount(); i++)
            {
                var index = i;
                var gObject = ui.list_sign.GetChildAt(i) as GComponent;
                itemLst.Add(gObject);
                FX.Instance.SetFx<ParticleSystem>(gObject.GetChild("gp_fx").asGraph, Fx_Type.fx_icon_glow, null,
                    CloseCb);
                itemLst[i].SetClick(() => { OnClickItem(index); });
            }

            RefreshUI();
        }

        private void OnClickItem(int index)
        {
            var dailySum = model.GetDailyDonusDay();
            if (index + 1 > dailySum)
            {
                return;
            }

            var dailyBonusItem = PreferencesMgr.Instance.DailyBonusItemLst[index];
            if (dailyBonusItem.IsCollect)
            {
                return;
            }

            if (index + 1 == dailySum)
            {
                GetReward(index, dailyBonusItem);
            }
            else
            {
                GameHelper.GetVideo("reward_dailyBonus", isSuccess => { GetReward(index, dailyBonusItem); });
            }
        }

        public void GetReward(int index, DailyBonusItem dailyBonusItem)
        {
            Audio.Instance.PlayDynamicEffect("dailyBonus_collect");

            var startPosition = ui.list_sign.LocalToRoot(itemLst[index].position, GRoot.inst) +
                                itemLst[index].size / 2;

            if (dailyBonusItem.isDouble)
            {
                GameHelper.GetRewardExtra(dailyBonusItem.id, (decimal)dailyBonusItem.quantity,
                    dailyBonusItem.cont_index, null, startPosition);
            }
            else
            {
                GameHelper.GetRewardOnly1(dailyBonusItem.id, (decimal)dailyBonusItem.quantity, RewardOrigin.SignIn,
                    null, startPosition);
            }


            dailyBonusItem.IsCollect = true;
            PreferencesMgr.Instance.SaveDailyBonusItemLst();
            RefreshUI();
        }

        private void UpdateTime()
        {
            ui.text_time.text = "Next in:" + model.GetDailyDonusDayTime();
        }

        private void RefreshUI()
        {
            UpdateTime();
            for (var i = 0; i < itemLst.Count; i++)
            {
                int index = i;
                var dailyBonus = PreferencesMgr.Instance.DailyBonusItemLst[index];
                if (index is 6 or 13)
                {
                    if (GameHelper.IsGiftSwitch()) (itemLst[index] as btn_day2).gift.selectedIndex = 1;
                    if (itemLst[index] is btn_day2 day)
                    {
                        if (dailyBonus.id == 101)
                        {
                            day.cont_currency.selectedIndex = btn_day2.Currency_coin;
                            day.text_reward.text = $"{dailyBonus.quantity:N0}";
                        }
                        else
                        {
                            day.cont_currency.selectedIndex = btn_day2.Currency_diam;
                            day.text_reward.text = (GameHelper.isRDExchangeMode() && GameHelper.IsGiftSwitch()) ?$"{dailyBonus.quantity:N}" : GameHelper.Get102Str((decimal)dailyBonus.quantity);
                        }

                        day.text_day.text = "Day " + (index + 1);

                        if (index + 1 == model.GetDailyDonusDay())
                        {
                            day.cont_state.selectedIndex = btn_day2.State_yellow;
                        }
                        else
                        {
                            day.cont_state.selectedIndex = btn_day2.State_purple;
                        }

                        if (dailyBonus.IsCollect)
                        {
                            day.cont_collect.selectedIndex = btn_day2.Collect_collected;
                            day.cont_state.selectedIndex = btn_day2.State_purple;
                        }
                        else
                        {
                            day.cont_collect.selectedIndex = index + 1 < model.GetDailyDonusDay()
                                ? btn_day2.Collect_miss
                                : btn_day2.Collect_uncollect;
                        }
                    }
                }
                else
                {
                    if (GameHelper.IsGiftSwitch()) (itemLst[index] as btn_day).gift.selectedIndex = 1;
                    if (itemLst[index] is btn_day day)
                    {
                        if (dailyBonus.id == 101)
                        {
                            day.cont_currency.selectedIndex = btn_day.Currency_coin;
                            day.text_reward.text = $"{dailyBonus.quantity:N0}";
                        }
                        else
                        {
                            day.cont_currency.selectedIndex = btn_day.Currency_diam;
                            day.text_reward.text = (GameHelper.isRDExchangeMode() && GameHelper.IsGiftSwitch()) ? $"{dailyBonus.quantity:N}" : GameHelper.Get102Str((decimal)dailyBonus.quantity);
                        }

                        day.text_day.text = "Day " + (index + 1);
                        day.cont_color.selectedIndex = index + 1 == model.GetDailyDonusDay()
                            ? btn_day2.State_yellow
                            : btn_day2.State_purple;

                        if (dailyBonus.IsCollect)
                        {
                            day.cont_collect.selectedIndex = btn_day2.Collect_collected;
                            day.cont_color.selectedIndex = btn_day2.State_purple;
                        }
                        else
                        {
                            day.cont_collect.selectedIndex = index + 1 < model.GetDailyDonusDay()
                                ? btn_day2.Collect_miss
                                : btn_day2.Collect_uncollect;
                        }
                    }
                }
            }
        }
    }
}