#include "char_lcd.h"

#include "./i2c_lcd_pcf8574.h"
#include <esp_log.h>
#include "state_tracking.h"
#include <cstring>
#include "power.h"
#include "starcode.h"
#include "game_info.h"

i2c_lcd_pcf8574_handle_t lcd;
SemaphoreHandle_t lcd_mutex;

static volatile bool cursor_visible = false;
static volatile uint8_t cursor_resting_row = 0;
static volatile uint8_t cursor_resting_col = 0;

static volatile bool header_enabled = false;

static const char *TAG = "char_lcd";
static const char* EMPTY_ROW = "                    ";

static char buf[65];

// TODO: move this to power.cpp
static void monitor_battery_task(void* _arg) {
    (void) _arg;

    while (true) {
        vTaskDelay(pdMS_TO_TICKS(1'000));
        lcd_print_header_bat();
    }
}

static bool replay_handler(const char* event, char* arg) {
    if (strcmp(event, "LCD_CLEAR") == 0) {
        lcd_clear();
    }
    else if (strcmp(event, "LCD_SET_DISPLAY") == 0) {
        lcd_set_display(strcmp(arg, "true") == 0);
    }
    else if (strcmp(event, "LCD_CURSOR_VIS") == 0) {
        lcd_set_cursor_vis(strcmp(arg, "true") == 0);
    }
    else if (strcmp(event, "LCD_CURSOR_BLINK") == 0) {
        lcd_set_cursor_blink(strcmp(arg, "true") == 0);
    }
    else if (strcmp(event, "LCD_SCROLL_DISPLAY_LEFT") == 0) {
        lcd_scroll_display_left();
    }
    else if (strcmp(event, "LCD_SCROLL_DISPLAY_RIGHT") == 0) {
        lcd_scroll_display_right();
    }
    else if (strcmp(event, "LCD_LEFT_TO_RIGHT") == 0) {
        lcd_left_to_right();
    }
    else if (strcmp(event, "LCD_RIGHT_TO_LEFT") == 0) {
        lcd_right_to_left();
    }
    else if (strcmp(event, "LCD_AUTOSCROLL") == 0) {
        lcd_set_autoscroll(strcmp(arg, "true") == 0);
    }
    else if (strcmp(event, "LCD_BACKLIGHT") == 0) {
        lcd_set_backlight(strcmp(arg, "true") == 0);
    }
    else if (strcmp(event, "LCD_CREATE_CHAR") == 0) {
        char* location_str = strtok(arg, ",");
        uint8_t location = atoi(location_str);

        uint8_t charmap[8];
        for (int i = 0; i < 8; i++) {
            char* str = strtok(NULL, ",");
            charmap[i] = atoi(str);
        }

        lcd_create_char(location, charmap);
    }
    else if (strcmp(event, "LCD_PRINT") == 0) {
        char* str = strtok(arg, ",");
        uint8_t row = atoi(str);
        str = strtok(NULL, ",");
        uint8_t col = atoi(str);
        // get remaining part of string.
        str = strtok(NULL, "");

        // TODO: handle \r and \n
        lcd_print(row, col, str);
    } else {
        return false;
    }

    return true;
}

void init_lcd() {
    ESP_LOGI(TAG, "Initializing LCD...");

    lcd_mutex = xSemaphoreCreateMutex();
    assert(lcd_mutex != NULL);

    lcd_init(&lcd, LCD_ADDR, CHAR_LCD_I2C_NUM);
    lcd_begin(&lcd, LCD_COLS, LCD_ROWS);

    lcd_set_backlight_to(&lcd, 1);

    register_replay_fn(replay_handler);

    xTaskCreate(monitor_battery_task, "bat_monitor", 1024*2, nullptr, 0, nullptr);

    ESP_LOGI(TAG, "LCD initialized!");
}

void lcd_clear(bool no_lock) {
    if (!header_enabled) {
        if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
        lcd_clear(&lcd);
        if (!no_lock) xSemaphoreGive(lcd_mutex);

        if (is_state_tracking()) {
            event_occured("LCD_CLEAR", NULL);
        }
    } else {
        if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
        lcd_print(1, 0, EMPTY_ROW, true);
        lcd_print(2, 0, EMPTY_ROW, true);
        lcd_print(3, 0, EMPTY_ROW, true);
        if (!no_lock) xSemaphoreGive(lcd_mutex);
    }
}

void lcd_set_display(bool display,  bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    if (display) {
        lcd_display(&lcd);
    } else {
        lcd_no_display(&lcd);
    }
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        event_occured("LCD_SET_DISPLAY", display ? "true" : "false");
    }
}

void lcd_set_cursor_vis(bool cursor, bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    if (cursor) {
        lcd_cursor(&lcd);
    } else {
        lcd_no_cursor(&lcd);
    }
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    cursor_visible = cursor;

    if (is_state_tracking()) {
        event_occured("LCD_CURSOR_VIS", cursor ? "true" : "false");
    }
}

void lcd_set_cursor_blink(bool blink, bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    if (blink) {
        lcd_blink(&lcd);
    } else {
        lcd_no_blink(&lcd);
    }
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        event_occured("LCD_CURSOR_BLINK", blink ? "true" : "false");
    }
}

void lcd_scroll_display_left(bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    lcd_scroll_display_left(&lcd);
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        event_occured("LCD_SCROLL_DISPLAY_LEFT", NULL);
    }
}
void lcd_scroll_display_right(bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    lcd_scroll_display_right(&lcd);
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        event_occured("LCD_SCROLL_DISPLAY_RIGHT", NULL);
    }
}

void lcd_left_to_right(bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    lcd_left_to_right(&lcd);
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        event_occured("LCD_LEFT_TO_RIGHT", NULL);
    }
}
void lcd_right_to_left(bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    lcd_right_to_left(&lcd);
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        event_occured("LCD_RIGHT_TO_LEFT", NULL);
    }
}

void lcd_set_autoscroll(bool autoscroll, bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    if (autoscroll) {
        lcd_autoscroll(&lcd);
    } else {
        lcd_no_autoscroll(&lcd);
    }
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        event_occured("LCD_AUTOSCROLL", autoscroll ? "true" : "false");
    }
}

void lcd_set_backlight(bool backlight, bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    lcd_set_backlight_to(&lcd, backlight);
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        sprintf(buf, "%d", backlight);
        event_occured("LCD_BACKLIGHT", backlight ? "true" : "false");
    }
}

void lcd_create_char(uint8_t location, const uint8_t charmap[], bool no_lock) {
    if (location == 8) location = 0;

    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    lcd_create_char(&lcd, location, charmap);
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        snprintf(buf, sizeof(buf),
            "%d,%d,%d,%d,%d,%d,%d,%d,%d", location, 
            charmap[0], charmap[1], charmap[2], charmap[3], charmap[4], charmap[5], charmap[6], charmap[7]
        );
        event_occured("LCD_CREATE_CHAR", buf);
    }
}

void lcd_print(uint8_t row, uint8_t col, const char* str, bool no_lock) {
    if (!no_lock) xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    lcd_set_cursor(&lcd, col, row);
    lcd_print(&lcd, str);
    
    if (cursor_visible) {
        lcd_set_cursor(&lcd, cursor_resting_col, cursor_resting_row);
    }
    
    if (!no_lock) xSemaphoreGive(lcd_mutex);

    if (is_state_tracking()) {
        // TODO: handle \r and \n and others
        snprintf(buf, sizeof(buf), "%d,%d,%s", row, col, str);
        event_occured("LCD_PRINT", buf);
    }
}

void set_lcd_header_enabled(bool enable) {
    bool old_header_enabled = header_enabled;
    header_enabled = enable;

    // update header in response to enabling/disabling the header
    if (enable && !old_header_enabled) {
        lcd_print_header();
    } else if (!enable && old_header_enabled) {
        lcd_print(0, 0, EMPTY_ROW);
    }
}

bool lcd_header_enabled() {
    return header_enabled;
}

void lcd_print_header() {
    lcd_print_header_star_code();
    lcd_print_header_step();
    lcd_print_header_bat();
}

void lcd_do_splash() {
    const uint8_t custom_char[6][8] = {
        { 0x01, 0x01, 0x02, 0x02, 0x07, 0x07, 0x0F, 0x0D },
        { 0x10, 0x10, 0x18, 0x18, 0x1C, 0x0C, 0x0E, 0x06 },
        { 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x07, 0x07 },
        { 0x19, 0x1B, 0x13, 0x17, 0x07, 0x0F, 0x0F, 0x1F },
        { 0x13, 0x1B, 0x1F, 0x1F, 0x00, 0x1F, 0x1F, 0x1F },
        { 0x00, 0x00, 0x10, 0x10, 0x00, 0x18, 0x1C, 0x1C },
    };

    // TODO: make the lcd_lib somehow support the custom character 0 which would otherwise be a null terminator
    xSemaphoreTake(lcd_mutex, portMAX_DELAY);
    lcd_create_char(1, custom_char[0], true);
    lcd_create_char(2, custom_char[1], true);
    lcd_create_char(3, custom_char[2], true);
    lcd_create_char(4, custom_char[3], true);
    lcd_create_char(5, custom_char[4], true);
    lcd_create_char(6, custom_char[5], true);

    lcd_print(1, 6, "\x01\x02Marino", true);
    lcd_print(2, 5, "\x03\x04\x05\x06""DEV", true);
    xSemaphoreGive(lcd_mutex);
}

bool lcd_lock(uint32_t ticks_to_wait) {
    return xSemaphoreTake(lcd_mutex, ticks_to_wait);
}

void lcd_unlock() {
    xSemaphoreGive(lcd_mutex);
}

void lcd_set_cursor_resting_position(uint8_t row, uint8_t col) {
    cursor_resting_row = row;
    cursor_resting_col = col;
}

void lcd_get_cursor_resting_position(uint8_t* row, uint8_t* col) {
    if (row) *row = cursor_resting_row;
    if (col) *col = cursor_resting_col;
}

bool lcd_is_cursor_visible() {
    return cursor_visible;
}