#include "main.h"

#include "ssd1366.h"

void sendMessages(void* pvParameter)
{
    while (1) {
        printf("Sending message...\n");
        ssd1306_display_clear();
        const char *pcTask1 = "Sending message...\n";
	    xTaskCreate(&task_ssd1306_display_text, "task_ssd1306_display_text", 4096, (void *) pcTask1, 5, NULL);
        TTNResponseCode res = ttn.transmitMessage(msgData, sizeof(msgData) - 1);

        if(res == kTTNSuccessfulTransmission){
            printf("Message sent.\n");
            ssd1306_display_clear();
            const char *pcTask1 = "Message sent.\n";
	        xTaskCreate(&task_ssd1306_display_text, "task_ssd1306_display_text", 4096, (void *) pcTask1, 5, NULL);
        }else{
            printf("Transmission failed.\n");
            ssd1306_display_clear();
            const char *pcTask1 = "Transmission failed.\n";
	        xTaskCreate(&task_ssd1306_display_text, "task_ssd1306_display_text", 4096, (void *) pcTask1, 5, NULL);
        }

        vTaskDelay(TX_INTERVAL * pdMS_TO_TICKS(1000));
    }
}

void messageReceived(const uint8_t* message, size_t length, ttn_port_t port)
{
    printf("Message of %d bytes received on port %d:", length, port);
    for (int i = 0; i < length; i++)
        printf(" %02x", message[i]);
    printf("\n");
}

extern "C" void app_main(void)
{
    esp_err_t err;
    // Initialize the GPIO ISR handler service
    err = gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
    ESP_ERROR_CHECK(err);
    
    // Initialize the NVS (non-volatile storage) for saving and restoring the keys
    err = nvs_flash_init();
    ESP_ERROR_CHECK(err);

    // Initialize SPI bus
    spi_bus_config_t spi_bus_config;
    spi_bus_config.miso_io_num = TTN_PIN_SPI_MISO;
    spi_bus_config.mosi_io_num = TTN_PIN_SPI_MOSI;
    spi_bus_config.sclk_io_num = TTN_PIN_SPI_SCLK;
    spi_bus_config.quadwp_io_num = -1;
    spi_bus_config.quadhd_io_num = -1;
    spi_bus_config.max_transfer_sz = 0;
    err = spi_bus_initialize(TTN_SPI_HOST, &spi_bus_config, TTN_SPI_DMA_CHAN);
    ESP_ERROR_CHECK(err);

    // Configure the SX127x pins
    ttn.configurePins(TTN_SPI_HOST, TTN_PIN_NSS, TTN_PIN_RXTX, TTN_PIN_RST, TTN_PIN_DIO0, TTN_PIN_DIO1);

    // The below line can be commented after the first run as the data is saved in NVS
    ttn.provision(CONFIG_DEVEUI, CONFIG_APPEUI, CONFIG_APPKEY);

    // Register callback for received messages
    ttn.onMessage(messageReceived);


    i2c_master_init();
	ssd1306_init();

    printf("Joining...\n");
    ssd1306_display_clear();
    const char *pcTask1 = "Joining...\n";
	xTaskCreate(&task_ssd1306_display_text, "task_ssd1306_display_text", 4096, (void *) pcTask1, 5, NULL);
    if (ttn.join())
    {
        printf("Joined.\n");
        ssd1306_display_clear();
        const char *pcTask1 = "Joined.\n";
	    xTaskCreate(&task_ssd1306_display_text, "task_ssd1306_display_text", 4096, (void *) pcTask1, 5, NULL);
        xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, nullptr);
    }
    else
    {
        printf("Join failed. Goodbye\n");
        esp_restart();
    }
}