// SliderCrankMechanism.h
#ifndef SLIDER_CRANK_MECHANISM_H
#define SLIDER_CRANK_MECHANISM_H
#include "FourBarMechanism/CrankSliderMechanism.h"
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include "SharedGeometry/SharedGeometry.h"
// 解模式枚举
enum class SolutionMode
{
    Auto = 0,
    Solution1 = 1,
    Solution2 = 2
};

// 机制参数结构体（扩展版本）
struct MechanismParametersExt
{
    double CrankLength;
    double ConnectingRodLength;
    double SliderOffset;
    double InputValue;
    double AngularVelocity;
    SolutionMode SolutionMode;
    std::pair<double, double> CrankAngleRange1;
    std::pair<double, double> CrankAngleRange2;

    MechanismParametersExt();
};
// 解模式枚举

// 曲柄滑块机构逆解类
class SliderCrankMechanism
{
private:
    std::string name;
    MechanismParametersExt parameters;

    // 轨迹数据
    std::vector<Vector2D> trajectoryB;
    std::vector<Vector2D> sliderTrajectory;
    std::vector<Vector2D> trajectoryBAlt;

    // 辅助方法
    double degreesToRadians(double degrees) const;
    double radiansToDegrees(double radians) const;
    std::string formatDouble(double value, int precision = 3) const;
    double clamp(double value, double min, double max) const;

    // 计算滑块范围
    std::pair<double, double> calculateSliderRange();

public:
    SliderCrankMechanism();

    // 基本属性访问
    std::string getName() const;
    MechanismType getType() const;
    MechanismParametersExt getParameters() const;

    void setName(const std::string &newName);
    void setParameters(const MechanismParametersExt &params);

    // 设置连杆长度
    void setLink(double crankLength, double rodLength, double sliderOffset);

    // 设置解模式
    void setSolutionMode(SolutionMode mode);

    // 主要计算函数（逆解）
    MechanismState calculate(double sliderX);

    // 参数验证
    ValidationResult validateParameters();

    // 获取输入范围
    std::pair<double, double> getInputRange();

    // 获取轨迹点
    std::vector<Vector2D> getTrajectoryPoints();
    std::vector<Vector2D> getSliderTrajectory() const;
    std::vector<Vector2D> getAlternativeTrajectory() const;

    // 清除轨迹
    void clearTrajectory();

    // 获取状态文本
    std::string getStatusText();
};

// 工厂函数
SliderCrankMechanism *createSliderCrankMechanism();
void deleteSliderCrankMechanism(SliderCrankMechanism *mechanism);

// 智能指针版本
using SliderCrankMechanismPtr = std::shared_ptr<SliderCrankMechanism>;
SliderCrankMechanismPtr createSliderCrankMechanismSmart();

#endif // SLIDER_CRANK_MECHANISM_H
