#define LED1 8
#define LED2 9
#define LED3 10
#define KODA_VKLOP1 5461
#define KODA_VKLOP2 1234
#define KODA_IZKLOP1 5460
#define KODA_IZKLOP2 1230
#define KODA_UTRIPANJE_VKLOP 4545
#define KODA_UTRIPANJE_IZKLOP 4540
#define KODA_UTRIPANJE_10X 1010

#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27
#define LCD_COLUMNS 20
#define LCD_LINES   2

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
bool UTRIP3 = false;
bool utrip10x = false;
int utripCounter = 0;

unsigned long trenutniCas;
int zakasnitev = 200;

void setup() {
  Serial.begin(115200);
  Serial.println("Vpiši kodo:");
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  lcd.init();
  lcd.backlight();
}

void loop() {
  lcd.setCursor(1, 0);
  lcd.print("Trenutno Stanje:");
  if (Serial.available()) {
    char vpis[10]; // Increase the buffer size to handle longer inputs
    int steviloZnakovVpis = Serial.readBytesUntil('\n', vpis, sizeof(vpis) - 1); // Read until newline or buffer is full

    vpis[steviloZnakovVpis] = '\0'; // Null-terminate the string

    if (steviloZnakovVpis > 4) {
      Serial.println("Koda je predolga");
      lcd.setCursor(0, 1);
      lcd.print("Koda je predolga   ");
    } else if (steviloZnakovVpis == 4) {
      int stevilka = atoi(vpis); // Convert string to integer
      lcd.setCursor(0, 1);

      switch (stevilka) {
        case KODA_VKLOP1:
          Serial.println("vklop 1");
          digitalWrite(LED1, HIGH);
          lcd.print("Vklopljena LED1   ");
          break;
        case KODA_IZKLOP1:
          Serial.println("izklop 1");
          digitalWrite(LED1, LOW);
          lcd.print("Izklopljena LED1  ");
          break;
        case KODA_VKLOP2:
          Serial.println("vklop 2");
          digitalWrite(LED2, HIGH);
          lcd.print("Vklopljena LED2   ");
          break;
        case KODA_IZKLOP2:
          Serial.println("izklop 2");
          digitalWrite(LED2, LOW);
          lcd.print("Izklopljena LED2  ");
          break;
        case KODA_UTRIPANJE_VKLOP:
          Serial.println("utripanje vklop");
          UTRIP3 = true;
          lcd.print("Vklopljeno utripanje");
          break;
        case KODA_UTRIPANJE_IZKLOP:
          Serial.println("utripanje izklop");
          UTRIP3 = false;
          lcd.print("Izklopjeno utripanje");
          digitalWrite(LED3, LOW);
          break;
        case KODA_UTRIPANJE_10X:
          Serial.println("utripanje 10x");
          utrip10x = true;
          utripCounter = 0;
          lcd.print("Utripanje 10x     ");
          break;
        default:
          Serial.println("FUCK YOU, WRONG");
          lcd.print("Wrong code       ");
          break;
      }
    } else {
      Serial.println("Koda je prekratka");
      lcd.setCursor(0, 1);
      lcd.print("Koda je prekratka");
    }
  }

  utripanjeLED3();
}

void utripanjeLED3() {
  static bool m = false;
  if (((millis() - trenutniCas) > zakasnitev)) {
    trenutniCas = millis();

    if (UTRIP3) {
      m = !m;
      digitalWrite(LED3, m);
    }

    if (utrip10x) {
      m = !m;
      digitalWrite(LED3, m);
      if (!m) { // Count only when the LED turns off to get 10 complete cycles
        utripCounter++;
        if (utripCounter >= 10) {
          utrip10x = false;
          digitalWrite(LED3, LOW); // Ensure the LED is off after 10 cycles
        }
      }
    }
  }
}