// ATtiny85 Blink!
#include <FastLED.h>


#define NUM_LEDS 50
// #define LED_PIN 3
#define LED_TYPE NEOPIXEL
#define COLOR_ORDER GRB
#define POWER_MA 200


CRGB leds[NUM_LEDS];
CRGB ledsLeft[NUM_LEDS];

#define numCombs 6
#define numSubCombs 3

CHSV colors[numCombs][numSubCombs] = {
{
  CHSV(170, 255, 255),
  CHSV(140,200,255),
  CHSV(140,200,255)
},
{
  // CHSV(45, 255, 255),
  CHSV(45, 255, 255),
  CHSV(210,255,255),
  CHSV(210,255,255)
  // CHSV(45, 255, 255)
},
{
  CHSV(224, 200, 255),
  CHSV(0,0,255),
  CHSV(145,160,255)
  },
  {
  CHSV(0, 255, 255),
  CHSV(0,0,255),
  CHSV(224,255,255)
},
{
  CHSV(130, 255, 255),
  CHSV(0,0,255),
  CHSV(70,255,255)
},
{
  CHSV(120, 255, 255),
  CHSV(0,0,255),
  CHSV(175,255,255)
}};

CHSV colorsBlend[] = {
  CHSV(140, 255, 255),
  CHSV(190,255,255),
  CHSV(0,255,255),
  CHSV(32,255,255),
  CHSV(70,255,255)
};

// CHSV colors[] = {
//   CHSV(0, 255, 255),
//   CHSV(0,0,255),
//   CHSV(224,255,255)
// };

uint8_t colorSize = (sizeof(colorsBlend)/sizeof(colorsBlend[0]));


void setup() {
  FastLED.addLeds<LED_TYPE, 2>(leds, NUM_LEDS);
  FastLED.addLeds<LED_TYPE, 3>(ledsLeft, NUM_LEDS);
//  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  // FastLED.setMaxPowerInVoltsAndMilliamps(5,POWER_MA);
  FastLED.setBrightness(255);
  FastLED.clear();
  // Serial.begin(115200);
}


// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8( uint8_t& cur, const uint8_t target, uint8_t amount)
{
  if( cur == target) return;
  
  if( cur < target ) {
    uint8_t delta = target - cur;
    delta = scale8_video( delta, amount);
    cur += delta;
  } else {
    uint8_t delta = cur - target;
    delta = scale8_video( delta, amount);
    cur -= delta;
  }
}

// Blend one CRGB color toward another CRGB color by a given amount.
// Blending is linear, and done in the RGB color space.
// This function modifies 'cur' in place.
CRGB fadeTowardColor( CRGB& cur, const CRGB& target, uint8_t amount)
{
  nblendU8TowardU8( cur.red,   target.red,   amount);
  nblendU8TowardU8( cur.green, target.green, amount);
  nblendU8TowardU8( cur.blue,  target.blue,  amount);
  return cur;
}

// Fade an entire array of CRGBs toward a given background color by a given amount
// This function modifies the pixel array in place.
void fadeTowardColor( CRGB* L, uint16_t N, const CRGB& bgColor, uint8_t fadeAmount)
{
  for( uint16_t i = 0; i < N; i++) {
    fadeTowardColor( L[i], bgColor, fadeAmount);
  }
}

uint8_t alternatingSize = 3;


CHSV alternatingColors[3][2] = {
  {
//    Blue, Golden
    CHSV(155,200,255),
    CHSV(22,255,255)
  },
  {
//    White, Tourquiose
    CHSV(155,255,255),
    CHSV(135,255,255)
  },
  {
//    White, Pink
    CHSV(0,0,255),
    CHSV(230,255,255)
  }
};

CHSV toColor = colorsBlend[random8(colorSize)];
CHSV *toAlternatingColor = alternatingColors[random8(alternatingSize)];

bool flipper = true;

void loop() {
  EVERY_N_SECONDS(3){
    // toColor = CHSV(random8(255), 255, 255);
    toColor = colorsBlend[random8(colorSize)];
  }

  // fade all existing pixels toward bgColor by "5" (out of 255)
  fadeTowardColor( ledsLeft, NUM_LEDS, toColor, 10);
  for(uint8_t i = 0 ; i < NUM_LEDS;i++){
      if(flipper){
        leds[i] = (i % 2 == 0) ? toAlternatingColor[0] : toAlternatingColor[1];
      } else {
        leds[i] = (i % 2 == 0) ? toAlternatingColor[1] : toAlternatingColor[0];
      }
  }

  // for(uint8_t i = 0 ; i < NUM_LEDS;i++){
  //     if(flipper){
  //       ledsLeft[i] = (i % 2 == 0) ? toAlternatingColor[0] : toAlternatingColor[1];
  //     } else {
  //       ledsLeft[i] = (i % 2 == 0) ? toAlternatingColor[1] : toAlternatingColor[0];
  //     }
  // }

  EVERY_N_SECONDS(15){
    toAlternatingColor = alternatingColors[random8(alternatingSize)];
  }
  flipper = !flipper;
  FastLED.show();
  FastLED.delay(80);
}

uint8_t seqIndex = 0;
uint8_t subIndex = 0;

void loop1(){
  EVERY_N_MILLISECONDS(100){
    for(uint8_t i = 0 ; i < NUM_LEDS ; i++){
      if(i%3 == 0){
        leds[i] = colors[seqIndex][subIndex];
      }else if(i%2 == 0){
        leds[i] = colors[seqIndex][subIndex];
      }else{
        leds[i] = colors[seqIndex][subIndex];
      }
      subIndex = (subIndex + 1) % numSubCombs;
      // seqIndex = 0;
    }
  }

  // EVERY_N_SECONDS(10){
  //     seqIndex = (seqIndex + 1) % numCombs;
  //     // FastLED.clear();
  // }
  FastLED.show();
}