using System;
using BingoBrain.Core;
using BingoBrain.HotFix;
using UnityEngine;

namespace BingoBrain
{
    public class SmailUI : BaseUI
    {
        private SmailUICtrl ctrl;
        private SmailModel model;
        public FGUI.ACommon.com_currency ui;

        public SmailUI(SmailUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.SmailUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "ACommon";
            uiInfo.assetName = "com_currency";
            uiInfo.layerType = UILayerType.Top;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = false;
        }

        #region 生命周期

        protected override void OnInit()
        {
            model = ModuleBoardk.GetModel(ModelConst.SmailModel) as SmailModel;
        }

        protected override void OnClose()
        {
            ui?.FadeOut();
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ACommon.com_currency;
        }

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

        protected override void OnOpen(object args)
        {
            ui?.FadeIn();
        }

        #endregion

        private void InitData()
        {
            model.show101 = GameHelper.Get101();
            model.show102 = GameHelper.Get102();
        }

        private void InitView()
        {
            Set101();
            Set102();
            SetName();
            ui.settings.SetClick(OnClickSetting);
            ui.com_avatar.loader_flag.visible = false;
            if (GameHelper.IsGiftSwitch()) ui.btn_cash.gift.selectedIndex = 1;
            if (GameHelper.IsGiftSwitch())
            {
                // ui.visible = false;
                // ui.x -= 250;
            }
            if (Screen.safeArea.y != 0)
            {
                ui.top_group.y = -90;
            }
            ui.btn_coin.SetClick(() =>
            {
                GameDispatcher.Instance.Dispatch(BingoInfo.MainTab, 1);
            });
            ui.btn_cash.SetClick(() =>
            {
                GameDispatcher.Instance.Dispatch(BingoInfo.MainTab, 1);
            });
        }

        private void OnClickSetting()
        {
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.JThinkUI_Open);
        }

        public void OnUpdate101(object obj = null)
        {
            if (obj != null)
            {
                var changeValue = (decimal)obj;
                model.show101 += changeValue;
                model.show101 = Math.Round(model.show101, 2);
                Set101(model.show101.ToString("N"));
            }
        }

        public void Set101(string c101 = null)
        {
            if (c101 == null)
            {
                c101 = GameHelper.Get101().ToString("N0");
            }

            ui.btn_coin.title = $"{c101}";
        }

        public void OnUpdate102(object obj = null)
        {
            if (obj != null)
            {
                var changeValue = (decimal)obj;
                model.show102 += changeValue;
                model.show102 = Math.Round(model.show102, 2);
                Set102(model.show102.ToString("N"));
            }
        }

        public void Set102(string c102 = null)
        {
            if (c102 == null)
            {
                c102 = GameHelper.Get102().ToString("N");
            }

            ui.btn_cash.title = $"{c102}";
        }

        private void SetName()
        {
            GameHelper.SetName(ui.com_avatar.text_name);
        }
    }
}