using System.Collections.Generic;
using ClumsyCore;
using Newtonsoft.Json;

namespace MultiWheelC;

public class VehicleSyncInfo
{
    [JsonProperty("Master")] public bool Master { get; set; }
    [JsonProperty("Ip")] public string Ip { get; set; } = "";
    [JsonProperty("Port")] public int Port { get; set; } = 8008;
    [JsonProperty("PosAvailable")] public bool PosAvailable { get; set; }
    [JsonProperty("X")] public float X { get; set; }
    [JsonProperty("Y")] public float Y { get; set; }
    [JsonProperty("Th")] public float Th { get; set; }
    [JsonProperty("LayoutX")] public float LayoutX { get; set; }
    [JsonProperty("LayoutY")] public float LayoutY { get; set; }
    [JsonProperty("LayoutTh")] public float LayoutTh { get; set; }
    [JsonProperty("Aligned")] public bool Aligned { get; set; }
    // 本车本轮是否成功识别到邻车（关闭互识别时恒为 true）。任一车为 false 则整队停车。
    [JsonProperty("DetectOk")] public bool DetectOk { get; set; }
    [JsonProperty("MotionFeasible")] public bool MotionFeasible { get; set; } = true;
    [JsonProperty("MotionInfeasibleReason")] public string MotionInfeasibleReason { get; set; } = "";
    [JsonProperty("RotateWheelsAligned")] public bool RotateWheelsAligned { get; set; } = true;
    [JsonProperty("RotateWheelAlignDetail")] public string RotateWheelAlignDetail { get; set; } = "";
    [JsonProperty("AppliedNotificationSeq")] public long AppliedNotificationSeq { get; set; } = -1;
}

public class VehicleSyncNotification
{
    [JsonProperty("PosAvailable")] public bool PosAvailable { get; set; }
    [JsonProperty("CenterX")] public float CenterX { get; set; }
    [JsonProperty("CenterY")] public float CenterY { get; set; }
    [JsonProperty("CenterTh")] public float CenterTh { get; set; }
    [JsonProperty("Aligned")] public bool Aligned { get; set; }
    [JsonProperty("Fleet")] public Dictionary<int, VehicleSyncInfo> Fleet { get; set; } = new();
    [JsonProperty("FleetVx")] public float FleetVx { get; set; }
    [JsonProperty("FleetFrontTh")] public float FleetFrontTh { get; set; }
    [JsonProperty("FleetRearTh")] public float FleetRearTh { get; set; }
    // 联动运动模式：0=常规(前进+转向) 1=蟹行(四轮同向平移) 2=原地旋转(绕车队中心)
    [JsonProperty("Mode")] public int Mode { get; set; }
    // 原地旋转角速度(deg/s，逆时针为正)，仅 Mode==2 有效
    [JsonProperty("FleetOmega")] public float FleetOmega { get; set; }
    [JsonProperty("RequestedFleetOmega")] public float RequestedFleetOmega { get; set; }
    [JsonProperty("FleetMotionReleased")] public bool FleetMotionReleased { get; set; } = true;
    [JsonProperty("FleetStopActive")] public bool FleetStopActive { get; set; }
    [JsonProperty("FleetStopReason")] public string FleetStopReason { get; set; } = "";
    [JsonProperty("FleetStopSourceCar")] public int FleetStopSourceCar { get; set; }
    [JsonProperty("AutoEnabled")] public bool AutoEnabled { get; set; }
    [JsonProperty("ManualEnabled")] public bool ManualEnabled { get; set; }
    [JsonProperty("UseDetourCorrection")] public bool UseDetourCorrection { get; set; }
    [JsonProperty("SyncTh")] public float SyncTh { get; set; }
    [JsonProperty("SyncDistance")] public float SyncDistance { get; set; }
    [JsonProperty("DeltaDetectCenter")] public float DeltaDetectCenter { get; set; }
    // 原地旋转纠偏参数由主车广播，从车运行时使用同一套增益/限幅，避免主从补偿强度不一致。
    [JsonProperty("RotateParamsValid")] public bool RotateParamsValid { get; set; }
    [JsonProperty("RotateActiveOmega")] public float RotateActiveOmega { get; set; }
    [JsonProperty("RotateCompXyFac")] public float RotateCompXyFac { get; set; }
    [JsonProperty("RotateCompXyIFac")] public float RotateCompXyIFac { get; set; }
    [JsonProperty("RotateCompXyMax")] public float RotateCompXyMax { get; set; }
    [JsonProperty("RotateCompThFac")] public float RotateCompThFac { get; set; }
    [JsonProperty("RotateCompThIFac")] public float RotateCompThIFac { get; set; }
    [JsonProperty("RotateCompThMax")] public float RotateCompThMax { get; set; }
    [JsonProperty("RotateCompTangentFrac")] public float RotateCompTangentFrac { get; set; }
    [JsonProperty("RotateStartWheelAlignDeg")] public float RotateStartWheelAlignDeg { get; set; }
    [JsonProperty("RotateActiveWheelAlignDeg")] public float RotateActiveWheelAlignDeg { get; set; }
    // F: 单调递增序列号，从车据此丢弃乱序到达的旧 notify 包。
    [JsonProperty("Seq")] public long Seq { get; set; }
    // D: 自动模式下主车路径控制器算出的车队中心理想位姿（世界系），由 idealPos/idealAngle 透传而来。
    // HasIdeal=true 时各从车按各自 layout 推算 per-car 目标位姿做前馈+补偿，弧线路径不再只靠事后纠偏。
    [JsonProperty("HasIdeal")] public bool HasIdeal { get; set; }
    [JsonProperty("IdealX")] public float IdealX { get; set; }
    [JsonProperty("IdealY")] public float IdealY { get; set; }
    [JsonProperty("IdealTh")] public float IdealTh { get; set; }
}
