//////////////////////////////////////////////////////////////////////////
// TODOS:                                                               //
// [X] Lower the the time text lower below the activity title.          //
// [X] On timeline, hower, display additional activity chunk info.      //
// [X] Display distribution timeline below the activity line.           //
// [ ] Better file save error handling.                                 //
// [X] Make Linux and MacOS libraries for RayLib; store locally.        //
//////////////////////////////////////////////////////////////////////////


#include "base_context_cracking.h"
#include "base_core.h"

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

#include "opensans.h"
#include "raylib.h"

#ifndef true
#define true 1
#endif

#ifndef false
#define false 0
#endif

typedef struct {
    S32 window_w;
    S32 window_h;
    Vector2 mouse_pos;

    Font font;
    S32 font_size;
} global_state;

global_state state;

B32 path_exists(const char *path)
{
    struct stat st;
    return stat(path, &st) == 0;
}

B32 is_directory(const char *path)
{
    struct stat st;
    return (stat(path, &st) == 0) && S_ISDIR(st.st_mode);
}

const char *get_home_dir()
{
    const char *home = getenv("HOME");
    if(home && home[0] != '\0') return home;

    struct passwd *pw = getpwuid(getuid());
    if(pw && pw->pw_dir && pw->pw_dir[0] != '\0') return pw->pw_dir;

    return NULL;
}

U64 file_size(FILE *f)
{
    fseek(f, 0, SEEK_END);
    U64 size = ftell(f);
    fseek(f, 0, SEEK_SET); // TODO: Save the current position, instead?
    return size;
}

U64 now()
{
    return (U64)time(NULL);
}

B32 activity_button(Rectangle rect, char *title, char *subtitle, F32 font_size, Color font_color, Color background_color)
{
    B32 button_pressed = false;

    // WARNING: Substracting from font size like that is unsafe.
    F32 subtitle_font_size = font_size - 8.0f;
    if(subtitle_font_size <= 0.0f)
        subtitle_font_size = font_size;

    Vector2 title_font_d = MeasureTextEx(state.font, title, (float)font_size, 2);
    Vector2 subtitle_font_d = MeasureTextEx(state.font, subtitle, (float)subtitle_font_size, 2);

    if(CheckCollisionPointRec(state.mouse_pos, rect)) {
        if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
            button_pressed = true;
    }

    // TODO: Clean up.
    DrawRectangleRounded(rect, 0.2f, 100, background_color);
    DrawTextEx(state.font, title, (Vector2){ (rect.x + rect.width / 2.0f - title_font_d.x / 2.0f), (rect.y + rect.height / 2.0f - title_font_d.y / 2.0f) }, font_size, 2, font_color);
    DrawTextEx(state.font, subtitle, (Vector2){ (rect.x + rect.width / 2.0f - subtitle_font_d.x / 2.0f), (rect.y + rect.height / 2.0f - subtitle_font_d.y / 2.0f) + 22.0f }, subtitle_font_size, 2, font_color);

    return button_pressed;
}

typedef enum activity_type {
    other,
    studying,
    programming,
    projects,
    gaming,
    exercise,
    reading,
    activity_type_COUNT
} activity_type;

char *activity_type_string_representation[activity_type_COUNT] = {
    "Other",
    "Studying",
    "Programming",
    "Projects",
    "Gaming",
    "Exercise",
    "Reading"
};

Color activity_type_color_representation[activity_type_COUNT] = {
    RED,
    GREEN,
    BLUE,
    ORANGE,
    MAROON,
    PURPLE,
    BROWN
};

typedef struct activity {
    activity_type type;
    U64 began;
    U64 ended;
} activity;

typedef struct Activity_Stat {
    U64 total_seconds;
    char seconds_str[24];
} Activity_Stat;

typedef struct Hours {
    U64 hours;
    U64 minutes;
    U64 seconds;
} Hours;

Hours break_time(U64 seconds)
{
    Hours hours;

    hours.hours   = seconds / 60 / 60;
    hours.minutes = seconds / 60 - (60 * hours.hours);
    hours.seconds = seconds - ((hours.hours * 60 * 60) + (hours.minutes * 60));

    return hours;
}

static void switch_activity(activity *activities, U32 *current_activity, activity_type type, U64 time)
{
    if((activities[*current_activity].type != type) && (0 <= type && type < activity_type_COUNT)) {
        (*current_activity)++;

        activities[*current_activity].type = type;
        activities[*current_activity].began = time;
        activities[*current_activity].ended = time;
    }
}

struct tm *today()
{
    U64 secs = now();
    struct tm *t = localtime((time_t *)(&secs));
    t->tm_hour = 0;
    t->tm_min  = 0;
    t->tm_sec  = 0;
    return t;
}

U64 start_of_day_seconds(struct tm *t)
{
    // NOTE: Technically, this can fail only if the year is >2038 on 32-bit
    //       platforms as size_t--a 32-bit--value would overflow. ON 64-bit,
    //       this is a non-issue.
    time_t ts = mktime(t);
    return ts;
}

char *get_dir()
{
    local_persist char buffer[128];
    snprintf(buffer, sizeof(buffer), "%s/.timetracker", get_home_dir());
    return buffer;
}

char *get_date_str(struct tm *t)
{
    local_persist char buffer[16];
    strftime(buffer, sizeof(buffer), "%Y_%m_%d", t);
    return buffer;
}

char *get_file(char *dir, struct tm *t)
{
    local_persist char buffer[256];
    snprintf(buffer, sizeof(buffer), "%s/%s", dir, get_date_str(t));
    return buffer;
}

FILE *load_activities(char *dir_path, char *file_path, activity *activities, U32 *current_activity)
{
    if(!is_directory(dir_path)) {
        if(mkdir(dir_path, 0700) == -1) {
            fprintf(stderr, "mkdir(%s) failed: %s\n", dir_path, strerror(errno));
            exit(1);
        }
    }

    FILE *f = fopen(file_path, "a+");
    if(!f) {
        fprintf(stderr, "fopen(%s) failed: %s\n", file_path, strerror(errno));
        exit(1);
    }

    local_persist char line[4096];
    if(file_size(f) > 0) {
        while(fgets(line, sizeof line, f)) {
            size_t n = strcspn(line, "\r\n");
            line[n] = '\0';

            char activity_name[64];
            U64 lower;
            U64 upper;
            int l = sscanf(line, "%s %llu %llu", activity_name, &lower, &upper);
            if(l != 3)
                continue;

            if(lower > upper)
                continue;

            activity_type type = other;
            for(U32 i = 0; i < activity_type_COUNT; i++) {
                if(strcmp(activity_name, activity_type_string_representation[i]) == 0)
                    type = i;
                else
                    continue;
            }

            activities[*current_activity].type = type;
            activities[*current_activity].began = lower;
            activities[*current_activity].ended = upper;

            (*current_activity)++;
        }
    }

    // TODO: Do we really need this?
    if(ferror(f)) {
        fprintf(stderr, "read error: %s\n", strerror(errno));
        exit(1);
    }

    return f;
}

void save_activities(FILE *f, char *file_path, U32 current_activity, activity *activities)
{
    // NOTE: We use "w+" because want to clear file contents before the next write.
    f = freopen(file_path, "w+", f);
    if(f) {
        local_persist char write_buffer[128];
        for(U32 i = 0; i <= current_activity; i++) {
            snprintf(write_buffer, sizeof(write_buffer), "%s %llu %llu\n", activity_type_string_representation[activities[i].type], activities[i].began, activities[i].ended);

            size_t write_buffer_len = strlen(write_buffer);
            if(fwrite(write_buffer, 1, write_buffer_len, f) != write_buffer_len) {
                // NOTE: We do not care, for now, that it might have failed to write some data.
                //       The program will overwrite everything on exit or next save interval.
            }
        }

        if(fflush(f) != 0)
            perror("fflush");
    }
}

global B32 dirty = true;

internal void mark_dirty()
{
    dirty = true;
}

internal void clear_dirty()
{
    dirty = false;
}

internal B32 is_dirty(void)
{
    return dirty;
}

internal void poll_input()
{
    if(GetMouseDelta().x != 0 || GetMouseDelta().y != 0 || IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsMouseButtonReleased(MOUSE_BUTTON_LEFT) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) || GetKeyPressed() != 0 || IsWindowResized())
        mark_dirty();
}

S32 main(S32 argc, char *argv[])
{
    // TODO: Move into global state.
    activity activities[256]; // TODO: This should be a dynamic array.
    memset(activities, 0, sizeof(activities));
    U32 current_activity = 0;

    // TODO: Move into global state.
    Activity_Stat activities_stats[activity_type_COUNT];
    memset(activities_stats, 0, sizeof(activities_stats));

    struct tm *t = today();
    U64 ts = start_of_day_seconds(t);

    // NOTE: Not sure this is great, but that is what we'll go with for now.
    char *dir_path = get_dir();
    char *file_path = get_file(dir_path, t);
    FILE *f = load_activities(dir_path, file_path, activities, &current_activity);

    U64 last_save = 0;

    U64 lower_bound_s = ts;
    U64 upper_bound_s = lower_bound_s + 86400;
    // U64 upper_bound_s = lower_bound_s + 3600;

    state.window_w = 1400;
    state.window_h = 300;

    SetConfigFlags(FLAG_VSYNC_HINT | FLAG_MSAA_4X_HINT | FLAG_WINDOW_HIGHDPI);
    InitWindow(state.window_w, state.window_h, "Time Tracker");
    SetWindowState(FLAG_MSAA_4X_HINT|FLAG_WINDOW_RESIZABLE);

    state.font = LoadFontFromMemory(".ttf", opensans_ttf, opensans_ttf_len, 96, NULL, 0);
    if(!IsFontValid(state.font)) {
        fprintf(stderr, "Unable to load font\n");
        state.font = GetFontDefault();
    }
    GenTextureMipmaps(&state.font.texture);
    SetTextureFilter(state.font.texture, TEXTURE_FILTER_BILINEAR);

    // Initialize to default activity at the start.
    activities[current_activity].type = other;
    activities[current_activity].began = now();
    activities[current_activity].ended = now();

    state.font_size = 30;

    double last_input_s = now();

    SetTargetFPS(30);

    while(!WindowShouldClose()) {
        poll_input();

        U64 now_s = now();

        if(now_s >= upper_bound_s) {
            t = today();
            ts = start_of_day_seconds(t);

            lower_bound_s = ts;
            upper_bound_s = lower_bound_s + 86400;

            fclose(f);

            file_path = get_file(dir_path, t);

            f = fopen(file_path, "r");
            if(!f) {
                fprintf(stderr, "Failed to create file: %s\n", file_path);
                return 1; // TODO: REMOVE.
            }

            activity_type old_type = activities[current_activity].type;
            memset(activities, 0, sizeof(activities));
            memset(activities_stats, 0, sizeof(activities_stats));
            current_activity = 0;

            activities[current_activity].type = old_type;
            activities[current_activity].began = now();
        }

        state.window_w = GetScreenWidth();
        state.window_h = GetScreenHeight();

        state.mouse_pos = GetMousePosition();

        if(IsKeyPressed(KEY_Q))
            break;

        for(U32 i = 0; i < activity_type_COUNT; i++) {
            if(IsKeyPressed(KEY_ONE + i)) {
                switch_activity(activities, &current_activity, i, now_s);
            }
        }

        activities[current_activity].ended = now_s;

        U64 total_seconds = 0;
        for(U32 i = 0; i <= current_activity; i++) {
            U64 difference = activities[i].ended - activities[i].began;

            activities_stats[activities[i].type].total_seconds += difference;
            total_seconds += difference;
        }

        BeginDrawing();
            ClearBackground(BLACK);

            F32 padding_x = 10.0f;

            DrawRectangle(0, 0, state.window_w, 40, GRAY);
            // TOOD: This should be its own component.
            for(U32 i = 0; i <= current_activity; i++) {
                F32 start_x = floor((F32)(activities[i].began - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s) * (F32)(state.window_w));
                F32 end_x = floor(((F32)(activities[i].ended - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s)) * (F32)(state.window_w));
                DrawRectangle(start_x, 0, end_x - start_x, 40, activity_type_color_representation[activities[i].type]);
            }

            DrawRectangle(0, 40, state.window_w, 40, GRAY);
            F32 last_x = 0.0f;
            for(U32 i = 0; i <= activity_type_COUNT; i++) {
                F32 ratio = (F32)(activities_stats[i].total_seconds) / (F32)total_seconds;

                F32 x = ((F32)(state.window_w) * ratio);
                DrawRectangle(last_x, 40, x, 40, activity_type_color_representation[i]);
                last_x += x;
            }

            F32 width = (state.window_w-padding_x*(float)(activity_type_COUNT+1)) / (float)activity_type_COUNT;

            for(U32 i = 0; i < activity_type_COUNT; i ++) {
                Hours time = break_time(activities_stats[i].total_seconds);
                snprintf(activities_stats[i].seconds_str, sizeof(activities_stats[i].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);

                Rectangle rect = { (padding_x*(i+1))+(width*i), 100, width, (state.window_h-110) };
                // TOOD: This should be its own component.
                if(activity_button(rect, activity_type_string_representation[i], activities_stats[i].seconds_str, state.font_size, (activities[current_activity].type == i ? WHITE : BLACK), activity_type_color_representation[i])) {
                    if(activities[current_activity].type != i) {
                        switch_activity(activities, &current_activity, i, now_s);
                    }
                }
                if(activities[current_activity].type == i)
                    DrawRectangleRoundedLinesEx(rect, 0.2f, 100, 5.0f, WHITE);

                local_persist char index_buf[8];
                snprintf(index_buf, sizeof(index_buf), "%d", i + 1);
                DrawTextEx(state.font, index_buf, (Vector2){ rect.x + 10.0f, rect.y + 10.0f }, state.font_size - 5.0f, 2, BLACK);
            }

            // NOTE: This is rendered last because the popups have to float above everything else.
            for(U32 i = 0; i <= current_activity; i++) {
                F32 start_x = floor((F32)(activities[i].began - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s) * (F32)(state.window_w));
                F32 end_x = floor(((F32)(activities[i].ended - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s)) * (F32)(state.window_w));

                Rectangle rect = { start_x, 0, end_x - start_x, 40 };
                if(CheckCollisionPointRec(state.mouse_pos, rect)) {
                    // TOOD: A general popup should be its own component.
                    char *str_activity_type = activity_type_string_representation[activities[i].type];

                    Vector2 activity_type_d = MeasureTextEx(state.font, str_activity_type, state.font_size - 5.0f, 2);
                    Vector2 activity_time_d = MeasureTextEx(state.font, activities_stats[i].seconds_str, state.font_size - 10.0f, 2);

                    S32 rect_w = (S32)((activity_type_d.x >= activity_time_d.x) ? activity_type_d.x + 10.0f : activity_time_d.x + 10.0f);
                    DrawRectangleRounded((Rectangle){ start_x, 40, rect_w, (F32)(activity_time_d.y + activity_type_d.y + 10.0f) }, 0.3f, 100, BLACK);
                    DrawRectangleRoundedLinesEx((Rectangle){ start_x, 40, rect_w, (F32)(activity_time_d.y + activity_type_d.y + 10.0f) }, 0.3f, 100, 2.0f, DARKGRAY);

                    local_persist char buffer[24];
                    Hours time = break_time(activities[i].ended - activities[i].began);
                    snprintf(buffer, sizeof(buffer), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds);

                    // TODO: Display the popup under cursor instead.
                    DrawTextEx(state.font, str_activity_type, (Vector2){ start_x + 5.0f, 40.0f + 5.0f }, state.font_size - 5.0f, 2, WHITE);
                    DrawTextEx(state.font, buffer, (Vector2){ start_x + 5.0f, 40.0f + (F32)(activity_type_d.y) }, state.font_size - 10.0f, 2, WHITE);
                }
            }

            DrawRectangle(state.mouse_pos.x, state.mouse_pos.y, 10, 10, WHITE);
        EndDrawing();

        // Reset statistics as we will accumulate seconds again.
        for(U32 i = 0; i <= current_activity; i++) {
            activities_stats[activities[i].type].total_seconds = 0;
        }

        // Save the state every 5 seconds.
        if((now_s - last_save) >= 5) {
            save_activities(f, file_path, current_activity, activities);
            last_save = now_s;
        }

        if(is_dirty()) {
            clear_dirty();
                SetTargetFPS(120); // FPS cap
        } else {
            if(now_s - last_input_s > 1) {
                SetTargetFPS(30);
                last_input_s = now_s;
            }
        }
    }

    save_activities(f, file_path, current_activity, activities);
    fclose(f);

    CloseWindow();

    return 0;
}
