#include "module_gvar_decoder.h"
#include "common/codings/differential/nrzs.h"
#include "gvar_deframer.h"
#include "gvar_derand.h"
#include "imgui/imgui.h"
#include "logger.h"

#define BUFFER_SIZE 8192

// Return filesize
uint64_t getFilesize(std::string filepath);

namespace goes
{
    namespace gvar
    {
        GVARDecoderModule::GVARDecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
            : satdump::pipeline::base::FileStreamToFileStreamModule(input_file, output_file_hint, parameters)
        {
            buffer = new int8_t[BUFFER_SIZE];
            fsfsm_file_ext = ".gvar";
        }

        GVARDecoderModule::~GVARDecoderModule() { delete[] buffer; }

        void GVARDecoderModule::process()
        {
            diff::NRZSDiff diff;

            // The deframer
            GVARDeframer deframer;

            // Derand
            PNDerandomizer derand;

            // Final buffer after decoding
            uint8_t finalBuffer[BUFFER_SIZE * 2];

            // Bits => Bytes stuff
            uint8_t byteShifter = 0;
            int inByteShifter = 0;
            int byteShifted = 0;

            while (should_run())
            {
                // Read a buffer
                read_data((uint8_t *)buffer, BUFFER_SIZE);

                // Group symbols into bytes now, I channel
                inByteShifter = 0;
                byteShifted = 0;

                for (int i = 0; i < BUFFER_SIZE; i++)
                {
                    byteShifter = byteShifter << 1 | (buffer[i] > 0);
                    inByteShifter++;

                    if (inByteShifter == 8)
                    {
                        finalBuffer[byteShifted++] = byteShifter;
                        inByteShifter = 0;
                    }
                }

                // Differential decoding for both of them
                diff.decode(finalBuffer, BUFFER_SIZE / 8);

                // Deframe
                std::vector<std::vector<uint8_t>> frameBuffer = deframer.work(finalBuffer, BUFFER_SIZE / 8);

                // If we found frames, write them out
                if (frameBuffer.size() > 0)
                {
                    for (std::vector<uint8_t> &frame : frameBuffer)
                    {
                        derand.derandData(frame.data(), 32786);

                        write_data(frame.data(), 32786);
                    }
                }
            }

            cleanup();
        }

        void GVARDecoderModule::drawUI(bool window)
        {
            ImGui::Begin("GVAR Decoder", NULL, window ? 0 : NOWINDOW_FLAGS);

            ImGui::BeginGroup();
            {
                // Constellation
                {
                    ImDrawList *draw_list = ImGui::GetWindowDrawList();
                    ImVec2 rect_min = ImGui::GetCursorScreenPos();
                    ImVec2 rect_max = {rect_min.x + 200 * ui_scale, rect_min.y + 200 * ui_scale};
                    draw_list->AddRectFilled(rect_min, rect_max, style::theme.widget_bg);
                    draw_list->PushClipRect(rect_min, rect_max);

                    for (int i = 0; i < 2048; i++)
                    {
                        draw_list->AddCircleFilled(ImVec2(ImGui::GetCursorScreenPos().x + (int)(100 * ui_scale + (buffer[i] / 127.0) * 130 * ui_scale) % int(200 * ui_scale),
                                                          ImGui::GetCursorScreenPos().y + (int)(100 * ui_scale + rng.gasdev() * 14 * ui_scale) % int(200 * ui_scale)),
                                                   2 * ui_scale, style::theme.constellation);
                    }

                    draw_list->PopClipRect();
                    ImGui::Dummy(ImVec2(200 * ui_scale + 3, 200 * ui_scale + 3));
                }
            }
            ImGui::EndGroup();

            drawProgressBar();

            ImGui::End();
        }

        std::string GVARDecoderModule::getID() { return "goes_gvar_decoder"; }

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