#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);

// char buffer[10] ="874699";

int k= 1;
int j=0;


// char lat1[10];
// char longi1[10];

// long eas;
// long nor;
// char zone[3];
// char GL[3];

// int number = 0; // to store the number entered
// int num_len = 0; // to store the length of the entered number


Adafruit_GFX_Button seloptions[5]; // creating an array of buttons for numbers
Adafruit_GFX_Button buttons[25]; // creating an array of buttons for numbers
//Adafruit_GFX_Button buttonsalpha[27]; // creating an array of buttons for numbers
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

void setup() 
{
  Serial.begin(9600);
  tft.begin();
  homescreen();
  delay(1500);
  seloption();

  // Meme reference: https://english.stackexchange.com/questions/20356/origin-of-i-can-haz
}

void homescreen()
{
  tft.fillScreen(BLACK);
  tft.setCursor(20,100);
  tft.setTextColor(RED);
  tft.setTextSize(2);
  tft.println(F("GR CONVERTOR"));
  tft.println(F("DEVELOPED by 12 GASL"));
  tft.println(F("MILITARY SURVEY"));
}

void seloption()
{
  tft.fillScreen(BLACK);
  const char* labels2[] = {"LL-DSM" ,"ESM-DSM", " DSM-LL", "DSM-ESM", "RESET"};
  tft.setTextSize(2);
  for (j=0; j<5; j++)
  { 
  seloptions[j].initButton(&tft, j%2*100 + 60, (j/2)*100 + 80, 80, 80, WHITE, CYAN, BLACK,labels2[j], 1);
  seloptions[j].drawButton(false);
  }

  j=k;
  delay(100);
  whichoption();
  
}

void whichoption()
{
  if (j == 0) 
      { 
        //convert lat long to dsm
        DrawButtons();
        k=11;
        inputvalLL();

      }
  if (j == 1) 
      { 
        //convert esm to dsm
        ESMzonebutton();
        k=11;
        inputvalLL();


      }
  if (j == 2) 
      { 
        //convert dsm to lat long
        underdev();
      }
  if (j == 3) 
      { 
        //convert dsm to esm
        underdev();
      }
  if (j == 4) 
      { 
        //reset
      }
}

void underdev()
{
  tft.fillScreen(BLACK);
        tft.setCursor(0, 160);
        tft.setTextColor(RED);
        tft.setTextSize(3);
        tft.println(F("under development"));
        delay(1500);
        seloption();
}

void DrawButtons()
 {
  tft.fillScreen(BLACK);
  tft.setTextSize(2);
  const char* labels1[] = {"CLR" ,"E", "L/E", "L/N", "H"};
  int tc=0;
  for (int i = 0; i < 15; i++) {
    int x = i % 5 * 40 + 20;
    int y = i / 5 * 40 + 20;
    if(i < 10)
    {
    buttons[i].initButton(&tft, x, y, 20, 20, WHITE, CYAN, BLACK,String(i).c_str(), 1);
    }
    if(i > 9)
    {
    buttons[i].initButton(&tft, x, y, 20, 20, WHITE, CYAN, BLACK,labels1[tc], 1);
    tc++;
    }
    buttons[i].drawButton(false);
  }
}
void ESMzonebutton() {
  DrawButtons();
  tft.setTextSize(2);
  const char* labels[] = {"Z0", "IA", "IB", "IIA", "IIB", "IIIA", "IIIB", "IVA", "IVB","Z"};
  for (int i = 0; i < 10; i++) {
    int x = (i + 15) % 5 * 40 + 20;
    int y = (i + 15) / 5 * 40 + 20;
    buttons[i + 15].initButton(&tft, x, y, 20, 20, WHITE, CYAN, BLACK, labels[i], 1);
    buttons[i + 15].drawButton(false);
  }
}

void inputvalLL()
{
//bool down = Touch_getXY();
for (int i=0; i<15; i++) 
{
  i=k;
//buttons[i].press(down && buttons[i].contains(pixel_x, pixel_y));
//if (buttons[i].justReleased()) // Check if a button was just released
{
// if (i == 10) 
//       { // clear button
//       clearbutton();
//       }
// if (i==12)
//       {
//       //lat button
//       strcpy(lat1,buffer);
//       //clear buffer
//       number  = 0;
//       num_len = 0; // clear the buffer length
//       memset(buffer, 0, sizeof buffer); // clear the buffer
//       DrawButtons();
//       displayResult();
//       }
// if (i==13)
//       {
//       //long button
//       strcpy(longi1,buffer);
//       DrawButtons();
//       displayResult();
//       }
if (i == 11) 
      {
      //enter button
      //convertlatlongtoGR();
      DrawButtons();
      displayResult();
      }

// if(i<10 and num_len < 6) 
//       { // limit number length to 6 digits
//       number = number * 10 + i; // add the digit to the number
//       buffer[num_len++] = '0' + i; // add the digit to the buffer
//       buffer[num_len] = '\0'; // null terminate the string
//       displayResult();
//       }
break;
}
}
}
void displayResult()
{
tft.fillRect(0,200,tft.width(), 200, RED);
tft.setCursor(0,200);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.println("buffer");
tft.print("ENTER LAT");tft.print(" ");tft.println("lat1");
tft.print("ENTER Long");tft.print(" ");tft.println("longi1");
tft.print("E");tft.print(" ");tft.println("eas");
tft.print("N");tft.print(" ");tft.println("nor");
tft.print("GRID LETTER");tft.print(" ");tft.println("GL");
tft.print("Zone");tft.print(" ");tft.println("zone");

}
void loop() 
{

}