// D:\Dev\Mosis\MosisService\designer\src\testing\action_recorder.h
#pragma once

#include "action_types.h"
#include <chrono>
#include <functional>

namespace mosis::testing {

class ActionRecorder {
public:
    ActionRecorder(int screen_width, int screen_height);
    ~ActionRecorder() = default;

    // Recording control
    void StartRecording(const std::string& initial_screen = "");
    void StopRecording();
    bool IsRecording() const { return m_recording; }

    // Record events (called from input callbacks)
    void RecordMouseDown(int x, int y);
    void RecordMouseUp(int x, int y);
    void RecordMouseMove(int x, int y);
    void RecordButton(const std::string& button);
    void RecordKey(int key_code, bool pressed);

    // Get recorded sequence
    const ActionSequence& GetSequence() const { return m_sequence; }
    ActionSequence& GetSequence() { return m_sequence; }

    // Save/Load
    bool SaveToFile(const std::string& path) const;
    static ActionSequence LoadFromFile(const std::string& path);

    // Configuration
    void SetLongPressThreshold(int ms) { m_long_press_threshold_ms = ms; }
    void SetSwipeThreshold(int pixels) { m_swipe_threshold_pixels = pixels; }

private:
    int64_t GetTimestamp() const;
    void FinalizeCurrentAction();

    ActionSequence m_sequence;
    bool m_recording = false;
    std::chrono::steady_clock::time_point m_start_time;

    // For detecting tap vs swipe vs long press
    bool m_mouse_down = false;
    int m_mouse_down_x = 0;
    int m_mouse_down_y = 0;
    int64_t m_mouse_down_time = 0;
    int m_last_x = 0;
    int m_last_y = 0;

    // Thresholds
    int m_long_press_threshold_ms = 500;
    int m_swipe_threshold_pixels = 20;
};

} // namespace mosis::testing
