using Core.State;
using Fort23.Core;
using GameLogic.Combat.CombatState;
using GameLogic.CombatScenesTool;
using UnityEngine;

namespace GameLogic.Combat.CombatTool
{
    public class CombatController
    {
        public static CombatController currActiveCombat;
        public CombatHeroController CombatHeroController;
        public CombatCameraControllder CombatCameraControllder;
        protected StateControl stateControl;
        private AssetHandle scenesHandle;
        private CombatScenesConfig _combatScenesConfig;
        public bool isUpdate;
        private int _currIndex = 0;

        public CombatScenesNodeConfig nextConfig;

        public CombatStateBasic CurrState
        {
            get { return stateControl.CurrIState as CombatStateBasic; }
        }

        public async CTask InitCombat()
        {
            currActiveCombat = this;
            stateControl = new StateControl();
            stateControl.AddState("idle", new CombatIdleState(this));
            stateControl.AddState("update", new CombatUpdateState(this));
            scenesHandle =
                await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<GameObject>("TestCombatScenes.prefab");
            GameObject gameObject = scenesHandle.AssetObject<GameObject>();
            gameObject.SetActive(true);
            _combatScenesConfig = gameObject.GetComponent<CombatScenesConfig>();
            nextConfig = _combatScenesConfig.allNodeConfig[_currIndex];
            Camera camera = gameObject.transform.GetComponentInChildren<Camera>();
            CombatCameraControllder = new CombatCameraControllder();
            CombatCameraControllder.Init(this, camera);
            CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();

            CombatHeroController = new CombatHeroController();
            TestCombat(cTaskAwaitBuffer);
            await cTaskAwaitBuffer.WaitAll();
            ChangeState("update");
            isUpdate = true;
        }


        private void TestCombat(CTaskAwaitBuffer cTaskAwaitBuffer)
        {
            TestCombatHeroConfig testCombatHeroConfig = GameObject.FindObjectOfType<TestCombatHeroConfig>();
            if (testCombatHeroConfig != null)
            {
                for (int i = 0; i < testCombatHeroConfig.myHeroInfo.Length; i++)
                {
                    int index = i;
                    CombatHeroInfo combatHeroInfo = testCombatHeroConfig.myHeroInfo[i];
                    CombatHeroEntity heroEntity = new CombatHeroEntity();
                    heroEntity.IsEnemy = false;
                    cTaskAwaitBuffer.AddTask(heroEntity.Init(new CombatAIBasic(), combatHeroInfo,
                        delegate(CombatHeroEntity entity)
                        {
                            CombatHeroController.AddHero(entity);
                            heroEntity.combatHeroGameObject.SetPosition(nextConfig.heroStartPos.position);
                        }));
                }

                for (int i = 0; i < testCombatHeroConfig.enemyHeroInfo.Length; i++)
                {
                    int index = i;
                    CombatHeroInfo combatHeroInfo = testCombatHeroConfig.enemyHeroInfo[i];
                    CombatHeroEntity heroEntity = new CombatHeroEntity();
                    heroEntity.IsEnemy = true;
                    cTaskAwaitBuffer.AddTask(heroEntity.Init(new CombatAIBasic(), combatHeroInfo,
                        delegate(CombatHeroEntity entity)
                        {
                            CombatHeroController.AddHero(entity);
                            heroEntity.combatHeroGameObject.SetPosition(nextConfig.monsterPoint[index].position);
                            ;
                        }));
                }
            }
        }

        public void ChangeState(string name)
        {
            stateControl.ChangeState(name);
        }

        public void Update(float t)
        {
            if (!isUpdate)
            {
                return;
            }

            stateControl.Update(t);
        }
    }
}