#include <Adafruit_GFX.h>       // include Adafruit graphics library
#include <Adafruit_ILI9341.h>   // include Adafruit ILI9341 TFT library
#include <ArduinoJson.h>


#define TFT_BLACK   0x0000
#define TFT_BLUE    0x001F
#define TFT_RED     0xF800
#define TFT_GREEN   0x07E0
#define TFT_CYAN    0x07FF
#define TFT_MAGENTA 0xF81F
#define TFT_YELLOW  0xFFE0
#define TFT_WHITE   0xFFFF

#define TFT_GREY 0x2104

#define RED2RED 0
#define GREEN2GREEN 1
#define BLUE2BLUE 2
#define BLUE2RED 3
#define GREEN2RED 4
#define RED2GREEN 5

#define SWIDTH 240
#define SHEIGHT 320
#define XMIN (16 + 10)
#define XMAX (SWIDTH - 10)
#define YMIN 10
#define YMAX (SHEIGHT - 10)



//////////////////////////////ili9341
#define TFT_CS    22      // TFT CS  pin is connected to arduino pin 8
#define TFT_RST   17      // TFT RST pin is connected to arduino pin 9
#define TFT_DC    21      // TFT DC  pin is connected to arduino pin 10
// initialize ILI9341 TFT library
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
//////////////////////////////end

const String data = "[[10.02446, 76.30813], [10.02417, 76.30839], [10.02404, 76.3085], [10.0238, 76.30872], [10.02332, 76.30912], [10.02298, 76.30937], [10.02265, 76.3096], [10.02235, 76.30977], [10.02171, 76.3101], [10.02123, 76.31031], [10.02072, 76.31049], [10.02028, 76.31061], [10.01974, 76.31072], [10.01814, 76.31097], [10.01748, 76.31106], [10.01467, 76.3115], [10.01311, 76.31175], [10.01223, 76.31189], [10.01125, 76.31204], [10.00878, 76.31247], [10.00854, 76.3125], [10.0083, 76.31262], [10.00712, 76.31282], [10.00639, 76.31294], [10.00602, 76.313], [10.0058, 76.31304], [10.00514, 76.31315], [10.0051, 76.31316], [10.00505, 76.31317], [10.00499, 76.31291], [10.00492, 76.31259], [10.00485, 76.31214], [10.00484, 76.31203], [10.00478, 76.31169], [10.00477, 76.31165], [10.00455, 76.31068], [10.00407, 76.30942], [10.00406, 76.30936], [10.00398, 76.3091], [10.00395, 76.30904], [10.00337, 76.30766], [10.00299, 76.30706], [10.00267, 76.3065], [10.00248, 76.30629], [10.00244, 76.30624], [10.00243, 76.3062], [10.00244, 76.30615], [10.00247, 76.3061], [10.00252, 76.30609], [10.00257, 76.30609], [10.00261, 76.30612], [10.00264, 76.30617], [10.00264, 76.30618]]";
StaticJsonDocument<20000> doc;
float fdata[2000][2];
long flen = 0;
float xdmax = -999;
float xdmin = 999;
float ydmax = -999;
float ydmin = 999;

void setup() {
  Serial.begin(115200);
  Serial.println("ILI9341 Test!"); 
 
  tft.begin();
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(78,0);
  tft.setTextSize(2);
  tft.setTextColor(TFT_WHITE);
  tft.print("BikeNav");

  DeserializationError error = deserializeJson(doc, data);

  if (error) {
    Serial.println(error.c_str());
  }else{
    Serial.println(doc.size());
  }

  flen = doc.size();
  for(int i=0;i<doc.size();i++){
    fdata[i][0] = doc[i][1];
    fdata[i][1] = doc[i][0];
    xdmax = xdmax<fdata[i][0]?fdata[i][0]:xdmax;
    xdmin = xdmin>fdata[i][0]?fdata[i][0]:xdmin;
    ydmax = ydmax<fdata[i][1]?fdata[i][1]:ydmax;
    ydmin = ydmin>fdata[i][1]?fdata[i][1]:ydmin;
    // Serial.println(fdata[i][1],5);
  }
  Serial.println("xMax: " + String(xdmax) + " xMin: " + String(xdmin) + " yMax: " + String(ydmax) + " yMin: " + String(ydmin));

}


void loop(void) {
}