#pragma once

#include <string>
#include <vector>
#include <map>
#include <nlohmann/json.hpp>
#include <fstream>
#include <sstream>
#include <iostream> // 必须包含这个头文件
#include <iomanip>
// 前置声明
struct GaitInfo;
struct SystemParameters;
struct ComponentID;
struct LegModelIDs;
struct ModelID;
struct LegParam;
struct LegParameters;
struct RobotBody;
struct RequestParameters;

// GaitInfo 结构
struct GaitInfo
{
    std::string GaitType;
    double Period;
    double Frequency;
    double StepTime;
    double SupportTime;
    double SwingTime;
    double SupportRatio;
    double SwingRatio;
    int TotalFrames;
    double TotalTime;
};

// SystemParameters 结构
struct SystemParameters
{
    double A_x;
    double A_y;
    double L10;
    double L20;
    double L30;
    double StepLength;
    double StepHeight;
    double X;
    double DeltaX;
    double L21;
    double L22;
    double L23;
    double Beta1;
    double BB2_BC_Angle;
    double C1_B_Length;
    double CC1;
    double L31;
    double C2C3_Length;
    double Lead;
    double D1_L_Offset;
    double D2_L_Offset;
    double C4_D2_Offset;
    double ThighMotorReduction;
    double ShankMotorReduction;
    double AnkleMotorReduction;
};

// ComponentID 结构
struct ComponentID
{
    std::string name;
    std::string uuid;
};

// LegModelIDs 结构
struct LegModelIDs
{
    std::vector<ComponentID> components;
};

// ModelID 结构
struct ModelID
{
    LegModelIDs LF;
    LegModelIDs LH;
    LegModelIDs RF;
    LegModelIDs RH;
};

// LegParam 结构
struct LegParam
{
    double thigh_angle_deg;
    double shank_angle_deg;
    double ankle_angle_deg;
};

// LegParameters 结构
struct LegParameters
{
    LegParam LF;
    LegParam LH;
    LegParam RF;
    LegParam RH;
};

// RobotBody 结构
struct RobotBody
{
    std::string BodyCode;
    double x;
    double y;
    double z;
    double qx;
    double qy;
    double qz;
    double qw;
};

// RequestParameters 主结构
struct RequestParameters
{
    GaitInfo GaitInfo;
    SystemParameters SystemParameters;
    ModelID ModelID;
    LegParameters Param;
    RobotBody RobotBody;
    std::string RobotID = "0";
    double t_percentage;
    // 初始化静态变量 左腿和右腿
    std::map<std::string, std::vector<double>> old_initial_points_dictLF;
    std::map<std::string, std::vector<double>> old_initial_points_dictLH;
    // 静态方法
    static RequestParameters fromJsonString(const std::string &jsonStr);
    static RequestParameters fromJsonFile(const std::string &filename);
    // 静态函数版本
    static RequestParameters &updateFromJson(const nlohmann::json &j, RequestParameters &req_param);

    // 序列化为JSON字符串
    std::string toJsonString() const;

    // 保存到文件
    bool saveToFile(const std::string &filename) const;
    // 显示所有数据的调试函数
    void debugPrint() const;
};

// JSON序列化/反序列化声明
void to_json(nlohmann::json &j, const GaitInfo &g);
void from_json(const nlohmann::json &j, GaitInfo &g);

void to_json(nlohmann::json &j, const SystemParameters &s);
void from_json(const nlohmann::json &j, SystemParameters &s);

void to_json(nlohmann::json &j, const ComponentID &c);
void from_json(const nlohmann::json &j, ComponentID &c);

void to_json(nlohmann::json &j, const LegModelIDs &l);
void from_json(const nlohmann::json &j, LegModelIDs &l);

void to_json(nlohmann::json &j, const ModelID &m);
void from_json(const nlohmann::json &j, ModelID &m);

void to_json(nlohmann::json &j, const LegParam &l);
void from_json(const nlohmann::json &j, LegParam &l);

void to_json(nlohmann::json &j, const LegParameters &l);
void from_json(const nlohmann::json &j, LegParameters &l);

void to_json(nlohmann::json &j, const RobotBody &r);
void from_json(const nlohmann::json &j, RobotBody &r);

void to_json(nlohmann::json &j, const RequestParameters &c);
void from_json(const nlohmann::json &j, RequestParameters &c);