using FGUI.Prop_09;

namespace ChillConnect
{
    public class ChoosePropUI : BaseUI
    {
        private ChoosePropUICtrl ctrl;
        private ChoosePropModel model;
        private com_choose ui;

        public ChoosePropUI(ChoosePropUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.ChoosePropUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
        }

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

        protected override void OnOpenBefore(object args)
        {
            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()
        {
            if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1)
            {
                ui.btn_ad.GetChild("img_saveingpot").visible = true;
            }
            // Debug.Log(GameHelper.GetLevelstate());
            int state = GameHelper.GetLevelstate();
            int need_gold = ConfigSystem.GetConfig<CommonModel>().RevivalCoins;
            ui.btn_gold_buy.GetChild("text_gold").text = GameHelper.Get101Str(need_gold);

            initProp(state);

            // ui.btn_thanks.SetClick(() =>
            // {
            //     float[] cash_array = GameHelper.GetRewardValue(2);
            //     var temp = new SuccessData();
            //     temp.IsWin = false;
            //     temp.cash_number = cash_array[0];
            //     temp.IsLevelSuccess = true;
            //     temp.IsH5Reward = false;
            //     UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.LevelSuccessUI_Open, temp);
            //     UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ChoosePropUI_Close);

            // });
            ui.btn_close.SetClick(() =>
            {
                clsoeFunc();
            });

            ui.btn_thanks.SetClick(() =>
            {
                clsoeFunc();
            });


        }

        private static void clsoeFunc()
        {
            float[] cash_array = GameHelper.GetRewardValue(2);
            var temp = new SuccessData();
            temp.IsWin = false;
            temp.cash_number = cash_array[0];
            temp.IsLevelSuccess = true;
            temp.IsH5Reward = false;
            temp.boost_array = GameHelper.GetRewardBoost(2);
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.LevelSuccessUI_Open, temp);
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ChoosePropUI_Close);
        }

        private void initProp(int state)
        {
            int need_gold = ConfigSystem.GetConfig<CommonModel>().RevivalCoins;

            if (state == 3)
            {
                ui.state.selectedIndex = ui._State_all_;
                ui.btn_ad.SetClick(() =>
                {
                    GameHelper.ShowVideoAd("reward_lobby", isSuccess =>
                    {
                        if (isSuccess)
                        {
                            GameHelper.SetLevelstate(1);
                            GameDispatcher.Instance.Dispatch(GameMsg.Remake_state, true);
                            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ChoosePropUI_Close);
                        }
                    });

                });
                ui.btn_gold_buy.SetClick(() =>
               {
                   if (GameHelper.CheckGoldNumber(need_gold))
                   {
                       GameHelper.AddGoldNumber(-need_gold);
                       GameHelper.SetLevelstate(2);
                       GameDispatcher.Instance.Dispatch(GameMsg.Remake_state, true);
                       GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                       UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ChoosePropUI_Close);
                   }
                   else
                   {
                       GameHelper.ShowTips("Not enough gold");
                   }

               });
            }
            else if (state == 2)
            {
                ui.state.selectedIndex = ui._State_ad;
                ui.btn_ad.SetClick(() =>
                {
                    GameHelper.ShowVideoAd("reward_lobby", isSuccess =>
                    {
                        if (isSuccess)
                        {
                            GameHelper.SetLevelstate(0);
                            GameDispatcher.Instance.Dispatch(GameMsg.Remake_state, true);
                            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ChoosePropUI_Close);
                        }
                    });

                });

            }
            else if (state == 1)
            {
                ui.state.selectedIndex = ui._State_gold;
                ui.btn_gold_buy.SetClick(() =>
               {
                   if (GameHelper.CheckGoldNumber(need_gold))
                   {
                       GameHelper.AddGoldNumber(-need_gold);
                       GameHelper.SetLevelstate(0);
                       GameDispatcher.Instance.Dispatch(GameMsg.Remake_state, true);
                       GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                       UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ChoosePropUI_Close);
                   }
                   else
                   {
                       GameHelper.ShowTips("Not enough gold");
                   }

               });
            }

        }
    }
}