using System;
using SGModule.NetKit;
using Spine.Unity;

namespace RedHotRoast
{
    using System.Collections;
    using DG.Tweening;
    using FGUI.ZM_Login_05;
    using FGUI.ZM_Game_04;
    using UnityEngine;
    using UnityEngine.Video;
    using FairyGUI;
    // using FGUI.A000_common;

    public class GameLoginUI : BaseUI
    {
        private GameLoginUICtrl ctrl;
        private GameLoginModel model;
        private FGUI.ZM_Login_05.com_login_page ui;

        public GameLoginUI(GameLoginUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.GameLoginUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "ZM_Login_05";
            uiInfo.assetName = "com_login_page";
            uiInfo.layerType = UILayerType.Normal;
        }

        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
            netFbloadBindLogin?.Kill();
            CommonHelper.FadeOut(ui);
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenBgUI_Close);
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ZM_Login_05.com_login_page;
        }

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

            TrackKit.TrackLoginFunnel(LoginFunnelEventType.EnterButtonShow);


        }

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

        protected override void AddListener()
        {
        }

        protected override void RemoveListener()
        {
        }
        
        private Action closeCallback;

        private void InitView()
        {
            
            var btnAlbumAnim = FXManager.Instance.SetFx<SkeletonAnimation>(ui.btn_login.anim, Fx_Type.fx_login_btn, ref closeCallback);
            btnAlbumAnim.state.SetAnimation(0, "animation", true);

            ui.btn_login.SetClick(OnClickBtn, true);

            App.HideLoadingUI();
            ((com_login_privacy)ui.text_privacy_user).text_privacy_user.SetClick(OnClickPrivacy);
            ((com_login_privacy)ui.text_privacy_user).text_privacy_user.onClickLink.Add((content) =>
            {
                switch (content.data)
                {
                    case "link_href":
                        OnClickTerms();
                        break;
                    case "link_href1":
                        OnClickPrivacy();
                        break;
                }
            });
            ui.btn_space.SetClick(OnclickSpace);
             ui.btn_error.SetClick(OnClickError);
#if GAME_RELEASE
            ui.btn_space.visible = false;
            ui.btn_error.visible = false;
#endif

            //zhushi
            // var temp = new SuccessData();
            // float[] cash_array = GameHelper.GetRewardValue(2);
            // temp.ch_number = cash_array[0];
            // temp.IsWin = true;
            // temp.IsLevelSuccess = true;
            // temp.IsH5Reward = false;
            // temp.rate = (int)cash_array[1];
            // temp.boost_array = GameHelper.GetRewardBoost(2);
            // temp.level = GameHelper.GetLevel();
            // UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NewEndUI_Open, temp);
            // /  UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.LiveUI_Open);
          
        }
        private void OnclickSpace()
        {
            // Debug.Log("OnclickSpace");
#if !UNITY_EDITOR
            MaxSdk.ShowMediationDebugger();
#endif
        }
        
        private void OnClickError()
        {
            int[] test = new int[5];
            try
            {
                Debug.Log("barry This is a runtime error for testing purposes.");
                test[10] = 1;
                // throw new System.Exception($"This is a runtime error for testing purposes. click num = {}");
            }
            catch (System.Exception ex)
            {
                Debug.LogError(ex.Message);
            }
        }

        private Tween netFbloadBindLogin;

        public void OnClickBtn()
        {

            TrackKit.TrackLoginFunnel(LoginFunnelEventType.EnterButtonClick);

            CtrlDispatcher.Instance.Dispatch(CtrlMsg.Game_Start);
            CtrlCloseUI();
        }

        private void OnClickPrivacy()
        {
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PrivacyUI_Open, 0);
        }

        private void OnClickTerms()
        {
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PrivacyUI_Open, 1);
        }
    }
}