// Project includes
#include "types.hpp"

// SDK Includes
#include "led_strip.h"

// Could be a config but its the GPIO on my ESP32-S3-ETH
#define LED_GPIO GPIO_NUM_21

enum class led_status : uint8
{
  ConnectingEthernet,
  ConnectingWifi,
  ReadyEthernet,
  ReadyWifi,
  Failed
};

internal led_strip_handle_t led_strip;

internal void setup_led(void)
{
  /* 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);
}

internal void destroy_led(void) { led_strip_clear(led_strip); }

internal void set_led_status(led_status status)
{
  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);
}
#if CONFIG_CALENDINK_BLINK_IP
internal void led_blink_number(int n, uint8_t r, uint8_t g, uint8_t b)
{
  if (n <= 0)
  {
    for (int i = 0; i < 2; i++)
    {
      led_strip_set_pixel(led_strip, 0, r, g, b);
      led_strip_refresh(led_strip);
      vTaskDelay(pdMS_TO_TICKS(50));
      led_strip_clear(led_strip);
      led_strip_refresh(led_strip);
      vTaskDelay(pdMS_TO_TICKS(50));
    }
    vTaskDelay(pdMS_TO_TICKS(1000));
    return;
  }
  for (int i = 0; i < n; i++)
  {
    led_strip_set_pixel(led_strip, 0, r, g, b);
    led_strip_refresh(led_strip);
    vTaskDelay(pdMS_TO_TICKS(300));
    led_strip_clear(led_strip);
    led_strip_refresh(led_strip);
    vTaskDelay(pdMS_TO_TICKS(300));
  }
  vTaskDelay(pdMS_TO_TICKS(1000));
}
#endif
