// FourBarMechanism.h
#ifndef FOUR_BAR_MECHANISM_H
#define FOUR_BAR_MECHANISM_H
#include "FourBarMechanism/CrankSliderMechanism.h"
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <chrono>
#include <memory>
#include "SharedGeometry/SharedGeometry.h"

// 前向声明
struct MechanismParameters;
struct MechanismState;
struct ValidationResult;

// 四杆机构类
class FourBarMechanism
{
private:
    std::string name;
    MechanismParameters parameters;

    // 轨迹数据
    std::vector<Vector2D> trajectoryB;
    std::vector<Vector2D> trajectoryC;
    std::vector<Vector2D> crankCirclePoints;

    // 性能统计
    int frameCount;
    std::chrono::steady_clock::time_point startTime;

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

    // 内部计算方法
    bool calculatePointC(const Vector2D &A, const Vector2D &D, const Vector2D &B,
                         double l2, double l3, Vector2D &C) const;
    bool checkGrashofCondition() const;
    bool isShortestLinkCrank() const;

public:
    FourBarMechanism();

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

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

    // 设置杆件长度
    void setLink(double l1, double l2, double l3, double l4);

    // 主要计算函数
    MechanismState calculate(double angleDeg);

    // 参数验证
    ValidationResult validateParameters();

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

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

    // 清除轨迹
    void clearTrajectory();

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

    // 获取参数
    double getL1() const;
    double getL2() const;
    double getL3() const;
    double getL4() const;
};

// 工厂函数
FourBarMechanism *createFourBarMechanism();
void deleteFourBarMechanism(FourBarMechanism *mechanism);

// 智能指针版本
using FourBarMechanismPtr = std::shared_ptr<FourBarMechanism>;
FourBarMechanismPtr createFourBarMechanismSmart();

#endif // FOUR_BAR_MECHANISM_H
