/*
 *  Presets color palletes
 *  
 *  Cycle from the list below
 *  
 */

#define COLLOR_PALLETES_SIZE 12 //No of color pallete defined
#define C_RED {255, 0, 0}
#define C_ORANGE {255, 165, 0}
#define C_YELLOW {255, 255, 0}
#define C_YELLOWGREEN {170, 255, 0}
#define C_GREEN {0, 255, 0}
#define C_TEAL {0, 255, 166}
#define C_TURQUOISE {64, 224, 208}
#define C_CYAN {0, 255, 255}
#define C_BLUE {0, 0, 255}
#define C_PURPLE {255, 0, 255}
#define C_LAVENDER {174, 0, 255}
#define C_PINK {255, 0, 115}


//Load the default runtime for RGB Mode
int colorPalletes[COLLOR_PALLETES_SIZE][3] = {
  C_RED, 
  C_ORANGE, 
  C_YELLOW,
  C_YELLOWGREEN, 
  C_GREEN, 
  C_TEAL, 
  C_TURQUOISE, 
  C_CYAN, 
  C_BLUE, 
  C_LAVENDER, 
  C_PURPLE, 
  C_PINK
 };

//initiate preset mode
void loadPresetsDefault() {
  //Reset paramters
  adjustingCatergory = 0;
  //Set color pallete id to 0 (red), brightness to 50%, (last byte unused)
  values[0] = 0;
  values[1] = 50;
  values[2] = 0;

  //Update the color to the LEDs
  renderColorPallete(values[0]);
}

//Handlers for add / minus button
void handlePresetsAdd() {
  if (adjustingCatergory == 0){
    //Change pallete
    values[0] = values[0] + 1;
    if (values[0] > COLLOR_PALLETES_SIZE - 1){
      values[0] = 0;
    }
  }else{
    //Change intensity
    values[1]++;
    if (values[1] > 100){
      values[1] = 100;
      blinkUpperLimit();
    }
  }
  renderColorPallete(values[0]);
}

void handlePresetsMinus(){
  if (adjustingCatergory == 0){
    //Change pallete
    values[0] = values[0] - 1;
    if (values[0] < 0){
      values[0] = COLLOR_PALLETES_SIZE - 1;
    }
  }else{
    //Change intensity
    values[1]--;
    if (values[1] < 1){
      values[1] = 1;
      blinkLowerLimit();
    }
  }
  renderColorPallete(values[0]);
}

void renderColorPallete(int palleteID){
  //Load the color pallete
    int palleteRGB[3] = {
      colorPalletes[values[0]][0], 
      colorPalletes[values[0]][1], 
      colorPalletes[values[0]][2]
    };

    //Set control LED color
    if (adjustingCatergory == 0){
      //Color pallete selection mode
      setControlLEDColor(MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS);
    }else{
      //Brightness selection mode
      setControlLEDColor(MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS, 0);
    }
    
     
    //Adjust intensity
    palleteRGB[0] = int(float(palleteRGB[0]) * float(values[1]) / 100.0);
    palleteRGB[1] = int(float(palleteRGB[1]) * float(values[1]) / 100.0);
    palleteRGB[2] = int(float(palleteRGB[2]) * float(values[1]) / 100.0);

    /*
    //Debug output
    Serial.print(palleteRGB[0]);
    Serial.print(",");
    Serial.print(palleteRGB[1]);
    Serial.print(",");
    Serial.println(palleteRGB[2]);
    */
    
    //Update light color
    setLightColor(palleteRGB[0], palleteRGB[1], palleteRGB[2]);
    delay(100);
}
