using System.Collections.Generic;
using DG.Tweening;
using BingoBrain.Core;
using BingoBrain.HotFix;
using UnityEngine;

namespace BingoBrain
{
    public class BingoSystem : BaseSystem
    {
        public BingoSystem(bool isAutoInit = true)
        {
            if (isAutoInit)
            {
                Init();
            }
        }

        public override void Init()
        {
            base.Init();

            GameDispatcher.Instance.AddPriorityListener(BingoInfo.StartBingoGame, OnStartBingoGame);
            GameDispatcher.Instance.AddListener(BingoInfo.EndBingoGame, OnReset);
            GameDispatcher.Instance.AddPriorityListener(BingoInfo.ShowGameOver, OnShowGameOver);
        }

        public override void Dispose()
        {
            base.Dispose();

            GameDispatcher.Instance.RemovePriorityListener(BingoInfo.StartBingoGame, OnStartBingoGame);
            GameDispatcher.Instance.RemoveListener(BingoInfo.EndBingoGame, OnReset);
            GameDispatcher.Instance.RemovePriorityListener(BingoInfo.ShowGameOver, OnShowGameOver);
        }

        private void OnShowGameOver(object obj)
        {
            BingoCell.isGameOver = true;
        }

        private void InitTestCallList()
        {
            for (int i = 1; i <= 70; i++)
            {
                BingoCell.calledList.Add(i);
            }
        }

        private void OnStartBingoGame(object obj)
        {
            if (BingoCell.IsInGame)
            {
                return;
            }

            OnReset();

            BingoCell.isFirstGame = PlayerPrefs.GetInt("FirstGame", 0) == 0;
            InitBingoList();
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.GodPleAcUI_Close);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoStartUI_Open);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHallUI_Close);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BottomUI_Close);
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHalldUI_Close);
            BingoCell.isGameOver = false;

            BingoCell.IsInGame = true;

            var isFirst = PlayerPrefs.GetInt("FirstGame", 0) == 1;
            if (!isFirst)
            {
                PlayerPrefs.SetInt("FirstGame", 1);
            }
        }

        #region 流程

        void InitBingoList()
        {
            for (var i = 0; i < BingoCell.bingoCount; i++)
            {
                var bingoRow = new List<int>();
                var bingoColumn = new List<int>();
                for (var j = 0; j < BingoCell.bingoCount * BingoCell.bingoCount; j++)
                {
                    if (GetRow(j) == i)
                    {
                        bingoRow.Add(j);
                    }

                    if (GetColumn(j) == i)
                    {
                        bingoColumn.Add(j);
                    }
                }

                BingoCell.bingoDic.Add(i, bingoColumn);
                BingoCell.bingoDic.Add(i + 5, bingoRow);
            }

            var list = new List<int>();
            for (var i = 0; i < 5; i++)
            {
                list.Add(i * (BingoCell.bingoCount + 1));
            }

            BingoCell.bingoDic.Add(10, list);
            var list2 = new List<int>();

            for (var i = 0; i < 5; i++)
            {
                list2.Add(i * (BingoCell.bingoCount - 1) + 4);
            }

            BingoCell.bingoDic.Add(11, list2);

            var list3 = new List<int>
            {
                0,
                4,
                BingoCell.FixedNum,
                24,
                20
            };

            BingoCell.bingoDic.Add(12, list3);
        }

        void OnReset(object o = null)
        {
            var backToMain = false;
            if (o != null)
            {
                backToMain = (bool)o;
            }

            BingoCell.KeyCardSum = 0;
            BingoCell.bingoDic.Clear();
            foreach (var tween in BingoCell.tweens)
            {
                tween?.Kill();
            }

            BingoCell.tweens.Clear();
            BingoCell.isProcedure = false;
            BingoCell.isPropRewarding = false;
            BingoCell.IsInGame = false;
            BingoCell.PauseSum = 0;
            UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoStartUI_Close);
            if (backToMain)
            {
                DOVirtual.DelayedCall(UIMgrConst.OpenUIAnimEffectTime, delegate
                {
                    UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHallUI_Open);
                    UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHalldUI_Open);
                     UICtrlDispatcher.Instance.Dispatch(SkinInfo.GodPleAcUI_Close);
                    if (GameHelper.IsGiftSwitch() && Random.Range(0, 100) < ConfigSystem.GetConfig<CommonModel>().backhallrate)
                    {
                      //  BingoHalldUI.Instance.OnClickHall();
                        BingoHalldUI.Instance.OnClickMainTab();
                    }
                    else
                    {
                        BingoHalldUI.Instance.OnClickMainTab();
                    }
                });
            }
        }

        #endregion

        #region 封装

        public int GetColumn(int index)
        {
            return index / BingoCell.bingoCount;
        }

        public int GetRow(int index)
        {
            return index % BingoCell.bingoCount;
        }

        #endregion
    }
}