using System;
using FGUI.JMall;
using BingoBrain.Core;

namespace BingoBrain
{
    public class JMallUI : BaseUI
    {
        private JMallUICtrl ctrl;
        private JMallModel model;
        private FGUI.JMall.com_mall ui;

        public JMallUI(JMallUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.JMallUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "JMall";
            uiInfo.assetName = "com_mall";
            uiInfo.layerType = UILayerType.Normal;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = false;
        }

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.JMall.com_mall;
        }

        protected override void OnOpenBefore(object args)
        {
            InitView();
        }

        protected override void OnOpen(object args)
        {
        }

        #endregion

        private void InitView()
        {
            var item1 = ui.list_item.GetChild("item1") as com_mall_carditem;
            var item2 = ui.list_item.GetChild("item2") as com_mall_carditem;
            var item3 = ui.list_item.GetChild("item3") as com_mall_carditem;
            var item4 = ui.list_item.GetChild("item4") as com_mall_coinitem;
            var item5 = ui.list_item.GetChild("item5") as com_mall_coinitem;

            SetRedeemCard(item1, 1, 10);
            SetRedeemCard(item2, 3, 30);
            SetRedeemCard(item3, 10, 90);

            SetRedeemCoin(item4, 10, 10000);
            SetRedeemCoin(item5, 30, 30000);
        }

        private void SetRedeemCard(com_mall_carditem item, int count, int consume, Action<bool> action = null)
        {
            item.text_count.SetVar("count", count.ToString()).FlushVars();

            item.btn_redeem_coin.title = consume.ToString();
            item.btn_redeem_coin.type.selectedIndex = 1;
            item.btn_redeem_coin.SetClick(() =>
            {
                TuSystem.Consume(102, consume, isSuccess =>
                {
                    if (isSuccess)
                    {
                        GameHelper.Toast($"Get {count} Card Success");
                        GameHelper.GetRewardOnly(104, count, RewardOrigin.Redeem, action);
                    }
                    else
                    {
                        GameHelper.Toast("Get Card Failed");
                    }
                });
            });
        }

        private void SetRedeemCoin(com_mall_coinitem item, int count, int consume, Action<bool> action = null)
        {
            item.text_count.SetVar("count", count.ToString()).FlushVars();
            item.btn_redeem_coin.title = consume.ToString();
            item.btn_redeem_coin.SetClick(() =>
            {
                TuSystem.Consume(101, consume, isSuccess =>
                {
                    if (isSuccess)
                    {
                        GameHelper.Toast($"Redeem Success");
                        GameHelper.GetRewardOnly(102, count, RewardOrigin.Redeem, action);
                    }
                    else
                    {
                        GameHelper.Toast("Redeem Failed");
                    }
                });
            });
        }
    }
}