using SGame;
using UnityEngine;

namespace ScrewsMaster
{
    using DG.Tweening;
    using FGUI.A000_common;
    using FGUI.G018_GameHome;
    using Spine.Unity;
    using System;

    public class GameLoginUI : BaseUI
    {
        private GameLoginUICtrl ctrl;
        private GameLoginModel model;
        private FGUI.G001_login.com_login ui;
        private Action closeCallback;

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

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "G001_login";
            uiInfo.assetName = "com_login";
            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.G001_login.com_login;
        }

        protected override void OnOpenBefore(object args)
        {
            // var login = FXManager.Instance.SetFx<SkeletonAnimation>(ui.Title_parent, Fx_Type.fx_title, ref closeCallback);
            // login.state.SetAnimation(0, "animation", true);
            //
            // var rabbit = FXManager.Instance.SetFx<SkeletonAnimation>(ui.logo_parent, Fx_Type.fx_rabbit, ref closeCallback);
            // rabbit.state.SetAnimation(0, "animation", true);

            InitView();

            var reqData = new RespLoginFunnelData
            {
                type = "enterButtonShow",
                payload = ""
            };
            NetworkKit.PostFunnelLogin(reqData);
        }

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

        protected override void AddListener()
        {
        }

        protected override void RemoveListener()
        {
        }

        private void InitView()
        {
            ui.btn_login.SetClick(OnClickBtn, true);

            App.HideLoadingUI();
            ((com_loadingBlacklist)ui.com_blklist1).text_privacy.SetClick(OnClickPrivacy);
            ((com_loadingBlacklist)ui.com_blklist1).text_privacy.onClickLink.Add((content) =>
            {
                switch (content.data)
                {
                    case "link_href":
                        OnClickTerms();
                        break;
                    case "link_href1":
                        OnClickPrivacy();
                        break;
                }
            });


            // Action closeCallback = null;
            // var meteor = FXManager.Instance.SetFx<SkeletonAnimation>(ui.pla1, Fx_Type.meteor, ref closeCallback);
            // meteor.state.SetAnimation(0, "liziguang1", true);

            //var login = FXManager.Instance.SetFx<SkeletonAnimation>(ui.pla, Fx_Type.fx_login, ref closeCallback);
            //login.state.SetAnimation(0, "animation", true);

            // var lantern = FXManager.Instance.SetFx<SkeletonAnimation>(ui.pla1, Fx_Type.lantern, ref closeCallback);
            // lantern.state.SetAnimation(0, "deng", true);



        }

        private Tween netFbloadBindLogin;

        public void OnClickBtn()
        {
            UIManagerRegister.AdaptSafeArea();
            var reqData = new RespLoginFunnelData
            {
                type = "enterButtonClick",
                payload = ""
            };
            NetworkKit.PostFunnelLogin(reqData);

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

            ShowScrews.Instance.maskImg.gameObject.SetActive(false);

            // ShowScrews.Instance.InitLogic();

            ShowScrews.Instance.InitAllLeveData();
            ShowScrews.Instance.InitItemBar();
            ShowScrews.Instance.InitSaveData();
            int roomrate = GameHelper.GetCommonModel().roomrate;
            if (UnityEngine.Random.Range(1, 100) < roomrate)
            {
                var level = ShowScrews.Instance.gameMode == GameHome.Mode_difficult ? 1 : GameHelper.GetLevel(); ;
                ShowScrews.Instance.InitLogic(level);
                AudioManager.Instance.PlayBGM(AudioConst.PlayingBg);
            }
            else
            {
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.GameHomeUI_Open);
            }
            // UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.GameHomeUI_Open);
            Debug.Log("dakaidatingjiemian");
        }

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

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