#ifndef CRANK_ROCKING_BLOCK_MECHANISM_FORWARD_H
#define CRANK_ROCKING_BLOCK_MECHANISM_FORWARD_H

#include "FourBarMechanism/CrankSliderMechanism.h"
#include <vector>
#include <string>
#include <map>
#include <memory>

// 曲柄摇块机构（正解）
class CrankRockingBlockMechanism_Forward
{
private:
    std::string name;
    MechanismType type;
    MechanismParameters parameters;

    // 轨迹数据
    std::vector<Vector2D> trajectoryB;       // 滑块B的轨迹
    std::vector<Vector2D> crankCirclePoints; // 曲柄端点A的轨迹
    std::vector<Vector2D> rockerTrajectory;  // 摇块轨迹

    // 性能统计
    int frameCount;
    SimpleTimer timer;

    // 配置参数
    double thetaADelta;
    double thetaAValueFactor;
    int thetaAFlip;

public:
    CrankRockingBlockMechanism_Forward();

    // 基本属性
    std::string getName() const;
    MechanismType getType() const;
    MechanismParameters getParameters() const;
    void setParameters(const MechanismParameters &params);

    // 设置连杆长度
    void setLink(double OA, double AB, double OC);
    void setOA(double length);
    void setAB(double length);
    void setOC(double distance);

    // 配置方法
    void setThetaADelta(double delta);
    void setThetaAValueFactor(double factor);
    void setThetaAFlip(int flip);

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

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

    // 轨迹相关
    std::vector<Vector2D> getTrajectoryPoints() const;
    std::vector<Vector2D> getCrankCirclePoints() const;
    std::vector<Vector2D> getRockerTrajectory() const;
    void clearTrajectory();

    // 参数验证
    ValidationResult validateParameters();

    // 状态信息
    std::string getStatusText();

    // 获取配置参数
    double getThetaADelta() const;
    double getThetaAValueFactor() const;
    int getThetaAFlip() const;
    double getOA() const;
    double getAB() const;
    double getOC() const;

private:
    Vector2D calculatePointB(const Vector2D &A, const Vector2D &C, double AB);
    void checkLimitPositions(MechanismState &state, const Vector2D &A,
                             const Vector2D &B, const Vector2D &C,
                             double OA, double AB, double OC);
    double normalizeAngle(double angle);
    std::pair<double, double> getValidCrankRange() const;
    bool checkFullRotationCondition(double OA, double AB, double OC) const;

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

#endif // CRANK_ROCKING_BLOCK_MECHANISM_FORWARD_H
