#include <FastLED.h>
#include <ArduinoJson.h>

#define NUM_LEDS 256
//****************************
#define DATA_PINA RX

// Params for width and height
const uint8_t kMatrixWidth = 32;
const uint8_t kMatrixHeight = 8;

// Param for different pixel layouts
const bool    kMatrixSerpentineLayout = false;
const bool    kMatrixVertical = true;

int hue_ = 0;

CRGB leds[NUM_LEDS];

//*****************************************************

void setup() {
  FastLED.addLeds<WS2812B, DATA_PINA, GRB>(leds, NUM_LEDS);
  fill_solid(leds, NUM_LEDS, CRGB::Black);
}

//********************************************************

void loop() {
  ShowBootLogo("[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,63519,63519,63519,63519,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,63519,0,0,0,0,2016,0,0,0,0,0,0,0,0,0,0,31,0,0,0,0,0,0,0,0,0,63488,0,63488,0,0,0,63519,0,0,0,0,2016,2016,0,0,0,65514,0,65514,0,0,0,31,0,0,0,64512,0,0,64512,0,63488,63488,0,63488,63488,0,0,63519,63519,63519,0,0,2016,0,2016,0,65514,0,65514,0,65514,0,31,31,31,0,0,0,64512,64512,0,0,63488,63488,63488,63488,63488,0,0,63519,0,0,0,0,2016,0,2016,0,65514,0,65514,0,65514,0,0,31,0,0,0,0,64512,64512,0,0,0,63488,63488,63488,0,0,0,63519,63519,63519,63519,0,2016,0,2016,0,65514,0,65514,0,65514,0,0,0,31,31,0,64512,0,0,64512,0,0,0,63488,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]");
  FastLED.show();
  delay(10);
}

//**************************************************
void ShowBootLogo(std::string text) 
{
    //this->hue_++;
    hue_++;
    if (hue_ == 360)
    {
      hue_ = 0;
    }
    // float red, green, blue;
    // esphome::hsv_to_rgb(this->hue_, 0.8, 0.8, red, green, blue);
    // this->rainbow_color = Color(uint8_t(255 * red), uint8_t(255 * green), uint8_t(255 * blue));
    CHSV rainbow_color = CHSV(hue_,255,255);

    const size_t CAPACITY = JSON_ARRAY_SIZE(256);
    StaticJsonDocument<CAPACITY> doc;
    deserializeJson(doc, text);
    JsonArray array = doc.as<JsonArray>();
    // extract the values
    uint16_t i = 0;
    uint16_t x = 0;
    uint16_t y = 0;
    for (JsonVariant v : array)
    {
      uint16_t buf = v.as<int>();

      // this->bitmap[i++] = c;
      leds[XY(x,y)] = buf > 0 ? rainbow_color : CHSV(0, 0, 0);
      x++;
      if (x >= kMatrixWidth)
      {
        x = 0;
        y++;
        if (y >= kMatrixHeight)
        {
          y = 0;
        }
      }
    }  
}

uint16_t XY( uint8_t x, uint8_t y)
{
  uint16_t i;
  
  if( kMatrixSerpentineLayout == false) {
    if (kMatrixVertical == false) {
      i = (y * kMatrixWidth) + x;
    } else {
      i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
    }
  }

  if( kMatrixSerpentineLayout == true) {
    if (kMatrixVertical == false) {
      if( y & 0x01) {
        // Odd rows run backwards
        uint8_t reverseX = (kMatrixWidth - 1) - x;
        i = (y * kMatrixWidth) + reverseX;
      } else {
        // Even rows run forwards
        i = (y * kMatrixWidth) + x;
      }
    } else { // vertical positioning
      if ( x & 0x01) {
        i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
      } else {
        i = kMatrixHeight * (kMatrixWidth - x) - (y+1);
      }
    }
  }
  
  return i;
}