namespace RedHotRoast
{
    public class ChoosePropUI : BaseUI
    {
        private ChoosePropUICtrl ctrl;
        private ChoosePropModel model;
        private FGUI.ZM_Prop_09.com_choose ui;

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

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "ZM_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 FGUI.ZM_Prop_09.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()
        {
            // Debug.Log(GameHelper.GetLevelstate());
            int state = GameHelper.GetLevelstate();
            int need_gold = ConfigSystem.GetCommonConf().RevivalCoins;
            ui.btn_gold_buy.GetChild("text_gold").text = GameHelper.Get101Str(need_gold);

            initProp(state);

            ui.btn_thanks.SetClick(() =>
            {
                float[] ch_array = GameHelper.GetRewardValue(2);
                var temp = new SuccessData();
                temp.IsWin = false;
                temp.ch_number = ch_array[0];
                temp.IsLevelSuccess = true;
                temp.IsH5Reward = false;
                temp.boost_array = GameHelper.GetRewardBoost(2);
                temp.level = GameHelper.GetLevel();
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NewEndUI_Open, temp);
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ChoosePropUI_Close);

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

                float[] ch_array = GameHelper.GetRewardValue(2);
                var temp = new SuccessData();
                temp.IsWin = false;
                temp.ch_number = ch_array[0];
                temp.IsLevelSuccess = true;
                temp.IsH5Reward = false;
                temp.boost_array = GameHelper.GetRewardBoost(2);
                temp.level = GameHelper.GetLevel();
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NewEndUI_Open, temp);
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ChoosePropUI_Close);
            });


        }

        private void initProp(int state)
        {
            int need_gold = ConfigSystem.GetCommonConf().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("no_enough_gold", true);
                   }

               });
            }
            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("no_enough_gold", true);
                   }

               });
            }

        }
    }
}
