
void handleButtonLogic() {
  bool modePressed = (digitalRead(BUTTON_MODE) == LOW);
  bool addPressed = (digitalRead(BUTTON_ADD) == LOW);
  bool colorPressed = (digitalRead(BUTTON_COLOR) == LOW);
  bool minusPressed = (digitalRead(BUTTON_MINUS) == LOW);

  if (addPressed) {
    //Add button pressed
    HandleAddButtonPress();
    delay(BUTTON_AUTOINC_DELAY);
    while (digitalRead(BUTTON_ADD) == LOW) {
      //Delay auto-shift
      HandleAddButtonPress();
      delay(BUTTON_HOLD_DELAY);
    }
    delay(BUTTON_DEBOUNCE);
  } else if (minusPressed) {
    //Minus button pressed
    HandleMinusButtonPress();
    delay(BUTTON_AUTOINC_DELAY);
    while (digitalRead(BUTTON_MINUS) == LOW) {
      //Delay auto-shift
      HandleMinusButtonPress();
      delay(BUTTON_HOLD_DELAY);
    }
    delay(BUTTON_DEBOUNCE);
  } else if (colorPressed) {
    //Color button pressed
    HandleColorButtonPress();
    delay(BUTTON_DEBOUNCE);
    while (digitalRead(BUTTON_COLOR) == LOW) {
      //Wait for button up before continue
      delay(BUTTON_HOLD_DELAY);
    }
    //Debounce
    delay(BUTTON_DEBOUNCE);

  } else if (modePressed) {
    HandleModeButtonPress();
    while (digitalRead(BUTTON_MODE) == LOW) {
      //Wait for button up before continue
      delay(BUTTON_HOLD_DELAY);
    }
    //Debounce
    delay(BUTTON_DEBOUNCE);
  }

}

//Mode button is pressed
void HandleModeButtonPress() {
  //Update the mode number
  currentMode += 1;
  if (currentMode > 2) {
    currentMode = 0;
  }

  //Current mode updated
  //Reset the RGB values to default of each modes
  if (currentMode == 0) {
    Serial.println("Switched to White Balanced Mode");
    loadWhiteModeDefault();
  } else if (currentMode == 1) {
    Serial.println("Switched to RGB Mode");
    loadRGBModeDefault();
  } else if (currentMode == 2) {
    Serial.println("Switched to Color Palette Mode");
    loadPresetsDefault();
  }

}

//Color button is pressed
void HandleColorButtonPress() {
  if (currentMode == 0) {
    //White mode
    //Only accept 0 (temp) or 1 (brightness)
    if (adjustingCatergory == 0) {
      adjustingCatergory = 1;
      setControlLEDColor(MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS, 0);
    } else {
      adjustingCatergory = 0;
      //Set control LED to white
      setControlLEDColor(MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS);
    }
  } else if (currentMode == 1) {
    //RGB Adjustable Mode
    if (adjustingCatergory == 0) {
      //Red -> Green
      adjustingCatergory = 1;
      setControlLEDColor(0, MAX_CTRLBRIGHTNESS, 0);
    } else if (adjustingCatergory == 1) {
      //Green -> Blue
      adjustingCatergory = 2;
      setControlLEDColor(0, 0, MAX_CTRLBRIGHTNESS);
    } else if (adjustingCatergory == 2) {
      //Blue -> Red
      adjustingCatergory = 0;
      //Set LED to yellow for brightness
      setControlLEDColor(MAX_CTRLBRIGHTNESS, 0, 0);
    }
  } else if (currentMode == 2){
    //Color pallete mode
    if (adjustingCatergory == 0) {
      //Enter brightness control mode
      adjustingCatergory = 1;
      setControlLEDColor(MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS, 0);
    }else{
      //Enter pallete select mode
      adjustingCatergory = 0;
      renderColorPallete(values[0]);
    }
  }
}

void HandleAddButtonPress() {
  if (currentMode == 0) {
    //White Color Mode
    if (adjustingCatergory == 0) {
      //add 500 to K value
      values[0] = values[0] + 100;
      if (values[0] > 10000) {
        values[0] = 10000;
        blinkUpperLimit();
      }
      Serial.print("Updating color temperature to ");
      Serial.println(values[0]);
    } else if (adjustingCatergory == 1) {
      values[1] += 1;
      if (values[1] > MAX_BRIGHTNESS) {
        values[1] = MAX_BRIGHTNESS;
        blinkUpperLimit();
      }
      Serial.print("Updating brightness to ");
      Serial.println(values[1]);
    }
    setColorTemperature(values[0], values[1]);
  } else if (currentMode == 1) {
    //RGB Color Mode 
    handleRGBModeAdd();
  } else if (currentMode == 2){
    //Color Pallete Mode
    handlePresetsAdd();
  }
}

void HandleMinusButtonPress() {
  if (currentMode == 0) {
    //White Color Mode
    if (adjustingCatergory == 0) {
      //reduce 500 from K value
      values[0] = values[0] - 100;
      if (values[0] < 2500) {
        values[0] = 2500;
        blinkLowerLimit();
      }

      Serial.print("Updating color temperature to ");
      Serial.println(values[0]);
    } else if (adjustingCatergory == 1) {
      values[1] -= 1;
      if (values[1] < 0) {
        values[1] = 0;
        blinkLowerLimit();
      }
      Serial.print("Updating brightness to ");
      Serial.println(values[1]);
    }
    setColorTemperature(values[0], values[1]);
  } else if (currentMode == 1) {
    //RGB Color Mode 
    handleRGBModeMinus();
  } else if (currentMode == 2){
    //Color Pallete Mode
    handlePresetsMinus();
  }
}
