#include <imgui.h>
#include <imgui_internal.h>
#include <time.h>
#include <stdio.h>
#include <stdint.h>

#include "inter.h"
#include "inter-bold.h"


extern bool should_exit;
extern bool show_demo_window;

//standard year
static int year = 2025;

static int current_year = 2025;
static int current_month = 1;
static int current_day = 1;

//fonts
static ImFont *inter_regular;
static ImFont *inter_bold;

int get_current_year(){
    time_t t = time(NULL);           
    struct tm tm = *localtime(&t);   

    current_year  = tm.tm_year + 1900;
    current_month = tm.tm_mon + 1;
    current_day   = tm.tm_mday;
     
    // tm_year is years since 1900
    return year = current_year;
}

bool is_leap_year(int year) {
    return  ((year % 4) == 0 && (year % 100) != 0) || year % 400 == 0;
}

int weekday_from_day(int year, int month, int day) {
    int a = (14 - month) / 12;
    int y = year - a;
    int m = month + (12 * a) - 2;
    return (day + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m) / 12) - 1) % 7;
}

int calendar_week_from_day(int year, int month, int day){
    int days_sum[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
    int days = days_sum[month - 1];
    if(is_leap_year(year) && month > 2)
        days++;
    
    return (10 + days + day - weekday_from_day(year, month, day)) / 7;
}

int days_per_month(int year, int month){
    int days_in_month[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    if (month == 2) {
        return is_leap_year(year) ? 29 : 28;
    }
    return days_in_month[month - 1];
}

void init(){
    ImGuiIO &io = ImGui::GetIO();
    inter_regular = io.Fonts->AddFontFromMemoryCompressedTTF(inter_compressed_data, inter_compressed_size);
    inter_bold    = io.Fonts->AddFontFromMemoryCompressedTTF(inter_bold_compressed_data, inter_bold_compressed_size);
    year = get_current_year();
}

static const char *savefile_path = "./calendar";
static const char *wiki_url = "https://gitea.ammerhai.com/Ammerhai/work-calendar/wiki";

static const char *month_names[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};

typedef struct Category {
    bool editing;
    ImColor color;
    char name[64] = "New Category";
} Category;

static uint32_t num_categories = 0;
static Category categories[128];

static bool legend_visible = true;
static int selected_category = -1;

typedef struct Categorized_Day {
    int year;
    int month;
    int day;
    
    int category;
} Categorized_Day;

static int year_min_size = 1;
static uint32_t num_categorized_days = 0;
static Categorized_Day categorized_days[365*50];

//Colors
ImVec4 table_white_bg = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
ImVec4 table_saturday_bg = ImVec4(0.0f, 0.0f, 0.0f, 0.1f);
ImVec4 table_sunday_bg = ImVec4(0.0f, 0.0f, 0.0f, 0.2f);

ImVec4 table_hover_color = ImVec4(0.0f, 0.3f, 0.6f, 0.2f);

void save(){
    uint32_t version = 1;
    FILE *save_file = fopen(savefile_path, "wb");
    fwrite(&version,              sizeof(version),              1, save_file);
    fwrite(&num_categories,       sizeof(num_categories),       1, save_file);
    fwrite(&num_categorized_days, sizeof(num_categorized_days), 1, save_file);

    for (int i = 0; i < num_categories; i++) {
        fwrite(&categories[i].color, sizeof(categories[i].color), 1, save_file);
        fwrite(&categories[i].name,  sizeof(categories[i].name),  1, save_file);
    }

    for (int i = 0; i < num_categorized_days; i++) { 
        fwrite(&categorized_days[i].year,     sizeof(categorized_days[i].year),     1, save_file);
        fwrite(&categorized_days[i].month,    sizeof(categorized_days[i].month),    1, save_file);
        fwrite(&categorized_days[i].day,      sizeof(categorized_days[i].day),      1, save_file);
        fwrite(&categorized_days[i].category, sizeof(categorized_days[i].category), 1, save_file);
    }

    fclose (save_file);
}

void load(){
    FILE *save_file = fopen(savefile_path, "rb");
    uint32_t version = 0;
    fread(&version, sizeof(version), 1, save_file);
    if(version != 1) {
        return; //TODO Popup warning
    }

    fread(&num_categories,       sizeof(num_categories),       1, save_file);
    fread(&num_categorized_days, sizeof(num_categorized_days), 1, save_file);

    if(num_categories > IM_ARRAYSIZE(categories) || num_categorized_days > IM_ARRAYSIZE(categorized_days)){
        return; //TODO Popup warning
    }

    for (int i = 0; i < num_categories; i++) {
        fread(&categories[i].color, sizeof(categories[i].color), 1, save_file);
        fread(&categories[i].name,  sizeof(categories[i].name),  1, save_file);
    }

    for (int i = 0; i < num_categorized_days; i++) { 
        fread(&categorized_days[i].year,     sizeof(categorized_days[i].year),     1, save_file);
        fread(&categorized_days[i].month,    sizeof(categorized_days[i].month),    1, save_file);
        fread(&categorized_days[i].day,      sizeof(categorized_days[i].day),      1, save_file);
        fread(&categorized_days[i].category, sizeof(categorized_days[i].category), 1, save_file);
    }

    fclose (save_file);
}

void per_frame(){
    ImGuiStyle &style = ImGui::GetStyle();

    year_min_size = ImGui::CalcTextSize("8888").x + 5;

    ImGuiID main_viewport_dock = ImGui::GetID("main_viewport_dock");
    if (!ImGui::DockBuilderGetNode(main_viewport_dock)) {
        ImGui::DockBuilderAddNode    (main_viewport_dock, ImGuiDockNodeFlags_DockSpace | ImGuiDockNodeFlags_NoTabBar);
        ImGui::DockBuilderSetNodePos (main_viewport_dock, ImGui::GetMainViewport()->WorkPos);
        ImGui::DockBuilderSetNodeSize(main_viewport_dock, ImGui::GetMainViewport()->WorkSize);

        ImGuiID left_dock = 0;
        ImGuiID right_dock = ImGui::DockBuilderSplitNode(main_viewport_dock, ImGuiDir_Right, 0.2f, NULL, &left_dock);
        ImGui::DockBuilderDockWindow("Work Calendar", left_dock);
        ImGui::DockBuilderDockWindow("Legende", right_dock);
        ImGui::DockBuilderFinish(main_viewport_dock);
    }
    ImGui::DockSpaceOverViewport(main_viewport_dock, ImGui::GetMainViewport(), ImGuiDockNodeFlags_NoTabBar);

    ImGuiID close_popup  = ImGui::GetID("Close");
    ImGuiID open_popup   = ImGui::GetID("Open");
    ImGuiID save_popup   = ImGui::GetID("Save");
    ImGuiID logout_popup = ImGui::GetID("Logout"); //TODO
    ImGuiID about_popup  = ImGui::GetID("About");

    if (ImGui::BeginMainMenuBar()) {
        if (ImGui::BeginMenu("Datei")) {
            if (ImGui::MenuItem("Kalender öffnen", NULL)) {
                //TODO popup + filepicker
                load();
            }
            if (ImGui::MenuItem("Kalender speichern", NULL)) {
                save();
            }

            ImGui::Separator();
            ImGui::MenuItem("Abmelden", NULL);

            ImGui::Separator();
            if (ImGui::MenuItem("Beenden", NULL)) {
                ImGui::OpenPopup(close_popup);
            }
            ImGui::EndMenu();
        }

        if (ImGui::BeginMenu("Ansicht")) {
            ImGui::MenuItem("Legende", NULL, &legend_visible);
            ImGui::EndMenu();
        }

        if (ImGui::BeginMenu("Hilfe")) {
            ImGui::MenuItem("Demo", NULL, &show_demo_window);
            if (ImGui::MenuItem("Wiki", NULL)) {
                ImGui::GetPlatformIO().Platform_OpenInShellFn(ImGui::GetCurrentContext(), wiki_url);
            }
            ImGui::Separator();
            ImGui::MenuItem("Über", NULL);
            ImGui::EndMenu();
        }

        ImGui::EndMainMenuBar();
    }

    ImVec2 center = ImGui::GetMainViewport()->GetCenter();
    ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
    if (ImGui::BeginPopupModal("Close", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize)) {
        ImGui::Text("Möchten Sie wirklich beenden?");

        ImGui::SetItemDefaultFocus();
        if (ImGui::Button("Ja", ImVec2(120, 0))) { 
            should_exit = true;
            ImGui::CloseCurrentPopup(); 
        }

        ImGui::SameLine();
        if (ImGui::Button("Nein", ImVec2(120, 0))) { 
            ImGui::CloseCurrentPopup(); 
        }
        
        ImGui::EndPopup();
    }

    if (ImGui::Begin("Work Calendar", 0)) {
        ImGui::PushStyleColor(ImGuiCol_TableHeaderBg, {0,0,0,0});
        ImGui::PushStyleColor(ImGuiCol_HeaderHovered, {0,0,0,0});
        ImGui::PushStyleColor(ImGuiCol_HeaderActive, {0,0,0,0});

        if(ImGui::BeginTable("CalendarHeader", 5)) {
            
            ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch);    // Left side
            ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed);      // Prev year
            ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, year_min_size);      // Year
            ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed);      // Next year
            ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch);    // Right side
            
            ImGui::TableNextRow();

            ImGui::TableSetColumnIndex(0);
            ImGui::TableSetColumnIndex(1);
            if (ImGui::ArrowButton("##left", ImGuiDir_Left)) 
                year--;

            ImGui::TableSetColumnIndex(2);
            ImGui::TextAligned(0.5, -FLT_MIN, "%d", year);  

            ImGui::TableSetColumnIndex(3);
            if (ImGui::ArrowButton("##right", ImGuiDir_Right))  
                year++; 
        
            ImGui::TableSetColumnIndex(4);
            ImGui::EndTable();
        }

        for (int month = 1; month <= 12; month++) {
            ImGui::PushID(month);
            ImGui::PushStyleColor(ImGuiCol_TableBorderStrong, {0,0,0,0});
            ImGui::PushStyleColor(ImGuiCol_TableBorderLight, {0,0,0,0});

            if(ImGui::BeginTable("Month", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX)) {
                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);
                ImGui::TextAligned(0.5, -FLT_MIN, month_names[month - 1]);

                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);

                //calendar week table
                if(ImGui::BeginTable("CalendarWeek", 1, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){
                    ImGui::TableNextRow();
                    ImGui::TableSetColumnIndex(0);
                    ImGui::TextAligned(0.5, -FLT_MIN, "KW");

                    for (int day = 1; day <= days_per_month(year, month); day++) {
                        int weekday = weekday_from_day(year, month, day);

                        if(weekday == 0 || day == 1){
                            ImGui::TableNextRow();
                            ImGui::TableSetColumnIndex(0);

                            //if first week has 3 or less days, last week from previous year
                            int calender_week = calendar_week_from_day(year, month, day);
                            if(calender_week == 0) {
                                ImGui::TextAligned(0.5, -FLT_MIN, "%d", 53);
                            } else {
                                ImGui::TextAligned(0.5, -FLT_MIN, "%d", calender_week);
                            }
                        }
                    }

                    ImGui::EndTable();
                }
                ImGui::SameLine(0, 0);

                ImGui::BeginGroup();
                ImGui::PushStyleVarY(ImGuiStyleVar_ItemSpacing, 0);

                if(ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)){

                    ImGui::TableSetupColumn("Mo");
                    ImGui::TableSetupColumn("Di");
                    ImGui::TableSetupColumn("Mi");
                    ImGui::TableSetupColumn("Do");
                    ImGui::TableSetupColumn("Fr");
                    ImGui::TableSetupColumn("Sa");
                    ImGui::TableSetupColumn("So");
                    ImGui::TableHeadersRow();

                    ImGui::EndTable();
                }
                ImGui::PopStyleColor(2);

                ImGui::PushStyleColor(ImGuiCol_TableBorderStrong, {119/255.0f,119/255.0f,119/255.0f,83/255.0f});
                ImGui::PushStyleColor(ImGuiCol_TableBorderLight, {119/255.0f,119/255.0f,119/255.0f,83/255.0f});

                //days of month table
                if(ImGui::BeginTable("Weekdays", 7, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_Borders)) {
                    int offset = weekday_from_day(year, month, 1);

                    for (int day = 1; day <= days_per_month(2025, month); day++) {
                        int weekday = weekday_from_day(year, month, day);

                        if(weekday == 0 || day == 1){
                            ImGui::TableNextRow();
                        }

                        ImGui::TableSetColumnIndex(weekday);
                        ImU32 cell_bg_color_workweek = ImGui::GetColorU32(table_white_bg);
                        ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_workweek);

                        //weekend coloring
                        if(weekday == 5){
                            ImU32 cell_bg_color_sat = ImGui::GetColorU32(table_saturday_bg);
                            ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sat);
                        }

                        if(weekday == 6){
                            ImU32 cell_bg_color_sun = ImGui::GetColorU32(table_sunday_bg);
                            ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color_sun);
                        }

                        //color field with category 
                        for (int i = 0; i < num_categorized_days; i++) {
                            if (categorized_days[i].year  != year)  continue;
                            if (categorized_days[i].month != month) continue;
                            if (categorized_days[i].day   != day)   continue;
                            
                            ImColor color = categories[categorized_days[i].category].color;
                            color.Value.w = 0.5;

                            //sat + sun: add alpha value
                            if(weekday == 5)
                                color.Value.w = color.Value.w  + table_saturday_bg.w;
                            if(weekday == 6)
                                color.Value.w = color.Value.w  + table_sunday_bg.w;

                            ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ImU32(color));
                        }
                        
                        if(current_year == year && current_month == month && current_day == day ) {
                            ImGui::PushFont(inter_bold);
                            ImGui::TextAligned(0.5, -FLT_MIN, "%d", day);
                            ImGui::PopFont();
                        } else {
                            ImGui::TextAligned(0.5, -FLT_MIN, "%d", day);
                        }

                        if(ImGui::IsItemHovered()){
                            if(selected_category != -1) {
                                ImColor hover_color = categories[selected_category].color;
                                hover_color.Value.w = 0.2;
                                ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ImGui::GetColorU32(ImU32(hover_color)));
                            } else {
                                ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ImGui::GetColorU32(table_hover_color));
                            }
                        }

                        //apply selected color to field
                        if (selected_category != -1 && ImGui::IsItemClicked()) {
                            for (int i = 0; i < num_categorized_days; i++) {
                                if (categorized_days[i].year  != year)  continue;
                                if (categorized_days[i].month != month) continue;
                                if (categorized_days[i].day   != day)   continue;

                                if (categorized_days[i].category == selected_category) {
                                    categorized_days[i] = categorized_days[num_categorized_days-1];
                                    num_categorized_days--;
                                } else {
                                    categorized_days[i] = {
                                        .year = year,
                                        .month = month,
                                        .day = day,

                                        .category = selected_category,
                                    };
                                    num_categorized_days++;
                                }

                                goto color_changed;
                            }

                            categorized_days[num_categorized_days] = {
                                .year = year,
                                .month = month,
                                .day = day,

                                .category = selected_category,
                            };
                            num_categorized_days++;
                        }
                        color_changed:;
                    }

                    ImGui::EndTable();
                }
                ImGui::PopStyleColor(2);
                ImGui::PopStyleVar();
                //weekday header table + days of month table
                ImGui::EndGroup();
                //month table
                ImGui::EndTable();
            }

            if ((month % 4) != 0)
                ImGui::SameLine();

            ImGui::PopID();
        }
        
        ImGui::PopStyleColor(3);
    }
    ImGui::End();
    
    // Legende
    if (legend_visible && ImGui::Begin("Legende", 0)) {
        if(ImGui::BeginTable("Legend", 4, ImGuiTableFlags_SizingStretchProp)) {
            ImGui::TableSetupColumn("Color",  ImGuiTableColumnFlags_WidthFixed);
            ImGui::TableSetupColumn("Name",   ImGuiTableColumnFlags_WidthStretch);
            ImGui::TableSetupColumn("Edit",   ImGuiTableColumnFlags_WidthFixed);
            ImGui::TableSetupColumn("Delete", ImGuiTableColumnFlags_WidthFixed );

            for (size_t i = 0; i < num_categories; i++) {
                auto &category = categories[i];
                ImGui::PushID(i);
                ImGui::TableNextRow();

                ImGui::TableSetColumnIndex(0);
                if (ImGui::Selectable("##Category", selected_category == i, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap, { 0, style.FramePadding.y * 2 + ImGui::CalcTextSize("M").y })) {
                    selected_category = selected_category == i ? -1 : i;
                }
                ImGui::SameLine();
                ImGui::ColorEdit3(category.name, &category.color.Value.x, ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoInputs);

                ImGui::TableSetColumnIndex(1);
                if (category.editing) {
                    ImGui::SetNextItemWidth(-1);
                    if(ImGui::InputText("##name", category.name, IM_ARRAYSIZE(category.name), ImGuiInputTextFlags_EnterReturnsTrue)) {
                        category.editing = false;
                    }
                } else {
                    ImGui::Text("%s", category.name);
                }

                ImGui::TableSetColumnIndex(2);
                if (ImGui::Button("e"))
                    category.editing = !category.editing;

                ImGui::TableSetColumnIndex(3);
                if (ImGui::Button("d")) {
                    for (size_t j = 0; j < num_categorized_days; j++) {
                        // Remove categorized days that have the to be deleted category
                        if (categorized_days[j].category == i) {
                            for (size_t k = j; k < num_categorized_days - 1; k++) categorized_days[k] = categorized_days[k + 1];
                            num_categorized_days--;
                            j--;
                        }
                        // Decrement the category index of all categorized days that have an index higher than the one to be removed
                        if (categorized_days[j].category > i) categorized_days[j].category--;
                    }
                    // Remove the category
                    for (size_t j = i; j < num_categories - 1; j++) categories[j] = categories[j + 1];
                    num_categories--;
                }

                ImGui::PopID();
            }

            ImGui::TableNextRow();
            ImGui::TableSetColumnIndex(0);
            if (ImGui::Button("+")) {
                categories[num_categories] = {};
                num_categories++;
            }

            ImGui::EndTable();
        }
    }
    if (legend_visible)
        ImGui::End();
}

