// Pin definitions
const int LED_PIN = 12;
const int BUTTON_1_PIN = 5;
const int BUTTON_2_PIN = 19;
const int POT_PIN = 25;

// Student number last two digits
const int brightness_percentage = 83;

// State definitions
const int OFF = 0;
const int ON = 1;
const int ADJUSTABLE = 2;

// Current state
int state = OFF;

// Potentiometer scaling
const int POT_MIN = 0;
const int POT_MAX = 4095;
const int LED_MIN = 0;
const int LED_MAX = 255;

// Helper variables
unsigned long button1PressTime = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_1_PIN, INPUT_PULLUP);
  pinMode(BUTTON_2_PIN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int button1State = digitalRead(BUTTON_1_PIN);
  int button2State = digitalRead(BUTTON_2_PIN);

  // Handle button 1 presses
  if (button1State == LOW) {
    if (button1PressTime == 0) {
      // First press
      button1PressTime = millis();
    } else if (millis() - button1PressTime > 1000) {
      // Long press, print help text
      Serial.println("Help text goes here");
      button1PressTime = 0;
    }
  } else {
    if (button1PressTime != 0 && millis() - button1PressTime < 1000) {
      // Short press, toggle state
      button1PressTime = 0;
      if (state == ON || state == ADJUSTABLE) {
        state = OFF;
        analogWrite(LED_PIN, 0);
      } else {
        state = ON;
        analogWrite(LED_PIN, (brightness_percentage / 100.0) * 255);
      }
    }
  }

  // Handle button 2 presses
  if (button2State == LOW) {
    if (state == ON) {
      // Enter adjustable mode
      state = ADJUSTABLE;
    } else if (state == ADJUSTABLE) {
      // Exit adjustable mode
      state = ON;
      analogWrite(LED_PIN, (brightness_percentage / 100.0) * 255);
    }
  }

  // Handle potentiometer in adjustable mode
  if (state == ADJUSTABLE) {
    int potValue = analogRead(POT_PIN);
    int ledValue = map(potValue, POT_MIN, POT_MAX, LED_MIN, LED_MAX);
    analogWrite(LED_PIN, ledValue);
  }

  // Handle serial input
  if (Serial.available()) {
    char c = Serial.read();
    if (c == 'v') {
      Serial.print("Button 1: ");
      Serial.print(button1State);
      Serial.print(", Button 2: ");
      Serial.print(button2State);
      if (state == ADJUSTABLE) {
        int potValue = analogRead(POT_PIN);
        Serial.print(", Potentiometer: ");
        Serial.print(potValue);
      }
      Serial.print(", LED: ");
      Serial.println(analogRead(LED_PIN));
    } else if (c == 'o') {
      state = OFF;
      analogWrite(LED_PIN, 0);
    }
  }
}