#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Keypad.h>

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

//Indication
#define StockCode_Length 8
byte data_count = 0;
char customKey;
char Data[StockCode_Length]; 

const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'-', '0', '#', 'D'}
};

byte rowPins[ROWS] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2, 1};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

char* Rack[76]= {
"280-0862", "280-0844", "280-087C", "280-0852", "280-0814", "280-0835","280-0800","280-0882","280-0893","280-0896","280-3032","280-3009","280-3040","280-2023","280-2016","280-2009","280-2030","280-2098","280-099C","280-104B","280-1058","280-3008","280-3022",
// A
"280-0968","280-0959","280-0742","280-0735","280-0927","280-0941","280-0975","280-0913","280-0934","280-0900","280-0712","280-0732","280-0729","280-0726","280-0720","280-0700","280-0724","280-0705","280-0718","280-0709","280-0996","280-0992","280-0998","280-0994","280-2064","280-2105","280-204A",
// B
"280-4487","280-4885","280-4885","280-4288","280-5119","280-478C",
// C
"280-4017","280-4540","280-4020","280-430C","280-4030","280-0311",
// D
"280-4012","280-4559","280-4015","280-4452","280-5119","280-4319",
// E
"281-1508","281-1500","281-1504","280-2501","280-4579","280-4708","280-4489","280-4490"
// F
};

String m_sInput;


void setup() {
  tft.begin();
}

void loop() 
{
  tft.setCursor(17, 50);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(4);
  tft.print("Enter CPN");


  customKey = customKeypad.getKey();
  if (customKey)
  {
    Data[data_count] = customKey; 
    tft.setCursor(50+data_count*20,90); 
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(3);
    tft.print(Data[data_count]); 

    m_sInput += Data[data_count];
  
    data_count++; 
    
    if(customKey == '#')
    {
      tft.fillScreen(ILI9341_BLACK);
      clearData();  
    }
  }
  
  if(data_count == StockCode_Length)
  {
    int ReelIndex = GetStockCodeIndex(m_sInput);
    if(ReelIndex > 0)
    {
      String sRackLocation = IndexConverter(ReelIndex);

      tft.setCursor(10,160);
      tft.setTextColor(ILI9341_PURPLE);
      tft.setTextSize(12);
      tft.print(sRackLocation); 

      delay(5000);
    }
    tft.fillScreen(ILI9341_BLACK);
    clearData();  
  }

  delay(5);

}
void clearData(){
  while(data_count !=0){
    Data[data_count--] = 0; 
    m_sInput = "";
  }
  return;
}

int GetStockCodeIndex(String StockCode)
{
  int nIndexOut=0;

  for (int i=0; i<(sizeof(Rack)/2); i++) 
  {
    if (StockCode == Rack[i]) 
    {
      nIndexOut= i+1;
      break;
    }
    delay(10);
  }
  return nIndexOut;
}

String IndexConverter(int nIndex)
{
  String sLocation ="";
  int nIndexPlus = nIndex+1;
  if(nIndexPlus >0 && nIndexPlus <=3)
  {
    sLocation = "A1";
  }
   else if(nIndexPlus >3 && nIndexPlus <=6)
  {
    sLocation = "A2";
  }
   else if(nIndexPlus >6 && nIndexPlus <=9)
  {
    sLocation = "A3";
  }
   else if(nIndexPlus >9 && nIndexPlus <=11)
  {
    sLocation = "A4";
  }
   else if(nIndexPlus >11 && nIndexPlus <=13)
  {
    sLocation = "A5";
  }
   else if(nIndexPlus >13 && nIndexPlus <=16)
  {
    sLocation = "A6";
  }
   else if(nIndexPlus >16 && nIndexPlus <=18)
  {
    sLocation = "A7";
  }
   else if(nIndexPlus >18 && nIndexPlus <=21)
  {
    sLocation = "A8";
  }
   else if(nIndexPlus >21 && nIndexPlus <=23)
  {
    sLocation = "A9";
  }
   else if(nIndexPlus >23 && nIndexPlus <=26)
  {
    sLocation = "B1";
  }
   else if(nIndexPlus >26 && nIndexPlus <=29)
  {
    sLocation = "B2";
  }
   else if(nIndexPlus >29 && nIndexPlus <=32)
  {
    sLocation = "B3";
  }
   else if(nIndexPlus >32 && nIndexPlus <=35)
  {
    sLocation = "B4";
  }
   else if(nIndexPlus >35 && nIndexPlus <=38)
  {
    sLocation = "B5";
  }
   else if(nIndexPlus >38 && nIndexPlus <=41)
  {
    sLocation = "B6";
  }
   else if(nIndexPlus >41 && nIndexPlus <=44)
  {
    sLocation = "B7";
  }
   else if(nIndexPlus >44 && nIndexPlus <=47)
  {
    sLocation = "B8";
  }
   else if(nIndexPlus >47 && nIndexPlus <=50)
  {
    sLocation = "B9";
  }
   else if(nIndexPlus == 51)
  {
    sLocation = "C10";
  }
   else if(nIndexPlus == 52)
  {
    sLocation = "C11";
  }
   else if(nIndexPlus == 53)
  {
    sLocation = "C12";
  }
   else if(nIndexPlus == 54)
  {
    sLocation = "C13";
  }
   else if(nIndexPlus == 55)
  {
    sLocation = "C14";
  }
   else if(nIndexPlus == 56)
  {
    sLocation = "C15";
  }
   else if(nIndexPlus == 57)
  {
    sLocation = "D10";
  }
   else if(nIndexPlus == 58)
  {
    sLocation = "D11";
  }
   else if(nIndexPlus == 59)
  {
    sLocation = "D12";
  }
   else if(nIndexPlus == 60)
  {
    sLocation = "D13";
  }
   else if(nIndexPlus == 61)
  {
    sLocation = "D14";
  }
   else if(nIndexPlus == 62)
  {
    sLocation = "D15";
  }
   else if(nIndexPlus == 63)
  {
    sLocation = "E10";
  }
   else if(nIndexPlus == 64)
  {
    sLocation = "E11";
  }
   else if(nIndexPlus == 65)
  {
    sLocation = "E12";
  }
   else if(nIndexPlus == 66)
  {
    sLocation = "E13";
  }
   else if(nIndexPlus == 67)
  {
    sLocation = "E14";
  }
   else if(nIndexPlus == 68)
  {
    sLocation = "E15";
  }
   else if(nIndexPlus == 69)
  {
    sLocation = "F10";
  }
   else if(nIndexPlus == 70)
  {
    sLocation = "F11";
  }
   else if(nIndexPlus == 71)
  {
    sLocation = "F12";
  }
   else if(nIndexPlus == 72)
  {
    sLocation = "F13";
  }
   else if(nIndexPlus > 72 && nIndexPlus <=74)
  {
    sLocation = "F14";
  }
   else if(nIndexPlus > 74 && nIndexPlus <=77)
  {
    sLocation = "F15";
  }
  return sLocation;
}