/**
 * @file mqtt_application.c
 * @brief mosquitto application for group assignment
 * @author RobinVdB
 */

#include <string.h>
#include "httpd.h"
#include "lcd_api.h"
#include "lwip/apps/fs.h"
#include "lwip/ip_addr.h"
#include "mdns.h"
#include "mqtt.h"
#include "mqtt_application.h"

#define LOGGER_LEVEL_INFO
#include "log.h"

#define ATTEMPT_RECONNECT_AMOUNT 50
#define PUBLISH_QOS 2
#define PUBLISH_RETAIN 1
#define MQTT_SERVER_PORT 1883
#define PRINT_XPOS 50
#define PRINT_YPOS 50
#define MAX_FILES 20
#define SERVER_IP4_A 192
#define SERVER_IP4_B 168
#define SERVER_IP4_C 69
#define SERVER_IP4_D 11
#define SERVER_PORT 1883

typedef enum input_topic {
    set_text,
    set_text_color,
    set_color,
    set_image,
    other_topic
} input_topic_t;

// Function prototypes
static void mqtt_pub_request_cb(void*, err_t);
static void publish_data(mqtt_client_t*, void*);
static void mqtt_incoming_publish_cb(void*, const char*, u32_t);
static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t);
static void mqtt_sub_request_cb(void*, err_t);
static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t);
static void mosquitto_connect(mqtt_client_t*);
static uint32_t color_picker(char*);
static void create_publish_string(char*, char*, size_t);

// Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics
static sFONT* font;
static uint16_t xpos;
static uint16_t ypos;
static uint16_t connection_attempt_counter;
static uint32_t color;
static uint32_t bgcolor;
static input_topic_t inpub_id;
static const char* TAG = "MQTT";

/**
 * @brief callback function for publishing data
 *
 * @param[in] arg       User supplied argument to connection callback
 * @param[in] result    Whether the publish was successful or not
 */
static void mqtt_pub_request_cb(void* arg, err_t result) {
    LOG_DEBUG(TAG, "Publish result: %d", result);
}

/**
 * @brief Publishes data
 * Publishes the names of all the .bmp and .gif files on the topic getImageList
 *
 * @param[in] client    Pointer to the MQTT client
 * @param[in] arg       Additional argument to pass to the callback function
 */
static void publish_data(mqtt_client_t* client, void* arg) {
    char pub_payload[200] = {0};
    err_t err;

    LOG_DEBUG(TAG, "Entering publish");

    create_publish_string("*.bmp", pub_payload,sizeof(pub_payload));

    err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg);
    if (err != ERR_OK) {
        LOG_DEBUG(TAG, "Publish err: %d", err);
    }

    pub_payload[0] = '\0';
    create_publish_string("*.gif", pub_payload, sizeof(pub_payload));

    err = mqtt_publish(client, "getGifList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg);
    if (err != ERR_OK) {
        LOG_DEBUG(TAG, "Publish err: %d", err);
    }
}

/**
 * @brief Handles incoming publish
 * Callback function for when data was published to a subscribed topic
 *
 * @param[in] arg       User supplied argument to connection callback
 * @param[in] topic     The topic on which an incoming publish was received
 * @param[in] tot_len   Length of the incoming data
 */
static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_len) {
    LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %lu", topic, tot_len);
    // Check for which topic a publish was received
    if (strcmp(topic, "input/setText") == 0) {
        inpub_id = set_text;
        return;
    }
    if (strcmp(topic, "input/setImage") == 0) {
        inpub_id = set_image;
        return;
    }
    if (strcmp(topic, "input/setTextColor") == 0) {
        inpub_id = set_text_color;
        return;
    }
    if (strcmp(topic, "input/setColor") == 0) {
        inpub_id = set_color;
        return;
    }
    // In case of wrong topic
    inpub_id = other_topic;
}

/**
 * @brief Handles incoming publish data
 * Handles the received data from a publish to a subscribed topic
 *
 * @param[in] arg       User supplied argument to connection callback
 * @param[in] data      The incoming data
 * @param[in] len       Length of the data
 * @param[in] flags     Whether this is the last fragment of the incoming data
 */
static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, uint8_t flags) {
    char data_buffer[len + 1];
    lcd_gif_t* gif;

    LOG_INFO(TAG, "Incoming publish payload with length %d, flags %d", len, flags);
    if (!(flags & MQTT_DATA_FLAG_LAST)) {
       LOG_WARN(TAG, "incoming data too big to fit in buffer.");
       return;
    }
    memcpy(data_buffer, data, len);
    data_buffer[len] = '\0';
    switch (inpub_id) {
        case set_text:
            // Places text on the lcd
            LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer);
            lcd_clear_text();
            lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font);
            break;
        case set_image:
            // Places an image on the lcd
            LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer);
            lcd_clear_images();
            lcd_set_bg_color_layer0(bgcolor);
            if (len >= 3) {
                if (data_buffer[len - 3] == 'b') {
                    lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos);
                }
                if (data_buffer[len - 3] == 'g') {
                    gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos);
                    if (gif == NULL) {
                        LOG_INFO(TAG, "GIF could not be drawn");
                    }
                }
            }
            break;
        case set_text_color:
            // Changes the text color for the next time text is written
            LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer);
            color = color_picker(data_buffer);
            break;
        case set_color:
            // Changes the background color for the next time text is written
            LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer);
            bgcolor = color_picker(data_buffer);
            break;
        default:
            LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored.");
    }
}

/**
 * @brief Callback function for outgoing subscribe request
 *
 * @param[in] arg       User supplied argument to connection callback
 * @param[in] result    Result code for the subscribe request
 */
static void mqtt_sub_request_cb(void* arg, err_t result) {
    LOG_DEBUG(TAG, "Subscribe result: %d", result);
}

/**
 * @brief Callback function for attempting a connection
 * If a connection was made setup a callback function for incoming publishes.
 * subscribes to the input topics and calls the publish_data function.
 *
 * @param[in] client    Pointer to the MQTT client
 * @param[in] arg       User supplied argument to connection callback
 * @param[in] status    Connect result code or disconnection notification
 */
static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection_status_t status) {
    err_t err;

    if (status != MQTT_CONNECT_ACCEPTED) {
        LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status);

        if (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) {
            connection_attempt_counter++;
            // Try to reconnect
            mosquitto_connect(client);
        }
        return;
    }
    LOG_INFO(TAG, "Successfully connected");

    connection_attempt_counter = 0;
    // Set up callback function for input
    mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg);

    // Subscribe to the topics setText, setImage, setColor and setTextcolor
    err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg);
    if (err != ERR_OK) {
        LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err);
    }

    // Publish list of images here
    publish_data(client, NULL);
}

/**
 * @brief Attempts to create a connection to the mosquitto broker
 * Creates a mqtt client and sets up a connection callback function
 *
 * @param[in] client    Pointer to the MQTT client
 */
static void mosquitto_connect(mqtt_client_t* client) {
    struct mqtt_connect_client_info_t ci;
    err_t err;

    LOG_INFO(TAG, "Attempting MQTT Connection");

    memset(&ci, 0, sizeof(ci));

    ci.client_id = "STM32";
    ip_addr_t server_ip;
    IP4_ADDR(&server_ip, SERVER_IP4_A, SERVER_IP4_B, SERVER_IP4_C, SERVER_IP4_D);
    uint16_t server_port = SERVER_PORT;
    err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci);
    if (err != ERR_OK) {
        LOG_DEBUG(TAG, "mqtt_connect return %d", err);
        return;
    }
    LOG_DEBUG(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err);
}

/**
 * @brief Init function for the mosquitto application of the assignment
 * Gives the global variables a value and calls the mosquitto_connect function
 */
uint8_t mqtt_application_init(void) {
    color = LCD_BLACK;
    bgcolor = LCD_WHITE;
    font = LCD_FONT16;
    xpos = PRINT_XPOS;
    ypos = PRINT_YPOS;
    connection_attempt_counter = 0;

    mqtt_client_t* client = mqtt_client_new();
    if (client == NULL) {
        LOG_CRIT(TAG, "%s: client == NULL", __func__);
        return 1;
    }
    LOG_DEBUG(TAG, "Starting connection test");
    mosquitto_connect(client);

    return 0;
}

/**
 * @brief Reads the color input string and outputs it to a usable value for LCD_APi
 *
 * @param[in] color     Input string to select a color
 * @return color        Define to use with the LCD_API
 */
uint32_t color_picker(char* color_in) {
    for (int i = 0; i < strlen(color_in); i++) {
        color_in[i] = tolower(color_in[i]);
    }
    if (strcmp((const char*)color_in, "blue") == 0) {
        return LCD_BLUE;
    }
    if (strcmp((const char*)color_in, "green") == 0) {
        return LCD_GREEN;
    }
    if (strcmp((const char*)color_in, "red") == 0) {
        return LCD_RED;
    }
    if (strcmp((const char*)color_in, "cyan") == 0) {
        return LCD_CYAN;
    }
    if (strcmp((const char*)color_in, "magenta") == 0) {
        return LCD_MAGENTA;
    }
    if (strcmp((const char*)color_in, "yellow") == 0) {
        return LCD_YELLOW;
    }
    if (strcmp((const char*)color_in, "light blue") == 0) {
        return LCD_LIGHTBLUE;
    }
    if (strcmp((const char*)color_in, "light green") == 0) {
        return LCD_LIGHTGREEN;
    }
    if (strcmp((const char*)color_in, "light red") == 0) {
        return LCD_LIGHTRED;
    }
    if (strcmp((const char*)color_in, "light cyan") == 0) {
        return LCD_LIGHTCYAN;
    }
    if (strcmp((const char*)color_in, "light magenta") == 0) {
        return LCD_LIGHTMAGENTA;
    }
    if (strcmp((const char*)color_in, "light yellow") == 0) {
        return LCD_LIGHTYELLOW;
    }
    if (strcmp((const char*)color_in, "dark blue") == 0) {
        return LCD_DARKBLUE;
    }
    if (strcmp((const char*)color_in, "dark green") == 0) {
        return LCD_DARKGREEN;
    }
    if (strcmp((const char*)color_in, "dark red") == 0) {
        return LCD_DARKRED;
    }
    if (strcmp((const char*)color_in, "dark cyan") == 0) {
        return LCD_DARKCYAN;
    }
    if (strcmp((const char*)color_in, "dark magenta") == 0) {
        return LCD_DARKMAGENTA;
    }
    if (strcmp((const char*)color_in, "dark yellow") == 0) {
        return LCD_DARKYELLOW;
    }
    if (strcmp((const char*)color_in, "white") == 0) {
        return LCD_WHITE;
    }
    if (strcmp((const char*)color_in, "light gray") == 0) {
        return LCD_LIGHTGRAY;
    }
    if (strcmp((const char*)color_in, "gray") == 0) {
        return LCD_GRAY;
    }
    if (strcmp((const char*)color_in, "dark gray") == 0) {
        return LCD_DARKGRAY;
    }
    if (strcmp((const char*)color_in, "black") == 0) {
        return LCD_BLACK;
    }
    if (strcmp((const char*)color_in, "brown") == 0) {
        return LCD_BROWN;
    }
    if (strcmp((const char*)color_in, "orange") == 0) {
        return LCD_ORANGE;
    }
    if (strcmp((const char*)color_in, "transparent") == 0) {
        return LCD_TRANSPARENT;
    }

    return LCD_BLACK;
}

/**
 * @brief creates a string to publish on the getImageList topic
 *
 * @param[in] file_type         The file extension asked to be published
 * @param[in] payload_buffer    The string to be published
 * @param[in] buffer_size       Size of payload_buffer
 */
static void create_publish_string(char* file_type, char* payload_buffer, size_t buffer_size) {
    size_t num_files;
    llfs_file_t file_list[MAX_FILES];

    num_files = llfs_file_list(file_list, MAX_FILES, file_type);

    if (num_files == 0) {
        strncat(payload_buffer, "No files found of type: ", buffer_size - strlen(payload_buffer) - 1);
        strncat(payload_buffer, file_type, buffer_size - strlen(payload_buffer) - 1);
        LOG_INFO(TAG, "%s: No files found of type %s", __func__, file_type);
        return;
    }

    if (strcmp(file_type, "*.bmp") == 0) {
        strncat(payload_buffer, "Available images: ", buffer_size - strlen(payload_buffer) - 1);
    } else if (strcmp(file_type, "*.gif") == 0) {
        strncat(payload_buffer, "Available gifs: ", buffer_size - strlen(payload_buffer) - 1);
    } else {
        LOG_WARN(TAG, "No application for given file type: %s", file_type);
        return;
    }
    for (size_t i = 0; i < num_files; i++) {
        // Concatenate file names into the payload string
        strncat(payload_buffer, file_list[i].name, buffer_size - strlen(payload_buffer) - 1);
        strncat(payload_buffer, ", ", buffer_size - strlen(payload_buffer) - 1); // Add a comma between file names
    }
    strncat(payload_buffer, "\0", buffer_size - strlen(payload_buffer) - 1);
    LOG_DEBUG(TAG, "String: %s", payload_buffer);
}
