using SimulationCommon;

namespace Model
{
    public class FXJHGenerate
    {
        public static void FromStartToMission(FlightPlanEditor editor,ref List<TurningPoint> turningPoints) //生成从基地到任务段起点的航路点
        {
            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "爬升",
                TurningPointLongitude = editor.originbase.BaseLongitude, //基地位置
                TurningPointLatitude = editor.originbase.BaseLatitude,
                TurningPointHeight = editor.originbase.BaseHeight,
                TurningPointType = "普通",
                //TurningPointVelocity = editor.climbsegment.ClimbVelocity;
                SegmentFlightFuelConsumption = 1,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });

            // double k;
               double lat1, lon1; //直升机起飞后爬升到的航路点的经纬度,记为经纬度1


               lat1 = (editor.originbase.BaseLatitude + editor.missionpoint.MissionPointLatitude) / 2;
               lon1 = (editor.originbase.BaseLongitude + editor.missionpoint.MissionPointLongitude) / 2;
               
            // k = (editor.missionpoint.MissionPointLatitude - editor.originbase.BaseLatitude) /
            //     (editor.missionpoint.MissionPointLongitude - editor.originbase.BaseLongitude);
            // if (editor.missionpoint.MissionPointLongitude > editor.originbase.BaseLongitude)
            // {
            //     lat1 = 0.08544 * k / (Math.Sqrt(k * k + 1)) + editor.originbase.BaseLatitude; //经验公式
            //     lon1 = 0.08544 / (Math.Sqrt(k * k + 1)) + editor.originbase.BaseLongitude;
            // }
            // else
            // {
            //     lat1 = editor.originbase.BaseLatitude - 0.08544 * k / (Math.Sqrt(k * k + 1));
            //     lon1 = editor.originbase.BaseLongitude - 0.08544 / (Math.Sqrt(k * k + 1));
            // }

            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "平飞",
                TurningPointLongitude = lon1,
                TurningPointLatitude = lat1,
                TurningPointHeight = 2000,
                TurningPointType = "普通",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }

        //获取油耗率
        public static double GetClimbFuelConsumptionRate(FlightPlanEditor editor, double height)
        {
            double temp = Util.GetTemperature(editor.originbase.BaseLongitude, editor.originbase.BaseLatitude);
            Fuel fuel = Util.GetFuel(editor.aircraftparameter.AircraftID, editor.aircraftparameter.AircraftSubType,
                "爬升", temp.ToString(), height.ToString(),
                editor.aircraftparameter.MaxTakeoffWeight.ToString());
            if (fuel == null)
            {
                return 1100;
            }

            return fuel.oilconsume;
        }

        public static double GetCruisingFuelConsumptionRate(FlightPlanEditor editor, double height)
        {
            double temp = Util.GetTemperature(editor.originbase.BaseLongitude, editor.originbase.BaseLatitude);
            Fuel fuel = Util.GetFuel(editor.aircraftparameter.AircraftID, editor.aircraftparameter.AircraftSubType,
                "平飞远航", temp.ToString(), height.ToString(),
                editor.aircraftparameter.MaxTakeoffWeight.ToString());
            if (fuel == null)
            {
                return 600;
            }

            return fuel.oilconsume;
        }

        public static double GetEnduranceFuelConsumptionRate(FlightPlanEditor editor, double height)
        {
            double temp = Util.GetTemperature(editor.originbase.BaseLongitude, editor.originbase.BaseLatitude);
            Fuel fuel = Util.GetFuel(editor.aircraftparameter.AircraftID, editor.aircraftparameter.AircraftSubType,
                "平飞久航", temp.ToString(), height.ToString(),
                editor.aircraftparameter.MaxTakeoffWeight.ToString());
            if (fuel == null)
            {
                return 600;
            }

            return fuel.oilconsume;
        }

        public static double GetDescentFuelConsumptionRate(FlightPlanEditor editor, double height)
        {
            return 2 * GetCruisingFuelConsumptionRate(editor, height) - GetClimbFuelConsumptionRate(editor, height);
        }

        public static double GetHoverFuelConsumptionRate(FlightPlanEditor editor, double height)
        {
            double temp = Util.GetTemperature(editor.originbase.BaseLongitude, editor.originbase.BaseLatitude);
            Fuel fuel = Util.GetFuel(editor.aircraftparameter.AircraftID, editor.aircraftparameter.AircraftSubType,
                "悬停", temp.ToString(), height.ToString(),
                editor.aircraftparameter.MaxTakeoffWeight.ToString());
            if (fuel == null)
            {
                return 200;
            }

            return fuel.oilconsume;
        }


        //获取速度值

        public static double GetClimbVelocity(FlightPlanEditor editor, double height)
        {
            double temp = Util.GetTemperature(editor.originbase.BaseLongitude, editor.originbase.BaseLatitude);
            Fuel fuel = Util.GetFuel(editor.aircraftparameter.AircraftID, editor.aircraftparameter.AircraftSubType,
                "爬升", temp.ToString(), height.ToString(), editor.aircraftparameter.MaxTakeoffWeight.ToString());

            if (fuel == null)
            {
                return 60;
            }

            return fuel.speed;
        }

        public static double GetCruisingVelocity(FlightPlanEditor editor, double height)
        {
            double temp = Util.GetTemperature(editor.originbase.BaseLongitude, editor.originbase.BaseLatitude);
            Fuel fuel = Util.GetFuel(editor.aircraftparameter.AircraftID, editor.aircraftparameter.AircraftSubType,
                "平飞远航", temp.ToString(), height.ToString(), editor.aircraftparameter.MaxTakeoffWeight.ToString());

            if (fuel == null)
            {
                return 200;
            }

            return fuel.speed;
        }

        public static double GetEnduranceVelocity(FlightPlanEditor editor, double height)
        {
            double temp = Util.GetTemperature(editor.originbase.BaseLongitude, editor.originbase.BaseLatitude);
            Fuel fuel = Util.GetFuel(editor.aircraftparameter.AircraftID, editor.aircraftparameter.AircraftSubType,
                "平飞久航", temp.ToString(), height.ToString(), editor.aircraftparameter.MaxTakeoffWeight.ToString());
            if (fuel == null)
            {
                return 200;
            }

            return fuel.speed;
        }

        public static double GetDescentVelocity(FlightPlanEditor editor, double height)
        {
            return GetCruisingVelocity(editor, height) / 2;
        }

        public static void ZhenCha(List<double[]> SC01, FlightPlanEditor editor, ref List<TurningPoint> turningPoints) //侦查模型航路点生成
        {
            int i;
            for (i = 0; i < SC01.Count - 1; i++)
            {
                turningPoints.Add(new TurningPoint
                {
                    TurningPointName = "平飞",
                    TurningPointLongitude = SC01[i][0],
                    TurningPointLatitude = SC01[i][1],
                    TurningPointHeight = SC01[i][2],
                    TurningPointType = "侦查",
                    SegmentFlightFuelConsumption = 3,
                    SegmentFlightTime = 0,
                    RemainingFuel = 0,
                });
            }
        }

        public static void SuoHuaJiang(double resulttime, FlightPlanEditor editor,
           ref List<TurningPoint> turningPoints) //索滑降模型航路点生成
        {
            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "索滑降",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "索滑降",
                SegmentFlightFuelConsumption = 5,
                SegmentFlightTime = resulttime,
                RemainingFuel = 0,
            });

            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "索滑降",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "索滑降",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }

        public static void JIJIU(double resulttime, FlightPlanEditor editor,
   ref List<TurningPoint> turningPoints) //索滑降模型航路点生成
        {
            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "现场急救",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "现场急救",
                SegmentFlightFuelConsumption = 5,
                SegmentFlightTime = resulttime,
                RemainingFuel = 0,
            });

            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "现场急救",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "现场急救",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }

        public static void JIJIU1(double resulttime,MissionEndPoint hospitalPoint,
ref List<TurningPoint> turningPoints) //索滑降模型航路点生成
        {
            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "现场急救",
                TurningPointLongitude = hospitalPoint.MissionEndPointLongitude,
                TurningPointLatitude = hospitalPoint.MissionEndPointLatitude,
                TurningPointHeight = hospitalPoint.MissionEndPointHeight,
                TurningPointType = "现场急救",
                SegmentFlightFuelConsumption = 0,
                SegmentFlightTime = resulttime,
                RemainingFuel = 0,
            });

            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "现场急救",
                TurningPointLongitude = hospitalPoint.MissionEndPointLongitude,
                TurningPointLatitude = hospitalPoint.MissionEndPointLatitude,
                TurningPointHeight = hospitalPoint.MissionEndPointHeight,
                TurningPointType = "现场急救",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = resulttime,
                RemainingFuel = 0,
            });
        }

        public static void XunHu(FlightPlanEditor editor,ref List<TurningPoint> turningPoints)
        {
            int i;
            for (i = 0; i < editor.airroute.Length; i++)
            {
                turningPoints.Add(new TurningPoint
                {
                    TurningPointName = "平飞",
                    TurningPointLongitude = editor.airroute[i].AirRouteLongitude,
                    TurningPointLatitude = editor.airroute[i].AirRouteLatitude,
                    TurningPointHeight = editor.airroute[i].AirRouteHeight,
                    TurningPointType = "普通",
                    SegmentFlightFuelConsumption = 3,
                    SegmentFlightTime = 0,
                    RemainingFuel = 0,
                });
            }
        }

        public static void  MieHuo1(FlightPlanEditor editor,ref List<TurningPoint> turningPoints) //灭火任务从取水点到火场部分的航路点生成
        {
            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "取水",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "取水",
                SegmentFlightFuelConsumption = 3,
                SegmentFlightTime = 120,
                RemainingFuel = 0,
            });
            
            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "洒水",
                TurningPointLongitude = editor.firepoint[0].FirePointLongitude,
                TurningPointLatitude = editor.firepoint[0].FirePointLatitude,
                TurningPointHeight = editor.firepoint[0].FirePointHeight,
                TurningPointType = "巡航",
                SegmentFlightFuelConsumption = 3,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }

        public static MissionEndPoint ZhenChaMissionEndPoint(List<double[]> SC01) //侦查模型任务终点生成
        {
            MissionEndPoint missionEndPoint = new();
            int length = SC01.Count - 1;
            missionEndPoint.MissionEndPointLongitude = SC01[length][0];
            missionEndPoint.MissionEndPointLatitude = SC01[length][1];
            missionEndPoint.MissionEndPointHeight = SC01[length][2];

            return missionEndPoint;
        }

        public static MissionEndPoint SuoHuaJiangMissionEndPoint(FlightPlanEditor editor) //索滑降模型任务终点生成
        {
            MissionEndPoint missionEndPoint = new();
            missionEndPoint.MissionEndPointLongitude = editor.missionpoint.MissionPointLongitude;
            missionEndPoint.MissionEndPointLatitude = editor.missionpoint.MissionPointLatitude;
            missionEndPoint.MissionEndPointHeight = editor.missionpoint.MissionPointHeight;

            return missionEndPoint;
        }
        public static void JijiangMiehuo1(FlightPlanEditor editor,ref List<TurningPoint> turningPoint)
        {
            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }
        public static void JijiangMiehuo(FlightPlanEditor editor,ref List<TurningPoint> turningPoint)
        {
            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.originbase.BaseLongitude,
                TurningPointLatitude = editor.originbase.BaseLatitude,
                TurningPointHeight = editor.originbase.BaseHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
            
            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }


        public static void KouTouKouSong(FlightPlanEditor editor, ref List<TurningPoint> turningPoint)
        {
            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }

        public static void KouTouKouSong1(MissionPoint missionpoint, ref List<TurningPoint> turningPoint)
        {
            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = missionpoint.MissionPointLongitude,
                TurningPointLatitude = missionpoint.MissionPointLatitude,
                TurningPointHeight = missionpoint.MissionPointHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }

        public static void JijiangJiuYuan1(FlightPlanEditor editor, ref List<TurningPoint> turningPoint)
        {
            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 600,
                RemainingFuel = 0,
            });

            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }
        public static void JijiangJiuYuan(FlightPlanEditor editor, ref List<TurningPoint> turningPoint)
        {
            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.originbase.BaseLongitude,
                TurningPointLatitude = editor.originbase.BaseLatitude,
                TurningPointHeight = editor.originbase.BaseHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });

            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 600,
                RemainingFuel = 0,
            });

            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = editor.missionpoint.MissionPointLongitude,
                TurningPointLatitude = editor.missionpoint.MissionPointLatitude,
                TurningPointHeight = editor.missionpoint.MissionPointHeight,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 2,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }

        public static void ZhaoShuiJiuYuan(List<AirRoute> airRoutes, ref List<TurningPoint> turningPoint)
        {
            for (int i = 0;i < airRoutes.Count; i++)
            {
                turningPoint.Add(new TurningPoint
                {
                    TurningPointName = "转运",
                    TurningPointLongitude = airRoutes[i].AirRouteLongitude,
                    TurningPointLatitude = airRoutes[i].AirRouteLatitude,
                    TurningPointHeight = 1000,
                    TurningPointType = "转运",
                    SegmentFlightFuelConsumption = 3,
                    SegmentFlightTime = 0,
                    RemainingFuel = 0,
                });
            }
        }

        public static void ZhaoShuiJiuYuan1(List<AirRoute> airRoutes, ref List<TurningPoint> turningPoint)
        {
            turningPoint.Add(new TurningPoint
            {
                TurningPointName = "转运",
                TurningPointLongitude = airRoutes[airRoutes.Count - 1].AirRouteLongitude,
                TurningPointLatitude = airRoutes[airRoutes.Count - 1].AirRouteLatitude,
                TurningPointHeight = 1000,
                TurningPointType = "转运",
                SegmentFlightFuelConsumption = 3,
                SegmentFlightTime = 1800,
                RemainingFuel = 0,
            });
        }

        public static void SeaSouJiu(FlightPlanEditor editor, ref List<TurningPoint> turningPoints)
        {
            int i;
            for (i = 0; i < editor.airroute.Length; i++)
            {
                turningPoints.Add(new TurningPoint
                {
                    TurningPointName = "巡航",
                    TurningPointLongitude = editor.airroute[i].AirRouteLongitude,
                    TurningPointLatitude = editor.airroute[i].AirRouteLatitude,
                    TurningPointHeight = 150,
                    TurningPointType = "普通",
                    SegmentFlightFuelConsumption = 3,
                    SegmentFlightTime = 0,
                    RemainingFuel = 0,
                });
            }

        }
        public static void SeaSouJiu2(FlightPlanEditor editor, MissionPoint missionPoint, ref List<TurningPoint> turningPoints)
        {
            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "巡航",
                TurningPointLongitude = missionPoint.MissionPointLongitude,
                TurningPointLatitude = missionPoint.MissionPointLatitude,
                TurningPointHeight = 0,
                TurningPointType = "普通",
                SegmentFlightFuelConsumption = 3,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }
        
        public static void LandSouJiu(FlightPlanEditor editor, ref List<TurningPoint> turningPoints)
        {
            int i;
            for (i = 0; i < editor.airroute.Length; i++)
            {
                turningPoints.Add(new TurningPoint
                {
                    TurningPointName = "巡航",
                    TurningPointLongitude = editor.airroute[i].AirRouteLongitude,
                    TurningPointLatitude = editor.airroute[i].AirRouteLatitude,
                    TurningPointHeight = editor.airroute[i].AirRouteHeight,
                    TurningPointType = "普通",
                    SegmentFlightFuelConsumption = 3,
                    SegmentFlightTime = 0,
                    RemainingFuel = 0,
                });
            }

        }

        public static void FromMissionToEnd(FlightPlanEditor editor, MissionEndPoint missionEndPoint, ref List<TurningPoint> turningPoints) //生成从任务段终点到基地的航路点
        {
            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "平飞",
                TurningPointLongitude = missionEndPoint.MissionEndPointLongitude,
                TurningPointLatitude = missionEndPoint.MissionEndPointLatitude,
                TurningPointHeight = missionEndPoint.MissionEndPointHeight,
                TurningPointType = "普通",
                SegmentFlightFuelConsumption = 3,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
       

            //double k;
            double lat2, lon2;
            lat2 = (turningPoints[^1].TurningPointLatitude + editor.originbase.BaseLatitude) / 2;
            lon2 = (turningPoints[^1].TurningPointLongitude + editor.originbase.BaseLongitude) / 2;
            // k = (turningPoints[^1].TurningPointLatitude - editor.originbase.BaseLatitude) /
            //     (turningPoints[^1].TurningPointLongitude - editor.originbase.BaseLongitude);
            // if (turningPoints[^1].TurningPointLongitude > editor.originbase.BaseLongitude)
            // {
            //     lat2 = 0.08544 * k / (Math.Sqrt(k * k + 1)) + editor.originbase.BaseLatitude;
            //     lon2 = 0.08544 / (Math.Sqrt(k * k + 1)) + editor.originbase.BaseLongitude;
            // }
            // else
            // {
            //     lat2 = editor.originbase.BaseLatitude - 0.08544 * k / (Math.Sqrt(k * k + 1));
            //     lon2 = editor.originbase.BaseLongitude - 0.08544 / (Math.Sqrt(k * k + 1));
            // }

            turningPoints.Add(new TurningPoint
            {
                TurningPointName = "降高",
                TurningPointLongitude = lon2,
                TurningPointLatitude = lat2,
                TurningPointHeight = 2000,
                TurningPointType = "普通",
                SegmentFlightFuelConsumption = 4,
                SegmentFlightTime = 0,
                RemainingFuel = 0,
            });
        }
        
        public static void InitializeVelocities(FlightPlanEditor editor,List<TurningPoint> turningPoints, ref double[] velocitys)
        {
            velocitys[0] = GetClimbVelocity(editor, turningPoints[0].TurningPointHeight);
            velocitys[1] = GetCruisingVelocity(editor, turningPoints[1].TurningPointHeight);
            velocitys[2] = GetEnduranceVelocity(editor, turningPoints[2].TurningPointHeight);
            velocitys[3] = GetDescentVelocity(editor, turningPoints[0].TurningPointHeight);
        }
        
      
        public static void InitializeFuelConsumptions(FlightPlanEditor editor, List<TurningPoint> turningPoints, ref double[] fuelConsumptions)
        {
            fuelConsumptions[0] = GetClimbFuelConsumptionRate(editor, turningPoints[0].TurningPointHeight);
            fuelConsumptions[1] = GetCruisingFuelConsumptionRate(editor, turningPoints[1].TurningPointHeight);
            fuelConsumptions[2] = GetEnduranceFuelConsumptionRate(editor, turningPoints[2].TurningPointHeight);
            fuelConsumptions[3] = GetDescentFuelConsumptionRate(editor, turningPoints[0].TurningPointHeight);
            fuelConsumptions[4] = GetHoverFuelConsumptionRate(editor, turningPoints[2].TurningPointHeight);
        }
        public static void FXJHTPDiedai(FlightPlanEditor editor, ref List<TurningPoint> turningPoints, double[] velocitys, double[] fuelConsumptions)
        {
            int i;
            double DEG_TO_RAD_LOCAL = 3.1415926535897932 / 180;
            double[] x = new double[turningPoints.Count];
            double[] y = new double[turningPoints.Count];
            double[] z = new double[turningPoints.Count];

            // LLA坐标转换为ECEF坐标
            for (i = 0; i < turningPoints.Count; i++)
            {
                double lon = turningPoints[i].TurningPointLongitude * DEG_TO_RAD_LOCAL;
                double lat = turningPoints[i].TurningPointLatitude * DEG_TO_RAD_LOCAL;
                double hei = turningPoints[i].TurningPointHeight;

                double a = 6378137.0;
                double b = 6356752.31424518;
                double N = a / (Math.Sqrt(1 - ((a * a - b * b) / (a * a)) * Math.Sin(lat) * Math.Sin(lat)));

                x[i] = (N + hei) * Math.Cos(lat) * Math.Cos(lon);
                y[i] = (N + hei) * Math.Cos(lat) * Math.Sin(lon);
                z[i] = ((b * b * N) / (a * a) + hei) * Math.Sin(lat);
            }

            // 计算每个航段的飞行时间
            for (i = 0; i < turningPoints.Count; i++)
            {
                if (turningPoints[i].SegmentFlightTime == 0)
                {
                    double distanceab;
                    if (i != turningPoints.Count - 1)
                    {
                        distanceab = Math.Sqrt(Math.Pow(x[i] - x[i + 1], 2) + Math.Pow(y[i] - y[i + 1], 2) + Math.Pow(z[i] - z[i + 1], 2));
                    }
                    else
                    {
                        distanceab = Math.Sqrt(Math.Pow(x[i] - x[0], 2) + Math.Pow(y[i] - y[0], 2) + Math.Pow(z[i] - z[0], 2));
                    }

                    double velocity;
                    switch (turningPoints[i].SegmentFlightFuelConsumption)
                    {
                        case 1:
                            velocity = velocitys[0];
                            break;
                        case 2:
                            velocity = velocitys[1];
                            break;
                        case 3:
                            velocity = velocitys[2];
                            break;
                        case 4:
                            velocity = velocitys[3];
                            break;
                        case 5:
                            velocity = 0;
                            break;
                        default:
                            velocity = 0;
                            break;
                    }

                    turningPoints[i].SegmentFlightTime = CalculateSegmentFlightTime(distanceab, velocity);

                    double CalculateSegmentFlightTime(double distance, double velocit)
                    {
                        return distance * 3.6 / velocit;
                    }

                    // 打印调试信息
                    //Console.WriteLine($"航段 {i}: 时间 = {turningPoints[i].SegmentFlightTime} s, 速度 = {velocity} km/h, 距离 = {distanceab} m");
                }
                //Console.WriteLine($"航段 {i}: 时间 = {turningPoints[i].SegmentFlightTime} s");




                // 计算燃油消耗,并在燃油不足时插入“返回基地”、“加油”和“返回原航路点”航路点
                if (turningPoints[i].RemainingFuel == 0)
                {
                    double remainingFuel;
                    if (i == 0)
                    {
                        remainingFuel = editor.aircraftparameter.MaxFuelCapacity * 0.8;
                    }
                    else
                    {
                        remainingFuel = turningPoints[i - 1].RemainingFuel;
                    }

                    double fuelConsumption = 0;
                    switch (turningPoints[i].SegmentFlightFuelConsumption)
                    {
                        case 1:
                            fuelConsumption = fuelConsumptions[0];
                            break;
                        case 2:
                            fuelConsumption = fuelConsumptions[1];
                            break;
                        case 3:
                            fuelConsumption = fuelConsumptions[2];
                            break;
                        case 4:
                            fuelConsumption = fuelConsumptions[3];
                            break;
                        case 5:
                            fuelConsumption = fuelConsumptions[4];
                            break;
                        default:
                            fuelConsumption = 0;
                            break;
                    }

                    remainingFuel -= fuelConsumption * turningPoints[i].SegmentFlightTime / 3600;


                    turningPoints[i].RemainingFuel = remainingFuel;
                    // 打印调试信息
                    //Console.WriteLine($"航段 {i}: 油耗率 = {fuelConsumption} kg/h, 剩余燃油 = {turningPoints[i].RemainingFuel} kg");
                }
                //Console.WriteLine($"航段 {i}: 剩余燃油 = {turningPoints[i].RemainingFuel} kg");

                if (turningPoints[i].RemainingFuel < editor.aircraftparameter.MaxFuelCapacity * 0.2 && !turningPoints[i].IsRefuelPointInserted && i < turningPoints.Count - 2)
                {
                    // 标记当前航路点为已插入加油点
                    turningPoints[i].IsRefuelPointInserted = true;
                    turningPoints[i + 1].IsRefuelPointInserted = true;

                    TurningPoint returnToBase = new TurningPoint
                    {
                        TurningPointName = "返回基地",
                        TurningPointLongitude = editor.originbase.BaseLongitude,
                        TurningPointLatitude = editor.originbase.BaseLatitude,
                        TurningPointHeight = editor.originbase.BaseHeight,
                        TurningPointType = "返航",
                        SegmentFlightTime = 600,
                        SegmentFlightFuelConsumption = 0,
                        RemainingFuel = editor.aircraftparameter.MaxFuelCapacity,

                    };

                    TurningPoint refuelPoint = new TurningPoint
                    {
                        TurningPointName = "加油",
                        TurningPointLongitude = editor.originbase.BaseLongitude,
                        TurningPointLatitude = editor.originbase.BaseLatitude,
                        TurningPointHeight = editor.originbase.BaseHeight,
                        TurningPointType = "加油",
                        SegmentFlightTime = 0,
                        SegmentFlightFuelConsumption = 2,
                        RemainingFuel = 0,


                    };

                    TurningPoint returnToPrevious = new TurningPoint
                    {
                        TurningPointName = "返回原航路点",
                        TurningPointLongitude = turningPoints[i + 1].TurningPointLongitude,
                        TurningPointLatitude = turningPoints[i + 1].TurningPointLatitude,
                        TurningPointHeight = turningPoints[i + 1].TurningPointHeight,
                        TurningPointType = "返航",
                        SegmentFlightTime = 0,
                        SegmentFlightFuelConsumption = 2,
                        RemainingFuel = 0,
                    };

                    // 插入新航路点到当前位置
                    turningPoints.Insert(i + 2, returnToBase);
                    turningPoints.Insert(i + 3, refuelPoint);
                    turningPoints.Insert(i + 4, returnToPrevious);

                    // 重新计算插入的航路点的飞行时间和燃油消耗
                    FXJHTPDiedai(editor, ref turningPoints, velocitys, fuelConsumptions);

                    // 退出当前方法,避免重复计算
                    return;
                }


            }

        }

        public static double CalculateTotalFuelConsumption(FlightPlanEditor editor, List<TurningPoint> turningPoints)
        {
            double initialFuel = editor.aircraftparameter.MaxFuelCapacity; // 起始燃油量
            double totalFuelConsumption = 0; // 总燃油消耗量
            double totalRefuelAmount = 0; // 总加油量

            for (int i = 0; i < turningPoints.Count; i++)
            {
                //// 更新飞行过程中的燃油消耗
                //if (i ==0)
                //{
                //    double segmentFuelConsumed = initialFuel - turningPoints[i].RemainingFuel;
                //}
                //else
                //{
                //    double segmentFuelConsumed = (turningPoints[i - 1].RemainingFuel - turningPoints[i].RemainingFuel);
                //    totalFuelConsumption += segmentFuelConsumed;
                //}

                // 如果当前航路点是加油点,更新燃油并记录加油量
                if (turningPoints[i].TurningPointType == "加油")
                {
                    double refuelAmount = editor.aircraftparameter.MaxFuelCapacity - turningPoints[i - 2].RemainingFuel;
                    totalRefuelAmount += refuelAmount;
                }
            }

            // 最终燃油消耗 = 初始燃油 + 总加油量 - 最终剩余燃油
            double finalFuel = turningPoints.Last().RemainingFuel;
            totalFuelConsumption = (initialFuel + totalRefuelAmount) - finalFuel;

            return totalFuelConsumption;
        }




        public static void CalculateTrueHeading(FlightPlanEditor editor, ref List<TurningPoint> turningPoints)
        {
            for (int i = 0; i < turningPoints.Count - 1; i++)
            {
                // 转换为弧度
                double lat1Rad = turningPoints[i].TurningPointLatitude * Math.PI / 180;
                double lon1Rad = turningPoints[i].TurningPointLongitude * Math.PI / 180;
                double lat2Rad = turningPoints[i + 1].TurningPointLatitude * Math.PI / 180;
                double lon2Rad = turningPoints[i + 1].TurningPointLongitude * Math.PI / 180;
                double deltaLon = lon2Rad - lon1Rad;

                double x = Math.Sin(deltaLon) * Math.Cos(lat2Rad);
                double y = Math.Cos(lat1Rad) * Math.Sin(lat2Rad) - Math.Sin(lat1Rad) * Math.Cos(lat2Rad) * Math.Cos(deltaLon);

                // 计算航向角
                double headingRad = Math.Atan2(x, y);

                // 转换为度并标准化
                double headingDeg = headingRad * 180 / Math.PI;
                headingDeg = (headingDeg + 360) % 360;
                turningPoints[i].HeadingAngle = headingDeg;
            }
        }


        /// <summary>
        /// 巡护用
        /// </summary>
        /// <param name="turningPoints"></param>
        /// <param name="editor"></param>
        /// <param name="nowtime"></param>
        /// <returns></returns>
        public static (CurrentLocation, bool)  GetCurrentLocation(List<TurningPoint> turningPoints, FlightPlanEditor editor,
            double nowtime)
        {
            CurrentLocation currentLocation = new CurrentLocation();

            double[] timetable = new double[editor.airroute.Length + 2]; //airroute.Length表示巡护航线中有几个航路点
            timetable[0] = 0; //设起飞时刻为0
            int segmentnumber = -1;
            int i;
            for (i = 0; i < editor.airroute.Length + 1; i++)
            {
                timetable[i + 1] = timetable[i] + turningPoints[i].SegmentFlightTime;
            }

            for (i = 0; i < editor.airroute.Length + 2; i++)
            {
                if ((nowtime - timetable[i]) >= 0)
                {
                    segmentnumber += 1;
                }
                else
                {
                    break;
                }
            }

            bool isEnd = false || segmentnumber >= turningPoints.Count;

            currentLocation.CurrentLon = turningPoints[segmentnumber].TurningPointLongitude +
                                         (turningPoints[segmentnumber + 1].TurningPointLongitude -
                                          turningPoints[segmentnumber].TurningPointLongitude) *
                                         (nowtime - timetable[segmentnumber]) /
                                         turningPoints[segmentnumber].SegmentFlightTime;
            currentLocation.CurrentLat = turningPoints[segmentnumber].TurningPointLatitude +
                                         (turningPoints[segmentnumber + 1].TurningPointLatitude -
                                          turningPoints[segmentnumber].TurningPointLatitude) *
                                         (nowtime - timetable[segmentnumber]) /
                                         turningPoints[segmentnumber].SegmentFlightTime;
            currentLocation.CurrentHei = turningPoints[segmentnumber].TurningPointHeight +
                                         (turningPoints[segmentnumber + 1].TurningPointHeight -
                                          turningPoints[segmentnumber].TurningPointHeight) *
                                         (nowtime - timetable[segmentnumber]) /
                                         turningPoints[segmentnumber].SegmentFlightTime;
            currentLocation.CurrentFuel = 0;
            currentLocation.Currentvelo = 0;
            currentLocation.Currentsegnum = segmentnumber + 1;
            currentLocation.CurrentCourse = 75;

            return (currentLocation, isEnd);
        }

        //改
        /// <summary>
        /// 计算当前位置
        /// </summary>
        /// <param name="FXJHTP"></param>
        /// <param name="FXJHTPLength"></param>
        /// <param name="nowtime"></param>
        /// <returns></returns>
        public static (CurrentLocation, bool)  GetAllCurrentLocation(List<TurningPoint> turningPoints, double nowtime) //飞机实时位置打印
        {
            CurrentLocation currentLocation = new CurrentLocation();
            double[] timetable = new double[turningPoints.Count + 1];
            timetable[0] = 0;
            int segmentnumber = -1;
            int i;
            for (i = 0; i < turningPoints.Count; i++)
            {
                timetable[i + 1] = timetable[i] + turningPoints[i].SegmentFlightTime;
            }

            for (i = 0; i <  turningPoints.Count + 1; i++)
            {
                if ((nowtime - timetable[i]) >= 0)
                {
                    segmentnumber += 1;
                }
                else
                {
                    break;
                }
            }
            
            bool isEnd = false || segmentnumber >= turningPoints.Count;

            currentLocation.Currentsegnum = segmentnumber;

                if (segmentnumber <  turningPoints.Count - 1)
                {
                    currentLocation.CurrentLon = turningPoints[segmentnumber].TurningPointLongitude +
                                                 ((turningPoints[segmentnumber + 1].TurningPointLongitude -
                                                   turningPoints[segmentnumber].TurningPointLongitude) /
                                                  turningPoints[segmentnumber].SegmentFlightTime) *
                                                 (nowtime - timetable[segmentnumber]);
                    currentLocation.CurrentLat = turningPoints[segmentnumber].TurningPointLatitude +
                                                 ((turningPoints[segmentnumber + 1].TurningPointLatitude -
                                                   turningPoints[segmentnumber].TurningPointLatitude) /
                                                  turningPoints[segmentnumber].SegmentFlightTime) *
                                                 (nowtime - timetable[segmentnumber]);
                    currentLocation.CurrentHei = turningPoints[segmentnumber].TurningPointHeight +
                                                 ((turningPoints[segmentnumber + 1].TurningPointHeight -
                                                   turningPoints[segmentnumber].TurningPointHeight) /
                                                  turningPoints[segmentnumber].SegmentFlightTime) *
                                                 (nowtime - timetable[segmentnumber]);
                    currentLocation.CurrentFuel = turningPoints[segmentnumber].RemainingFuel -
                                                  turningPoints[segmentnumber].SegmentFlightFuelConsumption *
                                                  (nowtime - timetable[segmentnumber]);
                    currentLocation.PresentMission = turningPoints[segmentnumber].TurningPointName;
                }
                else if (segmentnumber == turningPoints.Count - 1)
                {
                    currentLocation.CurrentLon = turningPoints[segmentnumber].TurningPointLongitude +
                                                 ((turningPoints[0].TurningPointLongitude -
                                                   turningPoints[segmentnumber].TurningPointLongitude) /
                                                  turningPoints[segmentnumber].SegmentFlightTime) *
                                                 (nowtime - timetable[segmentnumber]);
                    currentLocation.CurrentLat = turningPoints[segmentnumber].TurningPointLatitude +
                                                 ((turningPoints[0].TurningPointLatitude -
                                                   turningPoints[segmentnumber].TurningPointLatitude) /
                                                  turningPoints[segmentnumber].SegmentFlightTime) *
                                                 (nowtime - timetable[segmentnumber]);
                    currentLocation.CurrentHei = turningPoints[segmentnumber].TurningPointHeight +
                                                 ((turningPoints[0].TurningPointHeight -
                                                   turningPoints[segmentnumber].TurningPointHeight) /
                                                  turningPoints[segmentnumber].SegmentFlightTime) *
                                                 (nowtime - timetable[segmentnumber]);
                    currentLocation.CurrentFuel = turningPoints[segmentnumber].RemainingFuel -
                                                  turningPoints[segmentnumber].SegmentFlightFuelConsumption *
                                                  (nowtime - timetable[segmentnumber]);
                    currentLocation.PresentMission = turningPoints[segmentnumber].TurningPointName;
                }
                else
                {
                    currentLocation.CurrentLon = turningPoints[^1].TurningPointLongitude;
                    currentLocation.CurrentLat = turningPoints[^1].TurningPointLatitude;
                    currentLocation.CurrentHei = turningPoints[^1].TurningPointHeight;
                    currentLocation.Currentvelo = 0;
                    currentLocation.CurrentFuel = turningPoints[segmentnumber - 1].RemainingFuel -
                                                  turningPoints[segmentnumber - 1].SegmentFlightFuelConsumption *
                                                  turningPoints[segmentnumber - 1].SegmentFlightTime;
                    currentLocation.PresentMission = turningPoints[segmentnumber - 1].TurningPointName;
                
                    isEnd = true;
                }
            return (currentLocation,isEnd);
        }
    }
}