// https://wokwi.com/projects/new/arduino-nano

#include <LiquidCrystal_PCF8574.h>

#define Dn_Pin 4
#define Up_Pin 5

LiquidCrystal_PCF8574 lcd(0x27); //lcd(0x3F);

byte menu = 0;
// byte prev_menu = 0;
byte last_menu = 0;
char prev_dir = '-';

char msg[40];

String settingsMenu[][2] ={
  {"Settings"},
  {" Select Mode      ", " Game: Test Targets: Practice"}, //Practice == Training
  {" Wireless Control ", " Tower: Server Room"},
  {" Sound Control    ", " ON: OFF"},
  {" LCD Light Control", " ON: OFF"},
  {" Motors Control   ", " Auto: OFF: Auto 1: Auto 2"}
};

// settings parameters to be saved...


const int MAX_MENU_ITEMS = sizeof(settingsMenu) / sizeof(settingsMenu[0]);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(Dn_Pin, INPUT_PULLUP);
  pinMode(Up_Pin, INPUT_PULLUP);

  lcd.begin(20,4);
  lcd.setBacklight(1);

  //MAX_MENU_ITEMS%3 > 0 ? screens = MAX_MENU_ITEMS/3+1 : screens = MAX_MENU_ITEMS/3;

  sprintf(msg, "MAX_MENU_ITEMS: %i\tReady.", MAX_MENU_ITEMS-1);
  Serial.println(msg);

  lcd.setCursor(0,0);
  lcd.print(settingsMenu[0][0]);

  lcd.setCursor(0,1);lcd.print(settingsMenu[1][0]);
  lcd.setCursor(0,1);lcd.write('>');
  lcd.setCursor(0,2);lcd.print(settingsMenu[2][0]);
  lcd.setCursor(0,3);lcd.print(settingsMenu[3][0]);
  menu = 1;

  // lcd.setCursor(18,0);
  // lcd.print(menu);

  // lcd.setCursor(19,0);
  // lcd.write(prev_dir);

}

void loop() {
  if(digitalRead(Dn_Pin) == LOW){
    while(digitalRead(Dn_Pin) == LOW);
    
    menu++;
    if(menu == MAX_MENU_ITEMS) menu = 5;

    // sprintf(msg, "menu = %i\ti = %i", menu, menu%3+1);
    // Serial.println(msg);
    
    // Serial.println(settingsMenu[0][0]);

    // lcd.setCursor(18,0);
    // lcd.print(menu);
    // delay(700);

    switch (menu){
      case 1:
        lcd.setCursor(0,1);lcd.print(settingsMenu[1][0]);
        lcd.setCursor(0,1);lcd.write('>');
        lcd.setCursor(0,2);lcd.print(settingsMenu[2][0]);
        lcd.setCursor(0,3);lcd.print(settingsMenu[3][0]);
        last_menu = 3;
        break;

      case 2:
        lcd.setCursor(0,1);lcd.print(settingsMenu[1][0]);
        lcd.setCursor(0,2);lcd.print(settingsMenu[2][0]);
        lcd.setCursor(0,2);lcd.write('>');
        lcd.setCursor(0,3);lcd.print(settingsMenu[3][0]);
        last_menu = 3;
        break;
      
      case 3:
        if(prev_dir == 'D'){
          lcd.setCursor(0,1);lcd.print(settingsMenu[1][0]);
          lcd.setCursor(0,2);lcd.print(settingsMenu[2][0]);
          lcd.setCursor(0,3);lcd.print(settingsMenu[3][0]);
          lcd.setCursor(0,3);lcd.write('>');
          last_menu = 3;
        }
        if(prev_dir == 'U'){
          if(last_menu == 4){
            lcd.setCursor(0,1);lcd.print(settingsMenu[2][0]);
            lcd.setCursor(0,2);lcd.print(settingsMenu[3][0]);
            lcd.setCursor(0,2);lcd.write('>');
            lcd.setCursor(0,3);lcd.print(settingsMenu[4][0]);
            last_menu = 4;
          }
          else{
            lcd.setCursor(0,1);lcd.print(settingsMenu[1][0]);
            lcd.setCursor(0,2);lcd.print(settingsMenu[2][0]);
            lcd.setCursor(0,3);lcd.print(settingsMenu[3][0]);
            lcd.setCursor(0,3);lcd.write('>');
            last_menu = 3;
          }
        }
        break;

      case 4:
        if(prev_dir == 'D'){
          lcd.setCursor(0,1);lcd.print(settingsMenu[2][0]);
          lcd.setCursor(0,2);lcd.print(settingsMenu[3][0]);
          lcd.setCursor(0,3);lcd.print(settingsMenu[4][0]);
          lcd.setCursor(0,3);lcd.write('>');
          last_menu = 4;
        }
        if(prev_dir == 'U'){
          if(last_menu == 4){
            lcd.setCursor(0,1);lcd.print(settingsMenu[2][0]);
            lcd.setCursor(0,2);lcd.print(settingsMenu[3][0]);
            lcd.setCursor(0,3);lcd.print(settingsMenu[4][0]);
            lcd.setCursor(0,3);lcd.write('>');
            last_menu = 4;
          }else{
            lcd.setCursor(0,1);lcd.print(settingsMenu[3][0]);
            lcd.setCursor(0,2);lcd.print(settingsMenu[4][0]);
            lcd.setCursor(0,2);lcd.write('>');
            lcd.setCursor(0,3);lcd.print(settingsMenu[5][0]);
            last_menu = 5;
          }
        }
        break;

      case 5:
        lcd.setCursor(0,1);lcd.print(settingsMenu[3][0]);
        lcd.setCursor(0,2);lcd.print(settingsMenu[4][0]);
        lcd.setCursor(0,3);lcd.print(settingsMenu[5][0]);
        lcd.setCursor(0,3);lcd.write('>');
        last_menu = 5;
        break;
    }

    // Serial.println("");
    prev_dir = 'D';
    //prev_menu = menu;

    // lcd.setCursor(19,0);
    // lcd.write(prev_dir);

    // lcd.setCursor(18,1);
    // lcd.print(prev_menu);
    // lcd.setCursor(19,1);
    // lcd.print(last_menu);
    
  }


  if(digitalRead(Up_Pin) == LOW){
    while(digitalRead(Up_Pin) == LOW);

    if(menu == 0) menu = 1;
    if(menu > 1) menu--;

    
    // sprintf(msg, "menu = %i\ti = %i", menu, menu%3+1);
    // Serial.println(msg);
    
    // Serial.println(settingsMenu[0][0]);

    // lcd.setCursor(18,0);
    // lcd.print(menu);
    //delay(700);

    switch (menu){
      case 1:
        lcd.setCursor(0,1);lcd.print(settingsMenu[1][0]);
        lcd.setCursor(0,1);lcd.write('>');
        lcd.setCursor(0,2);lcd.print(settingsMenu[2][0]);
        lcd.setCursor(0,3);lcd.print(settingsMenu[3][0]);
        last_menu = 3;
        break;

      case 2:
        if(prev_dir == 'D'){
          if(last_menu == 4){
            lcd.setCursor(0,1);lcd.print(settingsMenu[2][0]);
            lcd.setCursor(0,1);lcd.write('>');
            lcd.setCursor(0,2);lcd.print(settingsMenu[3][0]);
            lcd.setCursor(0,3);lcd.print(settingsMenu[4][0]);
            last_menu = 4;
          }
          else{
            lcd.setCursor(0,1);lcd.print(settingsMenu[1][0]);
            lcd.setCursor(0,2);lcd.print(settingsMenu[2][0]);
            lcd.setCursor(0,2);lcd.write('>');
            lcd.setCursor(0,3);lcd.print(settingsMenu[3][0]);
            last_menu = 3;
          }
        }
        if(prev_dir == 'U'){
          lcd.setCursor(0,1);lcd.print(settingsMenu[2][0]);
          lcd.setCursor(0,1);lcd.write('>');
          lcd.setCursor(0,2);lcd.print(settingsMenu[3][0]);
          lcd.setCursor(0,3);lcd.print(settingsMenu[4][0]);
          last_menu = 4;
        }
        break;
      
      case 3:
        if(prev_dir == 'D'){
          if(last_menu == 5){
            lcd.setCursor(0,1);lcd.print(settingsMenu[3][0]);
            lcd.setCursor(0,1);lcd.write('>');
            lcd.setCursor(0,2);lcd.print(settingsMenu[4][0]);
            lcd.setCursor(0,3);lcd.print(settingsMenu[5][0]);
            last_menu = 5;
          }
          else{
            lcd.setCursor(0,1);lcd.print(settingsMenu[2][0]);
            lcd.setCursor(0,2);lcd.print(settingsMenu[3][0]);
            lcd.setCursor(0,2);lcd.write('>');
            lcd.setCursor(0,3);lcd.print(settingsMenu[4][0]);
            last_menu = 4;
          }
        }
        if(prev_dir == 'U'){
          lcd.setCursor(0,1);lcd.print(settingsMenu[3][0]);
          lcd.setCursor(0,1);lcd.write('>');
          lcd.setCursor(0,2);lcd.print(settingsMenu[4][0]);
          lcd.setCursor(0,3);lcd.print(settingsMenu[5][0]);
          last_menu = 5;
        }
        break;

      case 4:
        lcd.setCursor(0,1);lcd.print(settingsMenu[3][0]);
        lcd.setCursor(0,2);lcd.print(settingsMenu[4][0]);
        lcd.setCursor(0,2);lcd.write('>');
        lcd.setCursor(0,3);lcd.print(settingsMenu[5][0]);
        last_menu = 5;
        break;

      case 5:
        lcd.setCursor(0,1);lcd.print(settingsMenu[3][0]);
        lcd.setCursor(0,2);lcd.print(settingsMenu[4][0]);
        lcd.setCursor(0,3);lcd.print(settingsMenu[5][0]);
        lcd.setCursor(0,3);lcd.write('>');
        last_menu = 5;
        break;
    }

    // Serial.println("");
    prev_dir = 'U';
    //prev_menu = menu;

    // lcd.setCursor(19,0);
    // lcd.write(prev_dir);

    // lcd.setCursor(18,1);
    // lcd.print(prev_menu);
    // lcd.setCursor(19,1);
    // lcd.print(last_menu);

  }

}