using KYFramework;
using Model;
using MuShiApp;
using SimulationServer.Utils;
using SimulationSingleServer.Utils;
using Unity.Mathematics;
using Random = System.Random;

namespace SimulationServer;

public class AircraftXH : Entity
{
    public string Name; // 飞机名称
    public string AircraftId;
    public string NextMissionId; // 下一个任务ID
    public AircraftDB Db;
    public FlightPlanEditor? FlightPlanEditor; // 飞行计划编辑器
    public List<TurningPoint> turningPoints = new List<TurningPoint>();
    public CurrentLocation currentLocation;
    public EquationHelper helper;
    public XHRescueMission mission;
    
    public double TotalTime;// 单机总效任务时长
    public double TotalFuelConsumption; 
    
    private Random random = new Random();
    private double temptime = 0;
    private double probability = 0;
    private double finalProbability = 1.0;
    private bool isSeeFire = false;
    public bool isOver = false;
    public double TaskReadyTime;
    public bool SyncOver;
    // 火情入场时间
    public double FireEnterTime;
    
    public double[] Velocitys = new double[5]{220,220,220,110,0}; // 速度
    public double[] FuelConsumptions = new double[5]{2800,2800,2800,1000,132}; // 燃油消耗
    
    public void Start()
    {
        FXJHGenerate.FromStartToMission(FlightPlanEditor,ref turningPoints);//生成从起点到任务段起点的航路点
        
        FXJHGenerate.MieHuo1(FlightPlanEditor,ref turningPoints);//生成从任务段起点到任务段终点的航路点
        FXJHGenerate.FXJHTPDiedai(FlightPlanEditor,ref turningPoints, Velocitys,FuelConsumptions);//生成从任务段终点到结束点的航路点

        // 火情入场时间
        foreach (TurningPoint turningPoint in turningPoints)
        {
            FireEnterTime += turningPoint.SegmentFlightTime;
        }
        FireEnterTime += 1800;
        
        turningPoints.Clear();
        FXJHGenerate.FromStartToMission(FlightPlanEditor,ref turningPoints);//生成从起点到任务段起点的航路点
        FXJHGenerate.XunHu(FlightPlanEditor,ref turningPoints);//生成巡航航路点
        FXJHGenerate.FXJHTPDiedai(FlightPlanEditor,ref turningPoints, Velocitys,FuelConsumptions);//生成从任务段终点到结束点的航路点
        Task.Run(() =>
        {
            do
            {
                var location = FXJHGenerate.GetCurrentLocation(turningPoints, FlightPlanEditor, temptime);
                currentLocation = location.Item1;
            
                double3 aricraftPoint = new double3(currentLocation.CurrentLon,currentLocation.CurrentLat,currentLocation.CurrentHei);
         
                double3 targetPoint = new double3(FlightPlanEditor.firepoint[0].FirePointLongitude,FlightPlanEditor.firepoint[0].FirePointLatitude, FlightPlanEditor.firepoint[0].FirePointHeight);
            
                probability = helper.getProbability(aricraftPoint,targetPoint,currentLocation.CurrentCourse);
           
                finalProbability *= (1 - probability);
            
                Console.WriteLine($"巡护任务: {Name} 当前时间:{temptime},当前位置:{currentLocation.CurrentLon},{currentLocation.CurrentLat},{currentLocation.CurrentHei},概率:{probability},最终概率:{1 -finalProbability},是否看到火点:{isSeeFire}");
                var randomValue = random.NextDouble();
            
                if (randomValue < (1 - finalProbability))
                {
                    isSeeFire = true;
                    isOver = true;
                    Log.Info("发现火情");
                }
                else
                {
                    isSeeFire = false;
                }
            
                if (temptime >= 7200)
                {
                    isOver = true;
                }
            
                temptime++;
            } while (!isSeeFire && isOver==false);
            
            finalProbability = 1 - finalProbability;
        
            FXJHGenerate.FromMissionToEnd(FlightPlanEditor, new MissionEndPoint
            {
                MissionEndPointLongitude = currentLocation.CurrentLon,
                MissionEndPointLatitude = currentLocation.CurrentLat,
                MissionEndPointHeight = currentLocation.CurrentHei
            }, ref turningPoints);
            
            FXJHGenerate.FXJHTPDiedai(FlightPlanEditor,ref turningPoints, Velocitys, FuelConsumptions);
            End();
        });


    }
    public void End()
    {
        for (int i = 0; i < currentLocation.Currentsegnum + 2; i++)
        {
            TotalTime += turningPoints[i].SegmentFlightTime;
        }
        TotalFuelConsumption = turningPoints[0].RemainingFuel - turningPoints[currentLocation.Currentsegnum + 1].RemainingFuel;
    }
   
}

[ObjectSystem]
public class AircraftXHAwakeSystem : AwakeSystem<AircraftXH,FlightPlanEditor,string,string>
{
    public override void Awake(AircraftXH self,FlightPlanEditor planEditor,string name,string aircraftId)
    {
        self.FlightPlanEditor = planEditor;
        self.Name = name;
        self.AircraftId = aircraftId;
        
        // 读取数据库
        self.Db = Util.GetAircraftDefine(self.FlightPlanEditor.aircraftparameter.AircraftType,self.FlightPlanEditor.aircraftparameter.AircraftSubType,self.FlightPlanEditor.aircraftparameter.AircraftID);
        self.helper = new EquationHelper(HttpInterface.baseUrl);
    }
}