#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#define LED_RED GPIO_NUM_25
#define LED_GREEN GPIO_NUM_26
#define LED_BLUE GPIO_NUM_27

struct led_task_parameters_t
{
  gpio_num_t led_gpio;
  TickType_t blink_time;
};

static led_task_parameters_t red_led_gpio = {LED_RED, 2000};
static led_task_parameters_t blue_led_gpio = {LED_BLUE, 1000};
static led_task_parameters_t green_led_gpio = {LED_GREEN, 500};


void led_task(void *pvParameter)
{
  gpio_num_t led_gpio = ((led_task_parameters_t *)pvParameter)->led_gpio;
  TickType_t blink_time = ((led_task_parameters_t *)pvParameter)->blink_time;
  uint8_t led_value = 0;
  gpio_reset_pin(led_gpio);
  gpio_set_direction(led_gpio, GPIO_MODE_OUTPUT);

  while (1) {
    gpio_set_level(led_gpio, led_value);
    led_value = !led_value;
    vTaskDelay(blink_time / portTICK_PERIOD_MS);
  }
  vTaskDelete( NULL );
}

const char *string_list =
  "Wireless\n"
  "Temperature\n"
  "Alarm\n"
  "Timer\n"
  "Reset\n"
  "Version\n"
  "Update\n"
  "Shutdown\n"
  "Flash Light\n"
  "Back";

uint8_t current_selection = 0;

void display_task(void *pvParameter){

  u8g2.begin(/*Select=*/ 2, /*Right/Next=*/ 18, /*Left/Prev=*/ 5, /*Up=*/ 19, /*Down=*/ 15, /*Home/Cancel=*/ 4);
  
  u8g2.setFont(u8g2_font_6x12_tr);

  while(1){
    current_selection = u8g2.userInterfaceSelectionList(
                        "Settings",
                        current_selection,
                        string_list);

  if ( current_selection == 0 ) {
    u8g2.userInterfaceMessage(
      "Nothing selected.",
      "",
      "",
      " ok ");
  } else {
    u8g2.userInterfaceMessage(
      "Selection:",
      u8x8_GetStringLineStart(current_selection - 1, string_list ),
      "",
      " ok \n cancel ");
  }
  }
}


void setup(void) {

  xTaskCreate(
    &led_task, // task function
    "red_led_task", // task name
    2048, // stack size in words
    &red_led_gpio, // pointer to parameters
    5, // priority
    NULL); // out pointer to task handle

  xTaskCreate(
    &led_task, // task function
    "blue_led_task", // task name
    2048, // stack size in words
    &blue_led_gpio, // pointer to parameters
    5, // priority
    NULL); // out pointer to task handle

  xTaskCreate(
    &led_task, // task function
    "green_led_task", // task name
    2048, // stack size in words
    &green_led_gpio, // pointer to parameters
    5, // priority
    NULL); // out pointer to task handle

   xTaskCreate(
    &display_task, // task function
    "green_led_task", // task name
    4096, 
    NULL, 
    1, 
    NULL);

}




void loop(void) {

  
}