using AX.InputSystem;
using AX.NetworkSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OilTanksBoilOver : MonoBehaviour
{
    // 正常罐模型
    public GameObject NomalModel;
    // 喷溅特效
    public GameObject SplashModel;
    // 沸溢罐模型   
    public GameObject BoilOverModel;
    // 罐体信息
    public OilTankMessage OilData;
    //// 罐体是否受热,冷却强度是否足够
    //public bool IsHeating = false;
    // 当前受热时长
    private float currentHeatingTime = 0;
    // 当前沸溢时长
    private float currentSplashingTime = 0;
    // 当前状态
    public OilTanksState State = OilTanksState.Normal;
    // 冷却组件
    public List<OilTanksCooling> oilTanksCoolings;
    private void Awake()
    {
        OilTanksBoilOverManager.AddOilTanksBoilOver(gameObject.name, this); 
    }
    private void OnDestroy()
    {
        OilTanksBoilOverManager.RemoveOilTanksBoilOver(gameObject.name);
    }
    void Update()
    {
        if (GameSettings.othersSettings.mode != Mode.DisasterManagement
            && CurrentUserInfo.role == Role.导调组 
            && State != OilTanksState.BoilOverEnd
            && GameSettings.othersSettings.isStartDrill
            )
        {
            CheckIsHeatingAndCooling();
            switch (State)
            {
                case OilTanksState.Normal:
                    SearchNearUnits();
                    break;
                case OilTanksState.Heating:
                    SearchNearUnits();
                    currentHeatingTime += Time.deltaTime;
                    if (currentHeatingTime >= OilData.BoilOverTime * 60)
                    {
                        int x = Random.Range(1, 101);
                        int temp = OilData.BoilOverProbability;
                        if (x <= temp)
                        {
                            State = OilTanksState.BoilOver;
                            NetworkManager.Default.SendAsync("OLITANKS_BOILOVER_SYNC", gameObject.name);
                        }
                        else
                        {
                            State = OilTanksState.BoilOverEnd;
                        }
                    }
                    break;
                case OilTanksState.Explode:
                    break;
                case OilTanksState.BoilOver:
                    currentSplashingTime += Time.deltaTime;
                    if (currentSplashingTime >= OilData.SplashTime * 60)
                    {
                        int x1 = Random.Range(1, 101);
                        int temp1 = OilData.SplashProbability;
                        if (x1 <= temp1)
                        {
                            NetworkManager.Default.SendAsync("OLITANKS_SPLASH_SYNC", gameObject.name);
                        }
                        State = OilTanksState.BoilOverEnd;
                    }
                    break;
                default:
                    break;
            }
        }
    }
    // 查询罐体是否受热
    public void SearchNearUnits()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, 29f, 1 << LayerMask.NameToLayer("Thermal"));
        if (colliders.Length > 0)
        {
            foreach (var item in oilTanksCoolings)
            {
                item.IsHeating = true;
                item.SetCoolingObject();
            }
        }
        else
        {
            foreach (var item in oilTanksCoolings)
            {
                item.IsHeating = false;
            }
        }
    }
    // 检查是否受热并冷却
    private void CheckIsHeatingAndCooling()
    {
        State = OilTanksState.Normal;
        foreach (var item in oilTanksCoolings)
        {
            if (item.IsHeating && item.IsCooling == false)
            {
                State = OilTanksState.Heating;
            }
        }
    }
    // 沸溢开始
    public void BoilOverStart()
    {
        NomalModel.SetActive(false);
        BoilOverModel.SetActive(true);
        // 如果是导调组,创建
        if (CurrentUserInfo.role == Role.导调组)
        {
            // 如果没有生成全页面火
            //if (CheckHaveLiquidLevel())
            //{
            //    //CloneLiquidLevelFire(NomalModel.transform.position + new Vector3(0, 10, 0));

            //}
            CheckHaveLiquidLevel();
            BoilOverModel.SetActive(true);
            HurtOnMan();
        }
    }
    private void HurtOnMan()
    {
        // 获取所有重伤员
        BaseHaemalController[] wounded = GameObject.Find("P_AllParent/P_Disasters/P_Wounded").GetComponentsInChildren<BaseHaemalController>();
        // 获取所有轻伤员
        BaseHaemalController[] trappedPerson = GameObject.Find("P_AllParent/P_Disasters/P_TrappedPerson").GetComponentsInChildren<BaseHaemalController>();
        // 获取所有伤员
        List<BaseHaemalController> allPerson = new List<BaseHaemalController>();
        allPerson.AddRange(wounded);
        allPerson.AddRange(trappedPerson);
        // 检测伤员与爆炸的距离
        foreach (var item in allPerson)
        {
            float distance = Vector3.Distance(transform.position, item.transform.position);
            if (distance <= OilData.BoilOverDeadRange)
            {
                item.Haemal = 0;
            }
        }
    }
    // 喷溅开始
    public void SplashStart()
    {
        SplashModel.SetActive(true);
        StartCoroutine(SplashEnd());
    }

    private IEnumerator SplashEnd()
    {
        yield return new WaitForSeconds(10f);
        SplashModel.SetActive(false);
    }

    // 检测是否存在全液面火
    public bool CheckHaveLiquidLevel()
    {
        Vector3 position = transform.position + new Vector3(0, 10, 0);
        Collider[] colliders = Physics.OverlapSphere(position, 5f, 1 << LayerMask.NameToLayer("Thermal"));
        if (colliders.Length > 0)
        {
            //  远近由索引限定
            for (int i = 0; i < colliders.Length; i++)
            {
                if (colliders[i].GetComponent<CloneGameObjInfo>().gameObjType == CloneObjType.LiquidLevel)
                {
                    colliders[i].gameObject.SetActive(false);
                    return true;
                }
            }
        }
        return false;
    }

    public void CloneLiquidLevelFire(Vector3 pos)
    {
        var arg = new CloneCmdArgs();
        if (NomalModel.transform.GetComponent<BaseGameObjInfo>())
        {
            arg.gameObjID = NomalModel.transform.GetComponent<BaseGameObjInfo>().GameObjID;
        }
        arg.cloneObjType = CloneObjType.LiquidLevel;
        arg.hitPos = pos;
        CloneCommand.Instance.Execute(EntitiesManager.Instance.CreateObjID(CurrentUserInfo.mySelf.Id), arg);
    }
}

public enum OilTanksState
{
    Normal,
    Heating,
    Explode,
    BoilOver,
    BoilOverEnd
}

public class OilTanksBoilOverManager
{
    static Dictionary<string, OilTanksBoilOver> OilTanksOilTanksBoilOvers = new Dictionary<string, OilTanksBoilOver>();

    public static void AddOilTanksBoilOver(string name, OilTanksBoilOver oilTanksExplode)
    {
        if (!OilTanksOilTanksBoilOvers.ContainsKey(name))
        {
            OilTanksOilTanksBoilOvers.Add(name, oilTanksExplode);
        }
    }
    public static void RemoveOilTanksBoilOver(string name)
    {
        if (OilTanksOilTanksBoilOvers.ContainsKey(name))
        {
            OilTanksOilTanksBoilOvers.Remove(name);
        }
    }
    public static void OilTanksBoilOverByName(string name)
    {
        if (OilTanksOilTanksBoilOvers.ContainsKey(name))
        {
            OilTanksOilTanksBoilOvers[name].BoilOverStart();
        }
    }

    public static void OilTanksSplashByName(string name)
    {
        if (OilTanksOilTanksBoilOvers.ContainsKey(name))
        {
            OilTanksOilTanksBoilOvers[name].SplashStart();
        }
    }
}