#ifndef ROBOT_FSM_H
#define ROBOT_FSM_H

#include <iostream>
#include <string>
#include <map>
#include <memory>
#include <chrono>
#include <thread>

// 前向声明
class InputDevice;

// 机器人运动状态枚举
enum class RobotState {
    STOPPED,       // 停止
    MOVING_FORWARD,// 前进
    MOVING_BACKWARD,// 后退
    TURNING_LEFT,  // 左转
    TURNING_RIGHT, // 右转
    MOVING_UP,     // 上升
    MOVING_DOWN    // 下降
};

// 控制命令枚举
enum class ControlCommand {
    NONE,
    STOP,
    FORWARD,
    BACKWARD,
    LEFT,
    RIGHT,
    UP,
    DOWN
};

// 输入设备类型
enum class InputType {
    KEYBOARD,
    GAMEPAD,
    VOICE
};


class RobotFsm {
private:
    RobotState currentState;
    std::map<InputType, std::unique_ptr<InputDevice>> inputDevices;
    
    // 状态转换表
    std::map<std::pair<RobotState, ControlCommand>, RobotState> transitionTable;
    
    // 初始化状态转换规则
    void initTransitionTable();
    
    // 执行当前状态对应的动作
    void executeStateAction();

public:
    RobotFsm();
    
    // 添加输入设备
    void addInputDevice(InputType type, std::unique_ptr<InputDevice> device);
    
    // 状态机主循环
    void run();
    
    // 处理命令并转换状态
    void processCommand(ControlCommand cmd);
    
    // 获取当前状态
    RobotState getCurrentState() const { return currentState; }
    
    // 状态转换为字符串
    std::string stateToString(RobotState state) const;
};

// 输入设备接口
class InputDevice {
protected:
    InputType type;
    
public:
    InputDevice(InputType t) : type(t) {}
    virtual ~InputDevice() = default;
    
    InputType getType() const { return type; }
    virtual ControlCommand getCommand() = 0;
    virtual std::string getDeviceName() const = 0;
};

// 键盘输入设备
class KeyboardInput : public InputDevice {
public:
    KeyboardInput() : InputDevice(InputType::KEYBOARD) {}
    
    ControlCommand getCommand() override;
    std::string getDeviceName() const override { return "Keyboard"; }
};

// 手柄输入设备
class GamepadInput : public InputDevice {
public:
    GamepadInput() : InputDevice(InputType::GAMEPAD) {}
    
    ControlCommand getCommand() override;
    std::string getDeviceName() const override { return "Gamepad"; }
};

// 语音输入设备
class VoiceInput : public InputDevice {
public:
    VoiceInput() : InputDevice(InputType::VOICE) {}
    
    ControlCommand getCommand() override;
    std::string getDeviceName() const override { return "Voice Control"; }
};

#endif // ROBOT_FSM_H
    