using BingoBrain.Core;
using UnityEngine;
using BingoBrain.Asset;
using BingoBrain.HotFix;
using UnityEngine.EventSystems;
using System.Collections.Generic;

namespace BingoBrain
{
    public class Battle : BaseUnity<Battle>
    {
        public ShungTik ShungTik = new();
        List<BaseSystem> systems = new();
        
        public override void Init()
        {
            base.Init();
            CtrlDispatcher.Instance.AddListener(CtrlMsg.Game_Start, OnGameStart);
            GameDispatcher.Instance.AddListener(BingoInfo.AddPause, AddPause);
        }

        private void AddPause(object obj)
        {
            BingoCell.PauseSum += (int)obj;
        }

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

        private GameObject bg;

        private void InitRoot()
        {
            BingoCell.root = new GameObject("GameRoot").transform;
            BingoCell.root.SetParent(OCConst.WorldSpaceGo.transform, false);
            BingoCell.root.localPosition = new Vector3(100, 99.5f);
            BetKit.Instance.LoadGameObjectAndClone("Prefab.Game.Card", "playbg", go =>
            {
                bg = go;
                BetKit.Instance.LoadSprite("Atlas.Game", "dt_bg", (spr) =>
                {
                    bg.GetComponent<SpriteRenderer>().sprite = spr;
                });
                bg.transform.SetParent(BingoCell.root, false);
                CameraBoardk.Instance.mainCamera.gameObject.AddComponent<PhysicsRaycaster>();
                // Debug.Log("??????????????????????????");
                // UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHallUI_Open);
                //UICtrlDispatcher.Instance.Dispatch(SkinInfo.BingoHalldUI_Open);
                
                InitSystems();
                ShungTik.InitRoot(BingoCell.root);
            });
        }

        public void SetBg(bool isShow)
        {
            if (bg != null)
            {
                bg.SetActive(isShow);
            }
        }

        private void InitSystems()
        {
            AddSystems();
        }

        private void AddSystems()
        {
            systems.Add(new BingoSystem());
            systems.Add(new CardBoardSystem());
            systems.Add(new PropSystem());
            systems.Add(new CallSystem());
        }

        private void Update()
        {
            if (systems != null)
            {
                foreach (var sys in systems)
                {
                    sys.Update();
                }
            }
        }
    }
}