// using FGUI.A000_common;

using System;
using System.Collections.Generic;
using DG.Tweening;
using FairyGUI;
using FGUI.Egg_20;
using IgnoreOPS;
using Spine.Unity;
using UnityEngine;
using Random = UnityEngine.Random;

namespace ChillConnect
{
    public class EggViewUI : BaseUI
    {
        private EggViewUICtrl ctrl;

        private com_egg ui;

        private Action closeCallback;
        private bool isShowAnim;

        public EggViewUI(EggViewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.EggViewUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "Egg_20";
            uiInfo.assetName = "com_egg";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = true;

        }

        #region 生命周期
        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {

        }

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

        }

        private List<com_egg_item> eggList = new List<com_egg_item>();

        protected override void OnOpenBefore(object args)
        {

            if (Screen.safeArea.y != 0)
            {
                ui.com_gold.y += 68;
            }


            eggList.Add(ui.egg_item0);
            eggList.Add(ui.egg_item1);
            eggList.Add(ui.egg_item2);
            eggList.Add(ui.egg_item3);

            InitView();
        }


        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }
        #endregion

        #region 消息
        protected override void AddListener()
        {

        }
        protected override void RemoveListener()
        {

        }
        #endregion


        //初始化页面逻辑
        private void InitView()
        {
            ui.com_gold.GetChild("text_gold").text = GameHelper.Get101Str(GameHelper.GetGoldNumber());

            int start = GameHelper.GetCommonModel().eggReward[0];
            int end = GameHelper.GetCommonModel().eggReward[1];
            int randomNumber = Random.Range(start, end + 1);

            ui.btn_close.SetClick(() =>
            {
                if (isShowAnim)
                {
                    GetReward(randomNumber);
                }
                else
                {
                    CtrlCloseUI();
                }
            });
            ui.btn_claim.SetClick(() =>
            {
                if (isShowAnim)
                {
                    GetReward(randomNumber);
                }
            });
            ui.btn_claim_mult.SetClick(() =>
            {
                if (isShowAnim)
                {
                    GameHelper.ShowVideoAd("reward_egg", isSuccess =>
                      {
                          if (isSuccess)
                          {
                              GetReward(randomNumber * 2);
                          }
                      });
                }
            });

            for (int i = 0; i < eggList.Count; i++)
            {
                int index = i;
                eggList[index].egg_state.selectedIndex = 0;
                eggList[index].SetClick(() =>
                {
                    eggList[index].egg.visible = false;
                    setEggClickStatus(false);
                    if (AudioManager.Instance.IsOpenEffect)
                    {
                        AudioManager.Instance.PlayDynamicEffect(AudioConst.egg);
                    }

                    eggList[index].text_num.text = "x" + GameHelper.Get101Str(randomNumber);
                    var ani_node = eggList[index].GetChild("anim_bg") as GGraph;
                    var sk = FXManager.Instance.SetFx<SkeletonAnimation>(ani_node, Fx_Type.fx_egg_light, ref closeCallback);
                    sk.state.SetAnimation(0, "animation", false);
                    sk.state.Complete += a =>
                    {
                        isShowAnim = true;
                        var sk1 = FXManager.Instance.SetFx<SkeletonAnimation>(eggList[index].GetChild("anim_bg1") as GGraph, Fx_Type.fx_egg, ref closeCallback);
                        sk1.state.SetAnimation(0, "animation", true);
                        eggList[index].egg_state.selectedIndex = 1;

                        setCliamStatus(true);
                    };
                });
            }
        }

        void setCliamStatus(bool show)
        {
            ui.btn_claim_mult.visible = show;

            DOVirtual.DelayedCall(1f, () =>
            {
                ui.btn_claim.visible = show;
            });
        }

        void setEggClickStatus(bool isClick)
        {
            foreach (var item in eggList)
            {
                item.touchable = isClick;
            }
        }

        private void GetReward(int awardNum)
        {
            isShowAnim = false;
            var rewardData = new RewardData();
            var start_pos = GameHelper.GetUICenterPosition(ui.btn_claim_mult);
            var end = GameHelper.GetUICenterPosition(ui.com_gold.GetChild("icon"));

            var rewardSingleData = new RewardSingleData(101, awardNum, RewardOrigin.AdTask)
            {
                startPosition = start_pos,
                endPosition = new Vector2(end.x - 150, end.y - 136)
            };

            rewardData.AddReward(rewardSingleData);
            rewardData.displayType = RewardDisplayType.RewardFly | RewardDisplayType.ValueChange;
            rewardData.AddCompleted(success =>
            {
                if (success)
                {
                    var startNum = DataMgr.Coin.Value - awardNum;

                    DOVirtual.Float(startNum, GameHelper.GetGoldNumber(), 1f,
                        value => { ui.com_gold.GetChild("text_gold").text = GameHelper.Get101Str((decimal)value); });

                    DOVirtual.DelayedCall(1.2f, () =>
                    {
                        CtrlCloseUI();
                    });

                    SaveData.GetSaveObject().crush_egg_time = (int)GameHelper.GetNowTime() + GameHelper.GetCommonModel().eggCD;
                    SaveData.saveDataFunc();
                }

            });
            GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardData);
        }
    }
}
