using System;
using UnityEngine;
using FGUI.G002_main;
using Spine.Unity;
using DG.Tweening;

namespace ScrewsMaster
{
    public class FirstRewardUI : BaseUI
    {
        private FirstRewardUICtrl ctrl;
        private FirstRewardModel model;
        private FGUI.G002_main.com_firstreward ui;
        private Action closeCallback;
        private Action open_sign;
        private bool is_get = false;
        public FirstRewardUI(FirstRewardUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.FirstRewardUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期
        protected override void OnInit()
        {
            model = ModuleManager.Instance.GetModel(ModelConst.FirstRewardModel) as FirstRewardModel;
        }

        protected override void OnClose()
        {
            closeCallback();
            //open_sign();
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.G002_main.com_firstreward;
        }

        protected override void OnOpenBefore(object args)
        {
            if (Screen.safeArea.y != 0)
            {//刘海屏
                ui.money.y += Screen.safeArea.y;
            }
            InitView();
        }

        protected override void OnOpen(object args)
        {
            //open_sign = (System.Action)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.text_num.text = GameHelper.GetPaymentTypeVO().payicon + ConfigSystem.GetConfig<CommonModel>().InitialNum;
            var sk = FXManager.Instance.SetFx<SkeletonAnimation>(ui.spine_parent, Fx_Type.fx_first_reward, ref closeCallback);
            sk.state.SetAnimation(0, "newbie_reward_an01", false);
            sk.state.Complete += (a) =>
            {
                sk.state.SetAnimation(0, "newbie_reward_an02", true);
                ui.btn_getreward.SetClick(() =>
                {
                    ui.btn_getreward.visible = false;
                    sk.state.SetAnimation(0, "newbie_reward_an03", false);
                    sk.state.Complete += (a) =>
                    {
                        ui.state.selectedIndex = com_firstreward.State_page;
                        
                        
                        
                    };
                });

            };
            ui.btn_get.SetClick(GetReward);
            ui.btn_close.SetClick(GetReward);
            ui.money.GetChild("number_text").text = GameHelper.Get102Str(GameHelper.Get102());
            
            var lightSk = FXManager.Instance.SetFx<SkeletonAnimation>(ui.bg_parent, Fx_Type.fx_win_light, ref closeCallback);
            lightSk.state.SetAnimation(0, "animation", true);
        }
        private void GetReward()
        {
            if (is_get) return;
            is_get = true;

            // GameHelper.GetRewardOnly(ConfigSystem.GetConfig<CommonModel>().InitialNum, RewardOrigin.OpenReward, ui.btn_get, ui.end_point, isSuccess =>
            // {
            //     // PreferencesMgr.Instance.IsShowOpenReward = false;
            //     // PreferencesMgr.Instance.OpenRewardCount++;

            // });
            var start = GameHelper.GetUICenterPosition(ui.img_cash);
            var end = GameHelper.GetUICenterPosition(ui.money.GetChild("number_text"));

            float cash = (float)ConfigSystem.GetConfig<CommonModel>().InitialNum;
            reward_data temp = new reward_data() { start = start, end = end, change = cash, type = 111 };
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.CoinWnd_newUI_Open, temp);
            GameHelper.AddMoney(cash);
            GameDispatcher.Instance.Dispatch(GameMsg.RefreshMakeupData);
            Debug.Log(cash);
            DOVirtual.DelayedCall(1, () =>
            {
                DOVirtual.Float(0, (float)GameHelper.Get102(), 1,
                    value => { ui.money.GetChild("number_text").text = GameHelper.Get102Str((decimal)value); });
                PlayerPrefs.SetInt("FirstReward", 1);

            });
            DOVirtual.DelayedCall(2.5f, () =>
            {
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.FirstRewardUI_Close);
            });
        }


    }
}