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

#include "imgui.h"

#include "serialib.h"
#include "ssup.h"

using namespace std;

void renderActions(){

    static string deleteString;
    ImVec2 parentWidth = ImVec2(ImGui::GetWindowWidth() - 16, 0);
    
    ImGui::BeginDisabled(!connected);
    
    ImGui::SeparatorText(appLang == LANG::ENGLISH ? "Erasing" : "Löschen");
    
    if (ImGui::Button(appLang == LANG::ENGLISH ? "Erase TOC (fastest)" : "TOC löschen (schnell)", parentWidth)) {
        deleteString = "f";
        ImGui::OpenPopup("Erase?");
    }
    ImGui::SetItemTooltip("Erase Filesystem structure -> fast -> could lead to \"ghost content\"");


    if (ImGui::Button(appLang == LANG::ENGLISH ? "Erase everything (slowest)" : "Alles löschen (langsam)", parentWidth)) {
        deleteString = "c";
        ImGui::OpenPopup("Erase?");
    }
    ImGui::SetItemTooltip("Overwrite the storage -> slower -> safer");

    ImVec2 center = ImGui::GetMainViewport()->GetCenter();
    ImGui::SetNextWindowPos(center, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
    if (ImGui::BeginPopupModal("Erase?", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
        ImGui::Text(appLang == LANG::ENGLISH ? "Erasing will result in permanent data loss!" : "Löschen kann nicht rückgängig gemacht werden!");
        ImGui::Separator();
        
        ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
        ImGui::PopStyleVar();
        
        if (ImGui::Button("OK", ImVec2(120, 0))) { 
            serial.writeString(deleteString.c_str());
            ImGui::CloseCurrentPopup(); 
        }
        ImGui::SetItemDefaultFocus();
        ImGui::SameLine();
        if (ImGui::Button(appLang == LANG::ENGLISH ? "Cancel" : "Abbrechen", ImVec2(120, 0))) ImGui::CloseCurrentPopup();
        ImGui::EndPopup();
    }
    
    ImGui::SeparatorText("");

    if (ImGui::Button(appLang == LANG::ENGLISH ? "Restore Settings" : "Einstellungen wiederherstellen", parentWidth)) serial.writeString("rg");
    ImGui::SetItemTooltip("Set all the settings back to their original value.\nThis operation does not erase content.");
    
    ImGui::SeparatorText("");

    ImGui::Button("Download SSUP Content", parentWidth);
    ImGui::SetItemTooltip("Download the content from SSUP to a JSON file.\nOne can reproduce and edit it's content after.");

    ImGui::Button("Upload to SSUP", parentWidth);
    ImGui::SetItemTooltip("Upload the content in this program to SSUP. This could take a couple of minutes");
    
    
    ImGui::EndDisabled();
}
