using System;
using BingoBrain.Asset;
using BingoBrain.Core;
using UnityEngine;
using UnityEngine.UI;

namespace BingoBrain
{
    public class Cheat : BaseUnity<Cheat>
    {
        public bool isCheat = true;
        private int triggerCount = 5;
        private int currentTriggerCount;

        private float currentTime;

        private int cheatPassword = 31415;

        private const string cheatPath = "Prefab/Cheat/CheatCanvas";
        private Transform cheatTransform;
        private Transform checkPermissionPanel;
        private Transform cheatSettingPanel;
        private Transform cheatSettingPanelContent;
        public float Speed = 1;

        public override void Init()
        {
            base.Init();
            cheatPassword = 1314;
            AddListener();
        }

        private void Update()
        {
            if (currentTriggerCount > 0)
            {
                if (Time.time - currentTime > 1)
                {
                    currentTriggerCount = 0;
                }
            }

            if (!isCheat)
            {
                return;
            }

#if UNITY_EDITOR
            if (Input.GetKeyDown(KeyCode.F12))
            {
                OpenCheatView();
            }

            if (Input.GetKeyDown(KeyCode.F5))
            {
                /*if (UI.Instance.IsExistUI(UIConst.ReconnectUI))
                {
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ReconnectUI_Close);
                }
                else
                {
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ReconnectUI_Open);
                }*/
            }

            if (Input.GetKeyDown(KeyCode.F6))
            {
                /*GameHelper.GetRewardOnly(new AwardInfo()
                {
                    diamond = 100, money = 100, active = 100
                }, RewardOrigin.DailyTask, s =>
                {
                    Debug.Log("eeeeeeeeee");
                });*/
            }

            if (Input.GetKeyDown(KeyCode.F1))
            {
                // UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.GameNotificationUI_Open);
                PreferencesMgr.Instance.Currency101 += 50000;
            }

            if (Input.GetKeyDown(KeyCode.F2))
            {
                // UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.GameNotificationUI_Open);
                PreferencesMgr.Instance.Currency102 += 500;
            }

            if (Input.GetKeyDown(KeyCode.F3))
            {
                CtrlDispatcher.Instance.Dispatch(CtrlMsg.WatchVideoFinish);
            }
#endif

            if (Input.touchCount >= 2)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
                {
                    OpenCheatView();
                }
            }
        }

        private void AddListener()
        {
            // CtrlDispatcher.Instance.AddListener(CtrlMsg.Game_Start, OnGameStart);
        }

        private void OnGameStart(object obj)
        {
            InitCheat();
        }

        private void InitCheat()
        {
        }

        public void OnTriggerCheat()
        {
            /*if (Channel.IsRelease)
            {
                return;
            }*/

            // Debug.LogWarning("开始触发GM");
            if (!isCheat)
            {
                return;
            }

            currentTime = Time.time;
            currentTriggerCount++;
            // Debug.LogWarning("点击GM标题");
            if (currentTriggerCount >= triggerCount)
            {
                OpenCheatView();
            }
        }

        private bool m_Alpha = false;

        private void OpenCheatView()
        {
#if BingoBrainDebug || UNITY_EDITOR
            if (cheatTransform != null && cheatTransform.gameObject.activeSelf)
            {
                cheatTransform.gameObject.SetActive(false);
                return;
            }

            // Debug.LogWarning("触发GM界面");
            if (cheatTransform == null)
            {
                // Debug.LogWarning("触发GM界面1");
               var  cheatTransform1 = BetKit.Instance.LoadGameObject("Prefab.Cheat", "CheatCanvas").transform;
               cheatTransform = Instantiate(cheatTransform1);
                checkPermissionPanel = cheatTransform.Find("CheatPanel/CheckPermissionPanel");
                cheatSettingPanel = cheatTransform.Find("CheatPanel/CheatSettingPanel");
                cheatSettingPanelContent = cheatSettingPanel.Find("Viewport/Content");

                var buttonSettingClose = cheatTransform.Find("CheatPanel/CheatSettingPanel/ButtonSettingClose")
                    .GetComponent<Button>();
                buttonSettingClose.onClick.AddListener(() => { cheatTransform.Active(false); });
                buttonSettingClose.gameObject.SetActive(false);

                var passwordInput = checkPermissionPanel.Find("InputField").GetComponent<InputField>();
                // passwordInput.text = "";
                var buttonClose = checkPermissionPanel.Find("ButtonClose").GetComponent<Button>();
                var buttonSure = checkPermissionPanel.Find("ButtonSure").GetComponent<Button>();
                buttonSure.onClick.AddListener(() =>
                {
                    if (passwordInput.text.Equals(cheatPassword.ToString()))
                    {
                        cheatSettingPanel.Active(true);
                        checkPermissionPanel.Active(false);
                        buttonSettingClose.Active(true);
                    }
                });
                buttonClose.onClick.AddListener(() => { cheatTransform.Active(false); });

                var InputFieldAddMoney = cheatSettingPanelContent
                    .Find("ImageGeneral/InputFieldAddMoney")
                    .GetComponent<InputField>();
                InitButton("ImageGeneral/InputFieldAddMoney/ButtonAddMoney", cheatSettingPanelContent,
                    () =>
                    {
                        var inputValue = InputFieldAddMoney.text;
                        var intPut = inputValue.ToInt();
                        // if (intPut > 0)
                        // {
                        PreferencesMgr.Instance.Currency102 += intPut;
                        // }
                    });

                var InputFieldAddCoin = cheatSettingPanelContent
                    .Find("ImageGeneral/InputFieldAddCoin")
                    .GetComponent<InputField>();
                InitButton("ImageGeneral/InputFieldAddCoin/ButtonAddCoin", cheatSettingPanelContent,
                    () =>
                    {
                        var inputValue = InputFieldAddCoin.text;
                        var intPut = inputValue.ToInt();
                        // if (intPut > 0)
                        // {
                        PreferencesMgr.Instance.Currency101 += intPut;
                        // }
                    });


                /*
                InitButton("ImageGeneral/ButtonNewDay", cheatSettingPanelContent,
                    () => { CtrlDispatcher.Instance.Dispatch(CtrlMsg.GameNewDays); });
                    */


                /*InitButton("ImageGeneral/ButtonWebView", cheatSettingPanelContent, () =>
                {
                    m_Alpha = !m_Alpha;
                    cheatSettingPanelContent.Find("ImageGeneral/ButtonWebView/Text").GetComponent<Text>().text =
                        m_Alpha ? "WebView透视 [已开启]" : "WebView透视 [已关闭]";
                    SetAlpha(m_Alpha ? 0.1f : 1);
                });*/

                /*
                InitButton("ImageSign/ButtonResetToday", cheatSettingPanelContent, () =>
                {
                    if (GameHelper.TodaySigned())
                    {
                        PreferencesMgr.Instance.SignState.RemoveAt(PreferencesMgr.Instance.SignState.Count - 1);
                        PreferencesMgr.Instance.SaveSignState();
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SignInUI_Close);
                    }
                });

                InitButton("ImageSign/ButtonResetAllSign", cheatSettingPanelContent, () =>
                {
                    PreferencesMgr.Instance.SignState.Clear();
                    PreferencesMgr.Instance.SaveSignState();
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SignInUI_Close);
                });
                */

                /*
                InitButton("ImageSign/ButtonAddSign", cheatSettingPanelContent, () =>
                {
                    GameHelper.AddSign();
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SignInUI_Close);
                });
                InitButton("ImageSign/ButtonShowSign", cheatSettingPanelContent,
                    () => { UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.SignInUI_Open); });*/

                /*var TextButtonAD = cheatSettingPanelContent.Find("ImageAD/ButtonSkipAD/Text").GetComponent<Text>();
                var TextAD = cheatSettingPanelContent.Find("ImageAD/TextAD").GetComponent<Text>();
                if (ChannelMgr.Instance.IsGMPassAds)
                {
                    TextAD.text = "广告：已跳过";
                    TextButtonAD.text = "观看广告";
                }
                else
                {
                    TextAD.text = "广告：未跳过";
                    TextButtonAD.text = "跳过广告";
                }*/

                /*InitButton("ImageAD/ButtonSkipAD", cheatSettingPanelContent, () =>
                {
                    ChannelMgr.Instance.IsGMPassAds = !ChannelMgr.Instance.IsGMPassAds;
                    if (ChannelMgr.Instance.IsGMPassAds)
                    {
                        TextAD.text = "广告：已跳过";
                        TextButtonAD.text = "观看广告";
                    }
                    else
                    {
                        TextAD.text = "广告：未跳过";
                        TextButtonAD.text = "跳过广告";
                    }
                });

                InitButton("ImageAD/ButtonAddADCount", cheatSettingPanelContent,
                    () => { CtrlDispatcher.Instance.Dispatch(CtrlMsg.WatchVideoFinish); });

                var ButtonGiftSwitch =
                    cheatSettingPanelContent.Find("ImageGiftSwitch/ButtonGiftSwitch").GetComponent<Button>();
                var textGift = ButtonGiftSwitch.transform.Find("Text").GetComponent<Text>();
                textGift.text = GameHelper.IsGiftSwitch() ? "转变为白包" : "转变为黑包";
                InitButton("ImageGiftSwitch/ButtonGiftSwitch", cheatSettingPanelContent, () =>
                {
                    GameHelper.SetGiftSwitch(!GameHelper.IsGiftSwitch());
                    CtrlDispatcher.Instance.Dispatch(CtrlMsg.Module_GiftSwitchChange);
                    textGift.text = GameHelper.IsGiftSwitch() ? "转变为白包" : "转变为黑包";
                });*/

                /*InitButton("ImageGuide/ButtonResetGuide", cheatSettingPanelContent,
                    () => { PreferencesMgr.Instance.IsResetGuide = true; })*/;

                /*InitButton("ImageDaily/ButtonResetDaily", cheatSettingPanelContent,
                    () => { CtrlDispatcher.Instance.Dispatch(CtrlMsg.ResetDailyTask); });

                InitButton("ImageWheel/ButtonRestCD", cheatSettingPanelContent,
                    () => { PreferencesMgr.Instance.NextOpenWheelStampTime = 0; });

                InitButton("ImageInvite/Button7Login", cheatSettingPanelContent,
                    () => { PreferencesMgr.Instance.LoginDay += 7; });


                InitButton("ImageDailyTask/ButtonAddVideo", cheatSettingPanelContent,
                    () => { PreferencesMgr.Instance.DailyTaskAdCount += 2; });

                InitButton("ImageDailyTask/ButtonAddProp", cheatSettingPanelContent,
                    () => { PreferencesMgr.Instance.DailyTaskPropCount += 2; });

                InitButton("ImageDailyTask/ButtonAddCard", cheatSettingPanelContent,
                    () => { PreferencesMgr.Instance.DailyTaskEliminateCount += 2; });

                InitButton("ImageDailyTask/ButtonAddWin", cheatSettingPanelContent,
                    () => { PreferencesMgr.Instance.DailyTaskWinCount += 2; });*/


                /*var InputFieldSetLevel = cheatSettingPanelContent.Find("ImageGod/InputFieldSetLevel")
                    .GetComponent<InputField>();
                InputFieldSetLevel.text = PreferencesMgr.Instance.GameLevel.ToString();
                InitButton("ImageGod/InputFieldSetLevel/ButtonSetLevel", cheatSettingPanelContent, () =>
                {
                    var inputValue = InputFieldSetLevel.text;
                    var intPut = inputValue.ToInt();
                    if (intPut > 0)
                    {
                        PreferencesMgr.Instance.GameLevel = intPut;
                    }
                });*/

                /*InitButton("ImageGod/ButtonWin", cheatSettingPanelContent,
                    () => { GameDispatcher.Instance.Dispatch(GameMsg.GameCheatWin); });*/


                // Debug.LogWarning("触发GM界面2");
            }

            cheatTransform.Active(true);
            checkPermissionPanel.Active(true);
            cheatSettingPanel.Active(false);
#endif
        }

        private void InitButton(string buttonName, Transform parent, Action action)
        {
            var button = parent.Find(buttonName).GetComponent<Button>();
            button.onClick.AddListener(() => { action?.Invoke(); });
        }

        private void SetAlpha(float alpha)
        {
            /*var allUI = UIMgr.Instance.GetExistDynamicUIs();
            if (allUI == null) return;
            for (int i = 0; i < allUI.Count; i++)
            {
                var ui = allUI[i];
                if (null != (ui.baseUI as com_maindi))
                {
                }
                else
                {
                    var welfareUI = ui.baseUI;
                    welfareUI.alpha = alpha;
                }
            }*/
        }
    }
}