/*
  Simple "Hello World" for ILI9341 LCD

  https://wokwi.com/arduino/projects/308024602434470466
*/

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

//debounce timer
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;

//home button
const int PB_2 = 2;
int PB_2_STATE = 0;
int last_PB_2_STATE = LOW;

//increase
const int PB_3 = 3;
int PB_3_STATE = 0;
int last_PB_3_STATE = LOW;

//decrease
const int PB_4 = 4;
int PB_4_STATE = 0;
int last_PB_4_STATE = LOW;

int currentPage;
int count=0;
int sub_count = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Start");
  tft.begin();
  pinMode(PB_2, INPUT);
  pinMode(PB_3, INPUT);
  pinMode(PB_4, INPUT);

  tft.setCursor(26, 50);
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(2);
  tft.println("Home Screen Test");

  tft.setCursor(0, 160);
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(2);
  tft.println("PB_2 HOME");
  tft.println("PB_3 UP");
  tft.println("PB_4 DOWN");
delay(1000);
  home_screen();

delay(1000);
  // Meme reference: https://english.stackexchange.com/questions/20356/origin-of-i-can-haz
}

void home_screen(){
  
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(20, 10);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(4);
  tft.println("HOME");
  currentPage = 0;
  count = 0; // reset count
  sub_count = 0; //reset subcategory
}


void menu_1(){
    
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(20, 10);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(4);
  tft.println("MENU #1");
  tft.fillCircle(120, 160, 50, ILI9341_BLUE);
  currentPage = 1;
}


void menu_2(){
    
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(20, 10);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(4);
  tft.println("MENU #2");
  currentPage = 2;
}


void menu_3(){
    
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(20, 10);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(4);
  tft.println("MENU #3");
  currentPage = 3;
}

void menu_4(){
    
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(20, 10);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(4);
  tft.println("MENU #4");
  currentPage = 4;
}


void menu_5(){
    
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(20, 10);
  tft.setTextColor(ILI9341_BLUE);
  tft.setTextSize(4);
  tft.println("MENU #5");
  tft.setTextSize(6);
  tft.setTextColor(ILI9341_ORANGE);
  tft.println("HI");
  tft.println("GREGG");
  currentPage = 5;
}





void loop() { 

  int  PB_2_STATE = digitalRead(PB_2); //home
  int PB_3_STATE_read = digitalRead(PB_3); //up button
  int PB_4_STATE_read = digitalRead(PB_4); // down button


//increase count
if(PB_3_STATE_read != last_PB_3_STATE){
      lastDebounceTime = millis();
}

if((millis() - lastDebounceTime) > debounceDelay) {

  if(PB_3_STATE_read != PB_3_STATE) {
    PB_3_STATE = PB_3_STATE_read;

    if(PB_3_STATE == HIGH){
      count++;
      sub_count++; // sub category counting increase
      Serial.print(count);
      Serial.print("          ");
      Serial.println(sub_count);
    }
  }
}


// decrease count
if(PB_4_STATE_read != last_PB_4_STATE){
      lastDebounceTime = millis();
}

if((millis() - lastDebounceTime) > debounceDelay) {

  if(PB_4_STATE_read != PB_4_STATE) {
    PB_4_STATE = PB_4_STATE_read;

    if(PB_4_STATE == HIGH){
      count--;
      sub_count--; //sub category counting decrease
      Serial.print(count);
      Serial.print("          ");
      Serial.println(sub_count);
    }
  }
}

last_PB_3_STATE = PB_3_STATE_read;
last_PB_4_STATE = PB_4_STATE_read;

//go to home screen
if((currentPage != 0) && (PB_2_STATE == HIGH)){
      home_screen();
      //currentPage = 1;
      Serial.println("HOME SCREEN (page 0)");  
      Serial.println(count);
  }else{}

//go to home screen
  if((count == 0) && (currentPage != 0)){
      home_screen();
      Serial.println("HOME SCREEN *(page0)");    
  }else{}


  //go to menu 1
  if((count == 1) && (currentPage != 1)){
      menu_1();
      Serial.println("Menu Option 1 *(page 1)");    
  }else{}

 //go to menu 2
  if((count == 2) && (currentPage != 2)){
      menu_2();
      Serial.println("Menu Option 2 *(page 2)");    
  }else{}

 //got to menu 3   
  if((count == 3) && (currentPage != 3)){
      menu_3();
      Serial.println("Menu Option 3 *(page 3)");    
  }else{}

//go to menu 4  
  if((count == 4) && (currentPage != 4)){
      menu_4();
      Serial.println("Menu Option 4 *(page 4)");    
  }else{}

//go to menu 5
  if((count == 5) && (currentPage != 5)){
      menu_5();
      Serial.println("Menu Option 5 *(page 5)");    
  }else{}

//max count
  if(count == 6){
    count--;
    Serial.println(count);
    Serial.println("MAX SCREEN");
  }
//min count
  if(count == -1){
    count++;
    Serial.println(count);
    Serial.println("MIN SCREEN");
  }







}