using FGUI.Prop_09;

namespace ChillConnect
{
    public class BuyPropUI : BaseUI
    {
        private BuyPropUICtrl ctrl;
        private BuyPropModel model;
        private com_prop ui;
        private int state_;
        private int prop_num = 1;

        private int _price;

        public BuyPropUI(BuyPropUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.BuyPropUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
            GameHelper.showGameUI = true;

        }

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

        protected override void OnOpenBefore(object args)
        {

            _price = ConfigSystem.GetConfig<CommonModel>().Purchaseprops;
            
            state_ = (int)args;
            ui.prop.selectedIndex = state_;

            ui.btn_buy.SetClick(buyItem);
            setBtnState(state_);

            InitView();
            setPropText();
            SetAddText();
            
            ui.btn_add.state.selectedIndex = 0;
            ui.btn_jian.state.selectedIndex = 1;
            ui.btn_add.SetClick(AddProNum);
            ui.btn_jian.SetClick(ReduceProNum);
            
        }
        void buyItem()
        {
            if (state_ == 0)
            {

            }
            else if (state_ == 1)
            {


            }
            else if (state_ == 2)
            {


            }
            
            int price = _price * prop_num;
            if (GameHelper.CheckGoldNumber(price))
            {
                GameHelper.AddGoldNumber(-price);
                int numbers = GameHelper.GetItemNumber(state_);
                numbers += prop_num;
                GameHelper.SetItemNumber(state_, numbers);
                GameDispatcher.Instance.Dispatch(GameMsg.Sheep_item_refresh);
                GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                GameHelper.ShowTips("The purchase was successful", true);

                CtrlCloseUI();

            }
            else
            {
                GameHelper.ShowTips("Not enough gold");
            }


        }

        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 AddProNum()
        {
            prop_num++;
            SetAddText();
            setPropText();
        }

        private void SetAddText()
        {
            ui.text_add.text = prop_num.ToString();
        }

        private void ReduceProNum()
        {
            prop_num--;
            if (prop_num <= 1)
            {
                prop_num = 1;
            }

            setPropText();
            SetAddText();
        }

        private void setPropText()
        {
            ui.btn_buy.GetChild("text_gold").text = GameHelper.Get101Str( _price * prop_num);
        }

        //初始化页面逻辑
        private void InitView()
        {
            ui.btn_back.SetClick(() =>
            {

                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SheepwindowUI_Close);

            });

            // ui.btn_buy0.SetClick(() =>
            // {
            //     state_ = 0;
            //     ui.prop.selectedIndex = 0;
            //     setBtnState(0);
            // });
            // ui.btn_buy1.SetClick(() =>
            // {
            //     state_ = 1;

            //     ui.prop.selectedIndex = 1;
            //     setBtnState(1);

            // });
            // ui.btn_buy2.SetClick(() =>
            // {
            //     state_ = 2;

            //     ui.prop.selectedIndex = 2;
            //     setBtnState(2);

            // });
        }

        private void setBtnState(int index)
        {
            // ui.btn_buy0.tab_choose.selectedIndex = index == 0 ? 1 : 0;
            // ui.btn_buy1.tab_choose.selectedIndex = index == 1 ? 1 : 0;
            // ui.btn_buy2.tab_choose.selectedIndex = index == 2 ? 1 : 0;
        }
    }
}