#include <cstdio>
#include <string>
#include <vector>
#include <utility> // std::pair
#include <opencv2/opencv.hpp> // all opencv header
#include "hdrplus/hdrplus_pipeline.h"
#include "hdrplus/burst.h"
#include "hdrplus/align.h"
#include "hdrplus/merge.h"
#include "hdrplus/finish.h"
#include <fstream>

#ifdef __ANDROID__
// #include <AndroidHelper.h>
#endif

namespace hdrplus
{

void hdrplus_pipeline::run_pipeline( \
    const std::string& burst_path, \
    const std::string& reference_image_path  )
{
    // Create burst of images
    burst burst_images( burst_path, reference_image_path );

    std::vector<std::vector<std::vector<std::pair<int, int>>>> alignments;

    // Run align
    align_module.process( burst_images, alignments );

    // Run merging
    merge_module.process( burst_images, alignments );


    // Run finishing
    cv::Mat finalImg;
    finish_module.process( burst_images, finalImg);
}

bool hdrplus_pipeline::run_pipeline( \
    const std::vector<std::string>& burst_paths, \
    int reference_image_index, cv::Mat& finalImg  )
{
    // Create burst of images
    burst burst_images( burst_paths, reference_image_index );
    std::vector<std::vector<std::vector<std::pair<int, int>>>> alignments;
#ifdef __ANDROID__
    // ALOGI("Finish loading images");
#endif

    // Run align
    align_module.process( burst_images, alignments );
#ifdef __ANDROID__
    // ALOGI("Finish align");
#endif

    // Run merging
    merge_module.process( burst_images, alignments );
#ifdef __ANDROID__
    // ALOGI("Finish merging");
#endif

    // Run finishing
    finish_module.process( burst_images, finalImg);
#ifdef __ANDROID__
    // ALOGI("Finish process");
#endif

    return true;
}

bool hdrplus_pipeline::run_pipeline( \
    const std::vector<std::vector<uint8_t> >& burst_contents, \
    int reference_image_index, cv::Mat& finalImg  )
{
    // Create burst of images
    burst burst_images( burst_contents, reference_image_index );
    std::vector<std::vector<std::vector<std::pair<int, int>>>> alignments;
#ifdef __ANDROID__
    // ALOGI("Finish loading images");
#endif

    // Run align
    align_module.process( burst_images, alignments );
#ifdef __ANDROID__
    // ALOGI("Finish align");
#endif

    // Run merging
    merge_module.process( burst_images, alignments );
#ifdef __ANDROID__
    // ALOGI("Finish merging");
#endif

    // Run finishing
    finish_module.process( burst_images, finalImg);
#ifdef __ANDROID__
    // ALOGI("Finish process");
#endif

    return true;
}


    bool hdrplus_pipeline::run_pipeline( \
    const std::vector<std::shared_ptr<MemFile> >& burst_files, \
    int reference_image_index, cv::Mat& finalImg  )
    {
        // Create burst of images
        burst burst_images( burst_files, reference_image_index );
        std::vector<std::vector<std::vector<std::pair<int, int>>>> alignments;
#ifdef __ANDROID__
        // ALOGI("Finish loading images");
#endif

        // Run align
        align_module.process( burst_images, alignments );
#ifdef __ANDROID__
        // ALOGI("Finish align");
#endif

        // Run merging
        merge_module.process( burst_images, alignments );
#ifdef __ANDROID__
        // ALOGI("Finish merging");
#endif

        // Run finishing
        finish_module.process( burst_images, finalImg);
#ifdef __ANDROID__
        // ALOGI("Finish process");
#endif

        return true;
    }

} // namespace hdrplus
