#include <SDL2/SDL.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <bits/stdc++.h>

#include "imgui.h"

#include "ssup.h"

using namespace std;

void addPage(PAGETYPE type){
    page* pg = new page();
    pg->type = type;
    
    if (type == PAGETYPE::IMAGE){
        pg->data = malloc((((config.imagex / 8) * config.imagey)*sizeof(uint8_t)));
        
    } else {
        pg->data = malloc(config.texty*sizeof(char*));
        char** txt = (char**)pg->data;
        for (int i = 0; i < config.texty; i++) {
            txt[i] = (char*)malloc((config.textx  + 1)*sizeof(char));
            for (int j = 0; j < config.textx + 1; j++) txt[i][j] = 0;
        }
    
    }
    pages.push_back(pg);
}

bool pageGetter(void* data, int index, const char** output) {
    vector<page*>* pages = (vector<page*>*)data;
    
    static char buff[16];
    if (pages->at(index)->type == PAGETYPE::IMAGE) sprintf(buff, appLang == LANG::ENGLISH ? "%d : Image" : "%d : Bild", index);
    else sprintf(buff, "%d : Text", index);
    *output = buff;
    
    return true;
}

void renderPageAllocator(int &currentPage){
    static int usedMemory = 0;
    
    
    ImGui::SetNextWindowSize(ImVec2(250, ImGui::GetMainViewport()->Size.y - 40));
    ImGui::SetNextWindowPos(ImVec2(10, 30), ImGuiCond_Always);
    ImGui::Begin(appLang == LANG::ENGLISH ? "Pages" : "Seiten", NULL, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse);
    
    ImGui::BeginDisabled(usedMemory < (config.textx * config.texty));
    if (ImGui::Button(appLang == LANG::ENGLISH ? "Add Text" : "Text hinzufügen", ImVec2(130, 0))) addPage(PAGETYPE::TEXT);
    ImGui::SameLine();
    ImGui::Text("%d Bytes", (config.textx * config.texty));
    ImGui::EndDisabled();
    
    ImGui::BeginDisabled(usedMemory < ((config.imagex / 8) * config.imagey));
    if (ImGui::Button(appLang == LANG::ENGLISH ? "Add Image" : "Bild hinzufügen", ImVec2(130, 0))) addPage(PAGETYPE::IMAGE);
    ImGui::SameLine();
    ImGui::Text("%d Bytes", ((config.imagex / 8) * config.imagey));
    ImGui::EndDisabled();
    
    ImGui::Separator();
    
    ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - 20);
    ImGui::ListBox("###Pages", &currentPage, pageGetter, &pages, pages.size(), 16);
    
    ImGui::BeginDisabled(currentPage == 0);
    if (ImGui::ArrowButton("###PUSHUP", ImGuiDir_Up)){
        page* swap = pages[currentPage];
        pages[currentPage] = pages[currentPage - 1];
        pages[currentPage - 1] = swap;
        
        currentPage--;
    }
    ImGui::EndDisabled();
    ImGui::BeginDisabled(currentPage >= (int)pages.size() - 1);
    ImGui::SameLine();
    
    if (ImGui::ArrowButton("###PUSHDOWN", ImGuiDir_Down)){
        page* swap = pages[currentPage];
        pages[currentPage] = pages[currentPage + 1];
        pages[currentPage + 1] = swap;
        
        currentPage++;
    }
    ImGui::EndDisabled();

    ImGui::SameLine();
    ImGui::Text(appLang == LANG::ENGLISH ? "Arrange Pages" : "Seiten Sortieren");
    
    ImGui::BeginDisabled(pages.size() <= 0);
    if (ImGui::Button(appLang == LANG::ENGLISH ? "Delete Page" : "Seite löschen")){
        vector<page*>::iterator it = pages.begin();
        advance(it, currentPage);
        pages.erase(it);
    }
    ImGui::EndDisabled();

    if (connected){
        ImGui::Separator();
        
        usedMemory = 0;
        for (uint i = 0; i < pages.size(); i++) usedMemory += ( pages[i]->type == PAGETYPE::IMAGE ? ((config.imagex / 8) * config.imagey) : ((config.textx * config.texty)));
        usedMemory = (config.eeprom_size * 1024) - usedMemory - config.toc_size;
        
        ImGui::Text(appLang == LANG::ENGLISH ? "%d Bytes left\n%d%% Used" : "%d Bytes verbleibend\n%d%% genutzt", 
                    usedMemory, (int)(((float)((config.eeprom_size * 1024) - usedMemory) / (float)(config.eeprom_size * 1024)) * 100));
        
    }
    
    ImGui::End();
}
