#include "module_scisat1_instruments.h"
#include "common/ccsds/ccsds_tm/demuxer.h"
#include "common/ccsds/ccsds_tm/vcdu.h"
#include "common/utils.h"
#include "image/io.h"
#include "imgui/imgui.h"
#include "logger.h"
#include <cstdint>
#include <filesystem>
#include <fstream>

namespace scisat1
{
    namespace instruments
    {
        SciSat1InstrumentsDecoderModule::SciSat1InstrumentsDecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
            : satdump::pipeline::base::FileStreamToFileStreamModule(input_file, output_file_hint, parameters)
        {
            fsfsm_enable_output = false;
        }

        void SciSat1InstrumentsDecoderModule::process()
        {
            uint8_t cadu[1199];

            // Demuxers
            ccsds::ccsds_tm::Demuxer demuxer_vcid0(1016, false, 2, 7);
            ccsds::ccsds_tm::Demuxer demuxer_vcid1(1016, false, 2, 7);
            ccsds::ccsds_tm::Demuxer demuxer_vcid4(1016, false, 2, 7);

            // std::ofstream output("file.ccsds");

            // Products dataset
            // satdump::ProductDataSet dataset;
            // dataset.satellite_name = "PROBA-1";
            // dataset.timestamp = time(0); // avg_overflowless(avhrr_reader.timestamps);

            while (should_run())
            {
                // Read buffer
                read_data((uint8_t *)&cadu, 1199);

                // Parse this transport frame
                ccsds::ccsds_tm::VCDU vcdu = ccsds::ccsds_tm::parseVCDU(cadu);

                // logger->info(pkt.header.apid);
                // logger->info(vcdu.vcid);

                if (vcdu.vcid == 0) // FTS
                {
                    std::vector<ccsds::CCSDSPacket> ccsdsFrames = demuxer_vcid1.work(cadu);
                    for (ccsds::CCSDSPacket &pkt : ccsdsFrames)
                        if (pkt.header.apid == 101) // FTS I/Q Samples
                            fts_reader.work(pkt);
                }
                else if (vcdu.vcid == 1) // MAESTRO? Doesn't seem to be VNIRI spec-wise
                {
                    std::vector<ccsds::CCSDSPacket> ccsdsFrames = demuxer_vcid4.work(cadu);
                    for (ccsds::CCSDSPacket &pkt : ccsdsFrames)
                        if (pkt.header.apid == 102) // MAESTRO Spectrometer data? Doesn't seem to be VNIRI spec-wise
                            maestro_reader.work(pkt);
                }
                else if (vcdu.vcid == 4) // Mainly HK it seems like
                {
                    std::vector<ccsds::CCSDSPacket> ccsdsFrames = demuxer_vcid4.work(cadu);
                    // for (ccsds::CCSDSPacket &pkt : ccsdsFrames)
                    //{
                    // }
                }
            }

            cleanup();

            // FTS
            {
                fts_status = SAVING;

                logger->info("----------- FTS");
                logger->info("Lines : %d", fts_reader.lines);

                std::string fts_directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/FTS";

                if (!std::filesystem::exists(fts_directory))
                    std::filesystem::create_directory(fts_directory);

                auto img = fts_reader.getImg();
                image::save_img(img, fts_directory + "/FTS");

                img.resize_bilinear(img.width() / 10, img.height() * 10);
                image::save_img(img, fts_directory + "/FTS_scaled");

                fts_status = DONE;
            }

            // MAESTRO
            {
                maestro_status = SAVING;

                logger->info("----------- MAESTRO");
                logger->info("Lines (1) : %d", maestro_reader.lines_1);
                logger->info("Lines (2) : %d", maestro_reader.lines_2);

                std::string maestro_directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/MAESTRO";

                if (!std::filesystem::exists(maestro_directory))
                    std::filesystem::create_directory(maestro_directory);

                auto img = maestro_reader.getImg1();
                image::save_img(img, maestro_directory + "/MAESTRO_1");
                img = maestro_reader.getImg2();
                image::save_img(img, maestro_directory + "/MAESTRO_2");

                maestro_status = DONE;
            }
        }

        void SciSat1InstrumentsDecoderModule::drawUI(bool window)
        {
            ImGui::Begin("SciSat-1 Instruments Decoder", NULL, window ? 0 : NOWINDOW_FLAGS);

            if (ImGui::BeginTable("##scisat1instrumentstable", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
            {
                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);
                ImGui::Text("Instrument");
                ImGui::TableSetColumnIndex(1);
                ImGui::Text("Lines / Frames");
                ImGui::TableSetColumnIndex(2);
                ImGui::Text("Status");

                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);
                ImGui::Text("FTS");
                ImGui::TableSetColumnIndex(1);
                ImGui::TextColored(style::theme.green, "%d", fts_reader.lines);
                ImGui::TableSetColumnIndex(2);
                drawStatus(fts_status);

                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);
                ImGui::Text("MAESTRO Mode 1");
                ImGui::TableSetColumnIndex(1);
                ImGui::TextColored(style::theme.green, "%d", maestro_reader.lines_1);
                ImGui::TableSetColumnIndex(2);
                drawStatus(maestro_status);

                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);
                ImGui::Text("MAESTRO Mode 2");
                ImGui::TableSetColumnIndex(1);
                ImGui::TextColored(style::theme.green, "%d", maestro_reader.lines_2);
                ImGui::TableSetColumnIndex(2);
                drawStatus(maestro_status);

                ImGui::EndTable();
            }

            drawProgressBar();

            ImGui::End();
        }

        std::string SciSat1InstrumentsDecoderModule::getID() { return "scisat1_instruments"; }

        std::shared_ptr<satdump::pipeline::ProcessingModule> SciSat1InstrumentsDecoderModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
        {
            return std::make_shared<SciSat1InstrumentsDecoderModule>(input_file, output_file_hint, parameters);
        }
    } // namespace instruments
} // namespace scisat1