// from: https://www.thingiverse.com/thing:4730334

#include <FastLED.h>
#define NUM_LEDS 50
#define DATA_PIN 13
#define BUTTON1_PIN 2
#define BUTTON2_PIN 3
#define rpin 11
#define gpin 10
#define bpin 9

CRGB ONE_HIGH = CHSV( 150, 255, 200);
CRGB ONE = CHSV( 150, 255, 125);
CRGB ONE_LOW = CHSV( 150, 255, 50);

CRGB TWO_HIGH = CHSV( 15, 255, 200);
CRGB TWO = CHSV( 15, 255, 125);
CRGB TWO_LOW = CHSV( 15, 255, 50);

CRGB current = CHSV( 0, 0, 0);

CRGB leds[NUM_LEDS];

uint16_t i, j, k;
byte sum, sum2;
int brightness = 100;
boolean up = true;
int interval = 0;

void setup() {
  const int frontLights[] = {7, 8, 5};

  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

  pinMode(frontLights[0], OUTPUT);
  pinMode(frontLights[1], OUTPUT);
  pinMode(frontLights[2], OUTPUT);

  pinMode(BUTTON1_PIN, INPUT_PULLUP);
  pinMode(BUTTON2_PIN, INPUT_PULLUP);

  flicker(frontLights);
}

void loop() {
  if (brightness == 255) {
    up = false;
  }
  if (brightness == 75) {
    up = true;
  }
  if (interval++ >= 4) {
    interval = 0;
    if (up) {
      brightness += 1;
    } else {
      brightness -= 1;
    }
  }

  FastLED.setBrightness(brightness);

  //Reset button variables
  int button1 = 0;
  int button2 = 0;
  //Read both buttons
  button1 = digitalRead(BUTTON1_PIN);
  button2 = digitalRead(BUTTON2_PIN);




  //If button 1 pressed
  if (button1 == LOW) {
    //Write LED to blue
    writeLED(0, 0, 100);
    delay(20);
    writeLED(0, 0, 250);
    //Pulse the strip
    pulse(ONE_LOW, ONE, ONE_HIGH);
  }

  if (button2 == LOW) {
    writeLED(100, 130, 0);
    delay(20);
    writeLED(200, 230, 0);
    pulse(TWO_LOW, TWO, TWO_HIGH);
  }

  FastLED.show();
}

void pulse(CRGB COLOR_DOWN, CRGB COLOR, CRGB COLOR_UP) {
  fill_solid( &(leds[i]), NUM_LEDS, COLOR);
  for (int dot = 1; dot < NUM_LEDS - 3; dot++) {
    leds[dot - 1] = COLOR_DOWN;
    leds[dot] = COLOR_DOWN;
    leds[dot + 1] = COLOR_UP;
    leds[dot + 2] = COLOR_UP;
    FastLED.show();
    // clear this led for the next time around the loop
    leds[dot - 1] = COLOR;
    leds[dot] = COLOR;
    delay(15);
  }
  fill_solid( &(leds[i]), NUM_LEDS, COLOR);
}

void writeLED(int r, int g, int b) {
  analogWrite(rpin, r);
  analogWrite(gpin, g);
  analogWrite(bpin, b);
}

void flicker(int frontLights[]) {
  boolean flicker[] = {false, false , false};


  for (int i = 0; i < 3; i++) {
    if (random(10) < 3) {
      flicker[i] = true;
    }
  }

  for (int i = 45; i > 0; i -= 20) {
    for (int j = 0; j < 3; j++) {
      if (flicker[j]) {
        digitalWrite(frontLights[j], HIGH);
        delay(i * 10);
        digitalWrite(frontLights[j], LOW);
      }
    }
    delay(i * 10);
  }

  for (int i = 0; i < 3; i++) {
    digitalWrite(frontLights[i], HIGH);
  }
}