#include "mtbutton.h"

uint16_t g_cnt;
uint8_t g_test;
uint8_t g_oldTest = ~0;

bool g_onEntry;
MtButton btnPdwn(9, InputMode::EXPDWN);
MtButton btnPup(6);  // default InputMode::IPUP
MtButton nextTest(5);
MtButton prevTest(4);

void setup() {
    Serial.begin(115200);
    Serial.println("setup");
    g_test = 3;
    btnPdwn.begin();
    btnPup.begin();
    nextTest.begin();
    prevTest.begin();
 
}

void loop() {
  btnPdwn.read();
  btnPup.read();
  nextTest.read();
  prevTest.read();
  if (prevTest.wasPressed()) {
      g_test = (((g_test -1) % (4)) + (4)) % (4);
  } else if (nextTest.wasPressed()) {
      g_test = (g_test + 1) % (4);
  } 
  if (g_oldTest != g_test) {
      g_onEntry = true;
      g_oldTest = g_test;
  } else {
      g_onEntry = false;
  }
  switch(g_test) {
      case 0:
        if (g_onEntry) {
            Serial.println("isPressed test.");
        }
        if (btnPdwn.isPressed()) {
            if (g_cnt > 0) {
                g_cnt--;
                Serial.println(g_cnt);
            }
            
        } else if (btnPup.isPressed()) {
            g_cnt++;
            Serial.println(g_cnt);
            /*if (g_cnt > 200) {
                g_test = 1;
                g_cnt = 0;
            }*/
        }
      break;
      case 1:
        if (g_onEntry) {
            Serial.println("wasPressed() test.");
        }
        if (btnPdwn.wasPressed()) {
            Serial.println("redleft");
            g_cnt++;
        } else if (btnPup.wasPressed()) {
            Serial.println("redRight");
            g_cnt++;
        }
        /*if (g_cnt > 3) {
            g_test = 2;
            g_cnt = 0;
        }*/

      break;
      case 2:
        if (g_onEntry) {
            Serial.println("wasPressedFor() test.");
        }
        if (btnPdwn.wasPressedFor(500)) {
            Serial.println("redleft 500");
            g_cnt++;
        } else if (btnPup.wasPressedFor(1500)) {
            Serial.println("redRight 1500");
            g_cnt++;
        }
        /*if (g_cnt > 3) {
            g_cnt = 0;
            g_test = 3;
            Serial.println("rilascia il pulsante");
            delay(1000);
        }*/


      break;
    case 3:
      if (g_onEntry) {
          Serial.println("isPressed_r() test.");
      }
      if (btnPdwn.isPressed_r(100, 50)) {
        if (g_cnt > 0) {
            g_cnt--;
            Serial.println(g_cnt);
        }
            
      } else if (btnPup.isPressed_r(100, 50)) {
          g_cnt++;
         Serial.println(g_cnt);
         /*if (g_cnt > 120) {
            g_test = 2;
            g_cnt = 0;
          }*/
      }

    break;

  }
/* 
  if (btnPdwn.wasPressedFor(500)) {
    Serial.println("btnPdwn long pressed");
  } else if (btnPup.wasPressedFor(1000)) {
    Serial.println("btnPup long pressed");

  }
  */

}