#include <cstdio>
#include <cstdlib>
#include <SDL2/SDL.h>
#include <random>
#include <string>
#include <iostream>
#include <bits/stdc++.h>

#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_sdlrenderer2.h"
#include "serialib.h"

#include "ssup.h"

#include "editor.cpp"
#include "connector.cpp"
#include "pages.cpp"
#include "menu.cpp"
#include "help.cpp"

using namespace std;


int main(int argc, char* argv[]){
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0){
        cerr << "Error: " << SDL_GetError() << endl;
        return -1;
    }
    
    SDL_Window* window = SDL_CreateWindow("SpEdit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
    if (window == nullptr) {
        cerr << "Error: SDL_CreateWindow(): " << SDL_GetError() << endl;
        return -1;
    }
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
    if (renderer == nullptr){
        cerr << "Error creating SDL_Renderer!" << endl;
        return -1;
    }

    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad;
    io.IniFilename = NULL;
    io.LogFilename = NULL;
    updateAppStyle();

    ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
    ImGui_ImplSDLRenderer2_Init(renderer);

    addPage(PAGETYPE::TEXT);
    
    bool done = false;
    while (!done){
        SDL_Delay(10);
        SDL_Event event;

        while (SDL_PollEvent(&event)){
            ImGui_ImplSDL2_ProcessEvent(&event);
            if (event.type == SDL_QUIT)
                done = true;
            if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
                done = true;
        }
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) {
            SDL_Delay(10);
            continue;
        }
        
        ImGui_ImplSDLRenderer2_NewFrame();
        ImGui_ImplSDL2_NewFrame();

        ImGui::NewFrame();
        
        renderMenu();
        
        ImGui::SetNextWindowPos(ImVec2(270, 30), ImGuiCond_Always);
        ImGui::SetNextWindowSize(ImVec2(ImGui::GetMainViewport()->Size.x - 280, ImGui::GetMainViewport()->Size.y - 40), ImGuiCond_Always);
        ImGui::Begin("###TABWIN", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse);
        ImGui::BeginTabBar("Tablol");
        
        renderConnectModule();
        renderPageEditor();
        renderHelpMenu();
        
        ImGui::EndTabBar();
        ImGui::End();
        
        renderPageAllocator(currentPage);

        ImGui::Render();

        
        
        SDL_RenderSetScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
        
        ImVec4 bgCol = ImGui::GetStyleColorVec4(ImGuiCol_FrameBg);
        SDL_SetRenderDrawColor(renderer, bgCol.x * 255, bgCol.y * 255, bgCol.z * 255, bgCol.w * 255);
        SDL_RenderClear(renderer);
        ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), renderer);

        SDL_RenderPresent(renderer);
    }

    serial.closeDevice();

    ImGui_ImplSDLRenderer2_Shutdown();
    ImGui_ImplSDL2_Shutdown();
    ImGui::DestroyContext();

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}
