#pragma once

#include "fgc/ICameraSource.h"
#include "fgc/Logger.h"

#include <chrono>

namespace fgc {

// Synthetic camera for development without hardware. On trigger() it generates
// a small RGB gradient frame (varying with time so successive captures differ)
// and delivers it to the frame callback.
class MockCameraSource : public ICameraSource {
public:
    explicit MockCameraSource(int count = 1, uint32_t width = 640, uint32_t height = 480)
        : count_(count), width_(width), height_(height) {}

    void open() override { LOG_INFO << "[mock] camera opened (" << count_ << ")"; }
    void close() override {}
    void start() override { LOG_INFO << "[mock] camera acquisition started"; }
    void stop() override {}

    bool trigger() override {
        if (!callback_) return false;
        LOG_TRACE_CAT(LogCat::Camera) << "TX trigger cam0 (mock)";
        Frame f;
        f.width    = width_;
        f.height   = height_;
        f.channels = 3;
        f.cam_id   = 0;
        f.timestamp_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
                             std::chrono::system_clock::now().time_since_epoch())
                             .count();
        f.data.resize(static_cast<size_t>(width_) * height_ * 3);
        const uint8_t phase = static_cast<uint8_t>(f.timestamp_ms);
        for (uint32_t y = 0; y < height_; ++y) {
            for (uint32_t x = 0; x < width_; ++x) {
                size_t i = (static_cast<size_t>(y) * width_ + x) * 3;
                f.data[i + 0] = static_cast<uint8_t>(x + phase);
                f.data[i + 1] = static_cast<uint8_t>(y + phase);
                f.data[i + 2] = phase;
            }
        }
        LOG_TRACE_CAT(LogCat::Camera)
            << "RX frame cam0 " << width_ << 'x' << height_ << ' ' << f.data.size() << "B (mock)";
        callback_(f);
        return true;
    }

    void setFrameCallback(FrameCallback cb) override { callback_ = std::move(cb); }
    int  cameraCount() const override { return count_; }

private:
    int           count_;
    uint32_t      width_;
    uint32_t      height_;
    FrameCallback callback_;
};

}  // namespace fgc
