#include "helper.h"

// TODO: move to driver
void clean_bomb(void) {
    // clear pending inputs
    clear_all_pressed_released();
    clear_wires_pressed_released_cut();

    // clear leds
    leds_clear();
    leds_flush();

    // clear module timer
    stop_module_timer();
    set_module_time(0);
    uint8_t clear[4] = {0};
    set_module_sseg_raw(clear);

    // clear char lcd
    lcd_set_cursor_vis(false);
    lcd_clear();

    // TODO: add stuff for starcode system
}

static lv_obj_t* old_scr;
static lv_obj_t* scr;

static lv_style_t game_results_style;
static lv_obj_t* overall_results_label;
static lv_obj_t* breakdown_results_label;
static lv_obj_t* strike_numbers_label;
static lv_obj_t* step_times_label;

static void write_time(char* buf, int32_t game_time) {
    char minus[2] = "\0";
    if (game_time < 0) {
        minus[0] = '-';
        game_time = -game_time;
    }

    uint8_t hours = (game_time / (1000*60*60)) % 10;
    uint8_t minutes = (game_time / (1000*60)) % 60;
    uint8_t seconds = (game_time / (1000)) % 60;

    sprintf(buf, "%s%d:%02d:%02d", minus, hours, minutes, seconds);
}

extern uint32_t initial_game_time;
void display_game_results(void) {
    while (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdFALSE) vTaskDelay(pdMS_TO_TICKS(10));
    scr = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(scr, lv_color_black(), LV_STATE_DEFAULT);
    
    lv_style_init(&game_results_style);
    lv_style_set_text_color(&game_results_style, lv_color_white());
    lv_style_set_text_align(&game_results_style, LV_TEXT_ALIGN_LEFT);

    overall_results_label = lv_label_create(scr);
    lv_obj_align(overall_results_label, LV_ALIGN_TOP_LEFT, 20, 20);
    lv_obj_set_size(overall_results_label, 460, 140);
    lv_obj_add_style(overall_results_label, &game_results_style, LV_STATE_DEFAULT);

    breakdown_results_label = lv_label_create(scr);
    lv_obj_align(breakdown_results_label, LV_ALIGN_BOTTOM_LEFT, 20, 0);
    lv_obj_set_size(breakdown_results_label, 460, 140);
    lv_obj_add_style(breakdown_results_label, &game_results_style, LV_STATE_DEFAULT);

    strike_numbers_label = lv_label_create(scr);
    lv_obj_align(strike_numbers_label, LV_ALIGN_BOTTOM_LEFT, 90, 0);
    lv_obj_set_size(strike_numbers_label, 60, 140);
    lv_obj_add_style(strike_numbers_label, &game_results_style, LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(strike_numbers_label, LV_TEXT_ALIGN_RIGHT, LV_STATE_DEFAULT);

    step_times_label = lv_label_create(scr);
    lv_obj_align(step_times_label, LV_ALIGN_BOTTOM_LEFT, 140, 0);
    lv_obj_set_size(step_times_label, 80, 140);
    lv_obj_add_style(step_times_label, &game_results_style, LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(step_times_label, LV_TEXT_ALIGN_RIGHT, LV_STATE_DEFAULT);

    char buf[1024] = {0};

    char time_buf_1[32] = {0};
    char time_buf_2[32] = {0};
    char time_buf_3[32] = {0};
    char time_buf_4[32] = {0};
    char time_buf_5[32] = {0};
    char time_buf_6[32] = {0};

    int32_t time_left = get_game_time();
    int32_t time_spent = initial_game_time - time_left;
    int32_t true_time_spent = time_spent - (total_strikes * STRIKE_TIME_PENALTY);
    write_time(time_buf_1, time_left);
    write_time(time_buf_2, time_spent);
    write_time(time_buf_3, true_time_spent);

    sprintf(buf,
        "Finished!\n\tTotal Strikes: %ld\n\tTime Left (w/ penalties): %s\n\tTime Spent (w/ penalties): %s\n\tTrue Time Spent: %s\n\n",
        total_strikes, time_buf_1, time_buf_2, time_buf_3
    );
    lv_label_set_text(overall_results_label, buf);

    lv_label_set_text(breakdown_results_label, "Step Breakdown:\n\tStep 1: \n\tStep 2:\n\tStep 3:\n\tStep 4:\n\tStep 5:\n\tStep 6:");

    sprintf(buf, 
        "\n%ld strikes\n%ld strikes\n%ld strikes\n%ld strikes\n%ld strikes\n%ld strikes",
        step_strikes[0], step_strikes[1], step_strikes[2], step_strikes[3], step_strikes[4], step_strikes[5]
    );
    lv_label_set_text(strike_numbers_label, buf);

    write_time(time_buf_1, initial_game_time - step_finish_times[0]);
    write_time(time_buf_2, step_finish_times[0] - step_finish_times[1]);
    write_time(time_buf_3, step_finish_times[1] - step_finish_times[2]);
    write_time(time_buf_4, step_finish_times[2] - step_finish_times[3]);
    write_time(time_buf_5, step_finish_times[3] - step_finish_times[4]);
    write_time(time_buf_6, step_finish_times[4] - step_finish_times[5]);
    sprintf(buf, 
        "\n%s\n%s\n%s\n%s\n%s\n%s",
        time_buf_1, time_buf_2, time_buf_3, time_buf_4, time_buf_5, time_buf_6
    );
    lv_label_set_text(step_times_label, buf);

    old_scr = lv_scr_act();
    lv_scr_load(scr);

    xSemaphoreGive(xGuiSemaphore);
}
