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

#include "imgui.h"
#include "ssup.h"

using namespace std;


void renderPageEditor(){
    //static char buffer[4][22];
    static uint8_t drawBuffer[32][16];
    
    static char label[64];
    static bool drawMode = false;
    
    //::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
    
    //ImGui::Begin("Page Editor", NULL,
    //             ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize | (drawMode ? ImGuiWindowFlags_NoMove : 0));
    
    if (ImGui::BeginTabItem(appLang == LANG::ENGLISH ? "Page Editor" : "Seiten Editor")){
        
        ImGui::BeginDisabled(currentPage <= 0);
        if (ImGui::ArrowButton("###PREVPAGE", ImGuiDir_Left)) currentPage--;
        ImGui::EndDisabled();
        
        ImGui::SameLine();
        ImGui::Text(appLang == LANG::ENGLISH ? "Page %d" : "Seite %d", currentPage);
        ImGui::SameLine();
        
        ImGui::BeginDisabled(currentPage >= (int)pages.size() - 1);
        if (ImGui::ArrowButton("###NEXTPAGE", ImGuiDir_Right)) currentPage++; 
        ImGui::EndDisabled();
        
        if (pages.size() == 0){
            ImGui::SeparatorText("###emptyText");
            ImGui::PushStyleColor(ImGuiCol_Text, 0xFF2222FF);
            ImGui::Text(appLang == LANG::ENGLISH ? 
                "Empty page table!\nPlease add a text or image page to edit it here.\nIf you're stuck, read the guide:" : 
                "Keine Seiten!\nBitte füge Text oder Bild hinzu um diese hier zu bearbeiten\nWenn du bilfe brauchst, ließ die Anleitung:");
            ImGui::PopStyleColor();
            ImGui::TextLinkOpenURL(appLang == LANG::ENGLISH ? "here" : "hier", "https://weingardt.dev/ssup");
            
        }
        else if (pages[currentPage]->type == PAGETYPE::TEXT){
            char** buffer = (char**)pages[currentPage]->data;
            bool focus = false;
            
            sprintf(label, "%dx%d Text", config.textx, config.texty);
            ImGui::SeparatorText(label);
            
            ImGui::PushStyleVarY(ImGuiStyleVar_ItemSpacing, 1);
            for (int i = 0; i < config.texty; i++){
                int length = config.textx - (int)strlen(buffer[i]);
                sprintf(label, "###TEXTEDIT%d", i);
                
                bool error = false;
                
                for (int j = 0; j < (int)strlen(buffer[i]); j++) if (!isascii(buffer[i][j])) error = true;
                
                if (error) ImGui::PushStyleColor(ImGuiCol_FrameBg, 0xFF0000FF);
                ImGui::PushItemFlag(ImGuiTabItemFlags_Leading, focus);
                
                ImGui::PushItemWidth(ImGui::CalcTextSize("a").x * (config.textx + 1));
                if (ImGui::InputText(label, buffer[i], config.textx + 1, ImGuiInputTextFlags_EnterReturnsTrue)) focus = true;
                
                ImGui::PopItemFlag();
                
                ImGui::SameLine();
                
                if (error) {
                    ImGui::PopStyleColor();
                    ImGui::Text(appLang == LANG::ENGLISH ? "Illegal Character!" : "Ungültiger Buchstabe!");
                } else ImGui::Text(appLang == LANG::ENGLISH ? "%d char%s left" : "%d Buchstabe%s übrig", length, length == 1 ? "" : (appLang == LANG::ENGLISH ? "s" : "n"));
            }
            ImGui::PopStyleVar();
            
            drawMode = false;
        } else {
            
            // REWORK!
            sprintf(label, "%dx%d %s", config.imagex, config.imagey, appLang == LANG::ENGLISH ? "Image" : "Bild");
            ImGui::SeparatorText(label);
            const int SIZE = 4;
            
            ImVec2 cursor = ImGui::GetCursorScreenPos();
            ImVec2 size = ImVec2(128*SIZE, 32*SIZE);
            ImVec2 mouse = ImGui::GetMousePos();
            
            ImGui::Dummy(size);
            ImDrawList* list = ImGui::GetWindowDrawList();
            
            list->AddRectFilled(cursor, ImVec2(size.x + cursor.x, size.y + cursor.y), 0xFFFFFFFF);
            list->AddRect(cursor, ImVec2(size.x + cursor.x, size.y + cursor.y), ImGui::GetColorU32(ImGui::GetStyleColorVec4(ImGuiCol_Border)));
            
            for (int y = 0; y < 32; y++){
                for (int x = 0; x < 128; x++){
                    
                    if (((drawBuffer[y][x / 16] >> (x & 0b111)) & 1) == 1){
                        list->AddRectFilled(ImVec2(cursor.x + (x * SIZE), cursor.y + (y * SIZE)),
                                            ImVec2(cursor.x + (x * SIZE) + SIZE, cursor.y + (y * SIZE) + SIZE), 0xFF000000);
                    }
                }
            }
            
            ImVec2 mousePreview = ImVec2((int)((((int)mouse.x) / SIZE) * SIZE), (int)((((int)mouse.y) / 4) * 4));
            if (mousePreview.x > cursor.x  && mousePreview.x < size.x + cursor.x &&
                mousePreview.y > cursor.y && mousePreview.y < size.y + cursor.y){
                
                list->AddRectFilled(mousePreview, ImVec2(mousePreview.x + SIZE, mousePreview.y + SIZE) , 0x55000000);
            //SDL_ShowCursor(false);
            drawMode = true;
                } else {
                    //SDL_ShowCursor(true);
                    drawMode = false;
                }
                //list->PushClipRect(cursor, ImVec2(size.x + cursor.x, size.y + cursor.y), true);
        }
    
        ImGui::EndTabItem();
        //ImGui::End();
    }
}
