//LIBRARIES
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Adafruit_SPIDevice.h>

#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>

#include <Adafruit_GFX.h>    // Core graphics library
#include <SPI.h>       // this is needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h>      // this is needed for FT6206
#include <Adafruit_FT6206.h>

// Initialize the TFT LCD since the FT6206 uses hardware I2C (SCL/SDA)
// This object is used to get the position of where the user presses
Adafruit_FT6206 ctp = Adafruit_FT6206();

// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

// Assign human-readable names to some common 16-bit color values:
#define BLACK           0x0000
#define WHITE           0xFFFF
#define JJCOLOR         0x1CB6



#define PAGE_HOME        0
#define PAGE_INSERT      1
#define PAGE_ANALYZING   2
#define PAGE_RESULT      3
#define PAGE_READERROR   4
#define PAGE_SETTINGS    5
#define PAGE_RESULT_NICUEMERGRESP1 6
#define PAGE_RESULT_NICUEMERGRESP2 7
#define PAGE_RESULTEXT_BD 8
#define PAGE_RESULTEXT2_BD 9
#define PAGE_RESULT_PEDSSEPSIS1 10
#define PAGE_RESULT_PEDSSEPSIS2 11
#define PAGE_RESULT_MODS_1 12
#define PAGE_RESULT_MODS_2 13
#define PAGE_RESULT_PACU_VASCOMP 14
#define CSN "Pt:5030621XXXX" //generic patient CSN
#define BORDER "------------"

int page = PAGE_HOME;

void nextButton() {
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.drawRoundRect(162, 270, 70, 30, 3, BLACK);
  tft.setCursor(175, 278);
  tft.print("NEXT");
}

void homeButton() {
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.drawRoundRect(86, 270, 70, 30, 3, BLACK);
  tft.setCursor(98, 278);
  tft.print("HOME");
}

void prevButton() {
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.drawRoundRect(6, 270, 70, 30, 3, BLACK);
  tft.setCursor(18, 278);
  tft.print("PREV");
}

void printHome() {
  tft.fillScreen(WHITE);
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  
  tft.setCursor(50, 60);  tft.print("Test Menu");
  tft.setCursor(80, 90);  tft.print("i-STAT Cartridge");
  tft.setCursor(80, 110); tft.print("Settings");
}




class Cartridge {
  public:
    int numValues; //number of labs that will display on screen
    char* labNames; //a string of lab names seperated ONLY by "," - Eg: "pH,PCO2,PO2"
    char* labValues; //a string of lab values seperated ONLY by "," and respective to labNames - Eg: "7.784, 14.8, 14.7"
    char* cartType; //a string with the type of cartridge - Eg: "CG8+"

    void populate(int _numValues, char* _labNames, char* _labValues, char* _cartType);
    void printCartridge();
};

void Cartridge::populate(int _numValues, char* _labNames, char* _labValues, char* _cartType) {
  numValues = _numValues;
  labNames = _labNames;
  labValues = _labValues;
  cartType = _cartType;
}

void Cartridge::printCartridge() {
  char* nameArr[numValues];
  char* valArr[numValues];

  char* nameToken;
  char* valToken;

  const char* delimiter = ","; //used to seperate text with ","

  //for indexing arrays
  int i = 0;
  int j = 0;

  //for ensuring #labs == #vals
  int count1 = 0;
  int count2 = 0;

  //create array for labNames
  nameToken = strtok(labNames, delimiter);
  while (nameToken != NULL) {
    nameArr[i] = nameToken;
    nameToken = strtok(NULL, delimiter);
    i++;
    count1++;
  }

  //apparently each array has to be made one at a time or the whole thing explodes...
  //involves some memory issue I don't fully understand

  //create array for labValues
  valToken = strtok(labValues, delimiter);
  while (valToken != NULL) {
    valArr[j] = valToken;
    valToken = strtok(NULL, delimiter);
    j++;
    count2++;
  }

  //if there is a mismatch between # of labs and # of values, will print error
  if (count1 != count2) {
    tft.setTextColor(0xF800);
    tft.setCursor(12,30);
    tft.setTextSize(2);
    tft.fillScreen(WHITE);
    tft.print("Value Mismatch\n Check Cartridge\n Programming");
    delay(5000);
    exit(1);
  }

  int y_pos = 90;

  tft.fillScreen(WHITE);
  tft.setTextColor(BLACK);
  tft.setTextSize(2);

  if (numValues < 11) {
    tft.setCursor(42, 2);    tft.print(CSN);
    tft.setCursor(52, 22);   tft.print("XX:XX  D/M/Y"); 
    tft.setCursor(55, 42);   tft.print("i-STAT");
    tft.setCursor(145, 42);  tft.print(cartType);
    tft.setCursor(52, 55);   tft.print(BORDER);
    tft.setCursor(95, 68);  tft.println("37.0C");
  } else {
    tft.setCursor(42, 2);    tft.print(CSN);
    tft.setCursor(55, 22);   tft.print("i-STAT");
    tft.setCursor(145, 22);  tft.print(cartType);
    tft.setCursor(52, 35);   tft.print(BORDER);
    y_pos = 45;
  }




  // if (numValues > 9) {
  //   for (int i = 0; i < 8; i++) {
  //     tft.setCursor(55,y_pos);   tft.print(nameArr[i]);
  //     tft.setCursor(160, y_pos); tft.print(valArr[i]);
  //     y_pos+=20;
  //     nextButton();
      
  //     //handle touch event
  //     if (ctp.touched()) {
  //       TS_Point p = ctp.getPoint();
        
  //       //flip it around to match the screen
  //       p.x = map(p.x, 0, 240, 240, 0);
  //       p.y = map(p.y, 0, 320, 320, 0);

  //       if (p.x > 135 && p.x < 195 &&
  //           p.y > 275 && p.y < 295) {
  //             y_pos = 90;
  //             for (int i = 8; i < numValues; i++) {
  //               tft.setCursor(55,y_pos);   tft.print(nameArr[i]);
  //               tft.setCursor(160, y_pos); tft.print(valArr[i]);
  //               y_pos+=20;
  //             }
  //           }
  //     }
  //   }
  // }
  for (int i = 0; i < numValues; i++) {
    tft.setCursor(55,y_pos);   tft.print(nameArr[i]);
    tft.setCursor(160, y_pos); tft.print(valArr[i]);
    y_pos+=20;    
  }
  tft.setCursor(52, y_pos);   tft.print(BORDER);
}

void cartridgeInserted() {
  //formatting
  tft.fillScreen(WHITE);
  tft.setTextColor(BLACK);
  tft.setTextSize(2);

  tft.setCursor(46, 2);    tft.print(CSN);
  tft.setCursor(55, 75);   tft.print("Identifying");
  tft.setCursor(70, 100);  tft.print("Cartridge");
  tft.setCursor(56, 150);  tft.print("Please Wait");
  tft.setCursor(25, 300);  tft.print("Cartridge Locked");

  //arbitrary delay
  delay(3000);
}


void setup() {
  Serial.begin(9600);
  
  tft.begin();
  tft.fillScreen(WHITE);
  tft.setTextColor(BLACK);
  tft.setTextSize(3);

  tft.fillRect(48, 30, 5, 40, BLACK);
  tft.fillCircle(50, 50, 15, WHITE);
  tft.drawCircle(50,50, 15, BLACK);
  tft.fillCircle(50, 50, 8, BLACK);
  tft.fillCircle(50, 50, 7, WHITE);

  // Cartridge c;
  // c.populate(8, 
  // "pH,PaCO2,PaO2,HCO3,SaO2,a,b,c", 
  // "7.4,40,84,23,97%,10,20,30", 
  // "CG3+");
  //c.printCartridge();

  prevButton();
  nextButton();
  homeButton();


}
 
void loop() {
  

}