#include <Wire.h> //include Wire.h library
#include <Arduino.h>

#include <WiFiMulti.h>

#include <HTTPClient.h>

#define USE_SERIAL Serial

WiFiMulti wifiMulti;

 
void setup(){
  USE_SERIAL.begin(115200);

  USE_SERIAL.println();
  USE_SERIAL.println();
  USE_SERIAL.println();

  for(uint8_t t = 4; t > 0; t--) {
      USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
      USE_SERIAL.flush();
      delay(1000);
  }

  wifiMulti.addAP("OSU_Access", "");

  int myInput = 0;
  int rp = 12;
  int gp = 13;
  int bp = 14;
  pinMode(rp, OUTPUT);//red
  pinMode(bp, OUTPUT);//green
  pinMode(gp, OUTPUT);//blue

  digitalWrite(rp, LOW);
  digitalWrite(gp, LOW);
  digitalWrite(bp, LOW);
}

void flashCOLOR(int dTime,int p, int p2){

    digitalWrite(p, HIGH);
    if (p2 != 0){
      digitalWrite(p2, HIGH);
    }
    //Serial.println("Light flash red");
    delay(dTime);
    digitalWrite(p, LOW);
    if (p2 != 0){
      digitalWrite(p2, LOW);
    }
    delay(dTime);
  }

void myBlink(int Ipt,int dT,int P,int P2){
  if(Ipt > 0){
   for(int i=0; i<Ipt; i++){
      flashCOLOR(dT,P, P2);
   }
  } else{
    Serial.println("Error: no. of calls MUST be more than 0.");
  }
}

void loop(){

  int myInput;
  int maxCall = 0;
  int rp = 12;
  int gp = 13;
  int bp = 14;
  int P;
  int P2;
  
  if((wifiMulti.run() == WL_CONNECTED)) {

      HTTPClient http;

      USE_SERIAL.print("[HTTP] begin...\n");
      // configure traged server and url
      //http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
      http.begin("https://web.engr.oregonstate.edu/~rivasmig/finalAssignmentOUTPUT.php"); //HTTP

      USE_SERIAL.print("[HTTP] GET...\n");
      // start connection and send HTTP header
      int httpCode = http.GET();
      Serial.println(httpCode);//

      // httpCode will be negative on error
      if(httpCode > 0) {
          // HTTP header has been send and Server response header has been handled
          USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

          // file found at server
          if(httpCode == HTTP_CODE_OK) {
              String payload = http.getString();
              Serial.println("Payload is: ");
              USE_SERIAL.println(payload);
              myInput = payload.toInt();
          }
      } else {
          USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }
      http.end();
  }
    
  delay(2500);
  if (myInput < 5){  //Red
    P = rp;
    P2 = 0;
  }
  if (5 <= myInput && myInput < 9){  //Magenta
    P = rp;
    P2 = bp;
  }
  if (9 <= myInput && myInput < 13){ //Blue
    P = bp;
    P2 = 0;
  }
  if (13 <= myInput && myInput < 17){ //Cyan
    P = bp;
    P2 = gp;
  }
  if (17 <= myInput && myInput < 21){ //Yellow
    P = gp;
    P2 = rp;
  }
  if (21 <= myInput){ //Green
    P = gp;
    P2 = 0;
  }
  if (myInput > 50){ //Fail safe, if more than 50 calls recieved, show warning blink
    P = rp;
    P2 = 0;
    for(int i=0; i<3; i++){
      myBlink(5,70,P, P2);
      delay(700);
    }
    Serial.println("More than 50 calls taken today!");
    maxCall = 1;
    delay(1000);
  }

  if(maxCall == 0){
    myBlink(myInput,(200-(2 * myInput)),P, P2);
    delay(100);
  }

  Serial.println("Total calls taken today: " + String(myInput) + "\n");
}
  
