// Project includes
#include "led.hpp"

// SDK Includes
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#if defined(CONFIG_CALENDINK_LED_TYPE_STRIP) || !defined(CONFIG_CALENDINK_LED_TYPE_SIMPLE)
#ifndef CONFIG_CALENDINK_LED_TYPE_STRIP
#define CONFIG_CALENDINK_LED_TYPE_STRIP 1
#endif
#include "led_strip.h"
#endif

#define LED_GPIO (gpio_num_t)CONFIG_CALENDINK_LED_GPIO

#if defined(CONFIG_CALENDINK_LED_TYPE_SIMPLE)
#if defined(CONFIG_CALENDINK_LED_ACTIVE_LOW)
#define SIMPLE_LED_ON  0
#define SIMPLE_LED_OFF 1
#else
#define SIMPLE_LED_ON  1
#define SIMPLE_LED_OFF 0
#endif
#endif

#if defined(CONFIG_CALENDINK_LED_TYPE_STRIP)
internal led_strip_handle_t led_strip;
#endif

void setup_led(void)
{
#if defined(CONFIG_CALENDINK_LED_TYPE_STRIP)
  /* LED strip initialization with the GPIO and pixels number*/
  led_strip_config_t strip_config = {};
  strip_config.strip_gpio_num = LED_GPIO;
  strip_config.max_leds = 1; // at least one LED on board

  led_strip_rmt_config_t rmt_config = {};
  rmt_config.resolution_hz = 10 * 1000 * 1000; // 10MHz
  rmt_config.flags.with_dma = false;
  ESP_ERROR_CHECK(
      led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));

  led_strip_clear(led_strip);
  led_strip_refresh(led_strip);
#elif defined(CONFIG_CALENDINK_LED_TYPE_SIMPLE)
  gpio_reset_pin(LED_GPIO);
  gpio_set_direction(LED_GPIO, GPIO_MODE_OUTPUT);
  gpio_set_level(LED_GPIO, SIMPLE_LED_OFF);
#endif
}

void turn_off_led(void)
{
#if defined(CONFIG_CALENDINK_LED_TYPE_STRIP)
  led_strip_clear(led_strip);
  led_strip_refresh(led_strip);
#elif defined(CONFIG_CALENDINK_LED_TYPE_SIMPLE)
  gpio_set_level(LED_GPIO, SIMPLE_LED_OFF);
#endif
}

void set_led_status(led_status status)
{
#if defined(CONFIG_CALENDINK_LED_TYPE_STRIP)
  switch (status)
  {
  case led_status::ConnectingEthernet:
    led_strip_set_pixel(led_strip, 0, 255, 165, 0);
    break;
  case led_status::ConnectingWifi:
    led_strip_set_pixel(led_strip, 0, 148, 0, 211);
    break;
  case led_status::ReadyEthernet:
    led_strip_set_pixel(led_strip, 0, 0, 255, 0); // Green
    break;
  case led_status::ReadyWifi:
    led_strip_set_pixel(led_strip, 0, 0, 0, 255); // Blue
    break;
  case led_status::Failed:
    led_strip_set_pixel(led_strip, 0, 255, 0, 0);
    break;
  }
  led_strip_refresh(led_strip);
#elif defined(CONFIG_CALENDINK_LED_TYPE_SIMPLE)
  // For a simple LED we just turn it on to show it's reached a status, 
  // or we could add simple blink patterns, but keeping it ON is simplest for status presence.
  switch (status)
  {
  case led_status::ConnectingEthernet:
  case led_status::ConnectingWifi:
  case led_status::ReadyEthernet:
  case led_status::ReadyWifi:
  case led_status::Failed:
    gpio_set_level(LED_GPIO, SIMPLE_LED_ON);
    break;
  }
#endif
}

void led_blink_number(int n, uint8_t r, uint8_t g, uint8_t b)
{
  for (int i = 0; i < n; i++)
  {
#if defined(CONFIG_CALENDINK_LED_TYPE_STRIP)
    led_strip_set_pixel(led_strip, 0, r, g, b);
    led_strip_refresh(led_strip);
#elif defined(CONFIG_CALENDINK_LED_TYPE_SIMPLE)
    gpio_set_level(LED_GPIO, SIMPLE_LED_ON);
#endif
    vTaskDelay(pdMS_TO_TICKS(300));
#if defined(CONFIG_CALENDINK_LED_TYPE_STRIP)
    led_strip_clear(led_strip);
    led_strip_refresh(led_strip);
#elif defined(CONFIG_CALENDINK_LED_TYPE_SIMPLE)
    gpio_set_level(LED_GPIO, SIMPLE_LED_OFF);
#endif
    vTaskDelay(pdMS_TO_TICKS(300));
  }
  vTaskDelay(pdMS_TO_TICKS(1000));
}
