#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <WiFi.h>

#define SCREEN_WIDTH 127 
#define SCREEN_HEIGHT 63 
#define OLED_RESET     -1
#define SCREEN_ADDRESS 0x3C


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, OLED_RESET);

const char* ssid = "Wokwi-GUEST";
const char* password = "";



void settingsLeft(String name, uint8_t index, String menu1, String menu2="",String menu3="") {
    display.clearDisplay();
    display.fillRoundRect(0, 0, 127, 10, 3, 1);
    display.drawRect(0, 0, 127, 63, 1);
    display.setCursor(40, 1);
    display.setTextColor(BLACK, WHITE);
    display.print(name);
    display.setTextColor(WHITE);
    int len = 1;
    
    display.setCursor(10, 15);
    display.print(menu1);

    if(menu2!=""){
      display.setCursor(10, 30);
      display.print(menu2);
      len = 2;
    }
    if(menu3!=""){
      display.setCursor(10, 45);
      display.print(menu3);
      len = 3;
    }
    if(len>1){
    display.drawRoundRect(5, index==0? 12: index==1? 27: 42, 110, 15, 3, 1);
    }
    display.display();
}
void settingsRight(String name, uint8_t index, String menu1, String menu2="",String menu3="") {
    display2.clearDisplay();
    display2.fillRoundRect(0, 0, 127, 10, 3, 1);
    display2.drawRect(0, 0, 127, 63, 1);
    display2.setCursor(40, 1);
    display2.setTextColor(BLACK, WHITE);
    display2.print(name);
    display2.setTextColor(WHITE);
    int len = 1;
    
    display2.setCursor(10, 15);
    display2.print(menu1);

    if(menu2!=""){
      display2.setCursor(10, 30);
      display2.print(menu2);
      len = 2;
    }
    if(menu3!=""){
      display2.setCursor(10, 45);
      display2.print(menu3);
      len = 3;
    }
    if(len>1 && index<3){
    display2.drawRoundRect(5, index==0? 12: index==1? 27: 42, 110, 15, 3, 1);
    }
    display2.display();
}

uint8_t leftIndex =0, rightIndex =0, sleepTime = 20;

bool selection = 0, wakeonapp = true, wifion = true, decoderenabled = true;

#define up 12
#define down 14
#define left 27
#define right 26
#define ok 13

void setup() {
  Serial.begin(115200);

  Wire.begin(22,23);
  Wire1.begin(16,17);

  pinMode(up, INPUT_PULLUP);
  pinMode(down, INPUT_PULLUP);
  pinMode(left, INPUT_PULLUP);
  pinMode(right, INPUT_PULLUP);
  pinMode(ok, INPUT_PULLUP);

  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display2.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
}

#define displayMenu(t,b) "Sleep < "+(t>0?(String(t)+" S"):"Never")+" > ", b?"Wake on App: Yes":"Wake on App: NO","Reset to Deafult" 
#define audioMenu(b) b?"Decoder mode: ON": "Decoder mode: Off", "Reset tone"
#define wifiMenu(b) b?"WiFi: ON":"WiFi: Off", "Configure WiFi"
void loop() { 
  if(!digitalRead(down)){
    onDownSetting();
    //delay(50);
  }
  if(!digitalRead(up)){

    onUpSetting();
    //delay(50);
  }

  if(!digitalRead(ok)){
    onOkSetting();
    //delay(50);
  }

  if(!digitalRead(left)){
    onBackSetting();
  }

  settingsLeft("SETTINGS",leftIndex,"Display settings","Audio Settings","WiFi Settings");
  if(leftIndex==0)
  settingsRight("DISPLAY",selection?rightIndex:10, displayMenu(sleepTime,wakeonapp));

  if(leftIndex==1)
  settingsRight("AUDIO",selection?rightIndex:10, audioMenu(decoderenabled));

  if(leftIndex==2)
  settingsRight("WiFi",selection?rightIndex:10, wifiMenu(wifion));

}

void onDownSetting(){
    if(selection==0){
      leftIndex++;
      if(leftIndex>2)leftIndex=0;
    }
    else {
       rightIndex++;
      if(rightIndex>2)rightIndex=0;
    }
}

void onUpSetting(){
  if(selection==0){
      leftIndex--;
      if(leftIndex<0)leftIndex=2;
    }
    else {
       rightIndex--;
      if(rightIndex<0)rightIndex=2;
    }
}

void onBackSetting(){
  selection = 0;
}

void onOkSetting(){
      if(selection && leftIndex == 0){
      if(rightIndex==0){sleepTime+=5;if(sleepTime>60)sleepTime=0;}
      if(rightIndex==1)wakeonapp = !wakeonapp;
      if(rightIndex==2){sleepTime=20; wakeonapp=true;}
    }
      if(selection && leftIndex == 1){
      if(rightIndex==0){decoderenabled=!decoderenabled;}
      if(rightIndex==1)decoderenabled=false;
    }
      if(selection && leftIndex == 2){
      if(rightIndex==0){wifion=!wifion;}
      if(rightIndex==1){}
    }
    if(!selection)selection = 1;
}