#include <iostream>
#include <memory>
#include <stdexcept>
#include <opencv2/imgcodecs.hpp>
#include <stereo/calibration.hpp>
#include <stereo/pipeline.hpp>

int main(int argc, char** argv) {
    if (argc != 4) {
        std::cerr << "usage: camera_consumer APP_ROOT RAW_SBS CALIBRATION\n";
        return 2;
    }
    try {
        auto calibration = std::make_shared<stereo::Calibration>(stereo::Calibration::load(argv[3]));
        auto sbs = cv::imread(argv[2], cv::IMREAD_COLOR);
        if (sbs.empty()) throw std::runtime_error("cannot read input");
        stereo::PipelineOptions options;
        options.ai.applicationRoot = argv[1];
        options.ai.backend = "auto";
        options.ai.profile = "quarter";
        options.snapshotUsage = "offline";
        options.snapshotScale = 0.25;
        stereo::ProcessingPipeline pipeline(calibration, options);
        auto result = pipeline.processSnapshot(sbs, 1, true);
        if (!result.match) throw std::runtime_error("no disparity result");
        std::cout << "disparity: " << result.match->disparity.size() << '\n';
        std::cout << "metric depth: " << !result.match->depthMetres.empty() << '\n';
        return 0;
    } catch (const std::exception& error) {
        std::cerr << error.what() << '\n';
        return 1;
    }
}
