#define BLYNK_PRINT SerialUSB
#include <Adafruit_CircuitPlayground.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <workMonitoring.h>
#include <light_and_sound_fire_Alert.h>
#include <electronics_control.h>


char ssid[] = "NETWORK_NAME";
char pass[] = "PASSWORD";
char auth[] = "BLYNK TOKEN";

#define EspSerial Serial1
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

bool registerReq = false;
bool neddToLock = false;
bool managerIsBusy= false;
bool electronicsTured = false;

void setup() {
  CircuitPlayground.begin();
  SerialUSB.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  CircuitPlayground.irReceiver.enableIRIn();
}

void loop() {
  Blynk.run();
  // someone wants to register 
  if (CircuitPlayground.leftButton()){
    registerReq = true;
  }
  if(registerReq){
    String workerID =  getWorkerID();
    Serial.println("sending id...");
    //https to V1 pin via blynk 
    Blynk.virtualWrite(V1,workerID);
    //get ready to the next worker
    registerReq = false;
    
  }
  if(neddToLock){
    //activating sound,light,temp sensors
    int reason = lockOffice();
    Serial.print("reason: ");
    Serial.println(reason);
    // if there is some interrupt- light(1),noise(2), fire(3)
    if(reason != 0){
      Blynk.virtualWrite(V3,reason);
      neddToLock=false; 
    }
  }
  // if the office is not locked checking what is the managers status
  if(!neddToLock){
    notifyBusy(managerIsBusy);    
  }
}

// incoming https req to lock the office
BLYNK_WRITE(V0) {
  Serial.println("lock office");
  neddToLock = true;
  for(int i =0; i < 10; i++){
      CircuitPlayground.setPixelColor(i,255,0,0);
  }
  delay(2000);
  for(int i =0; i < 10; i++){
      CircuitPlayground.setPixelColor(i,0,0,0);
  }
}

// incoming https req to unlock the office
BLYNK_WRITE(V9) {
  Serial.println("unlock office");
  neddToLock = false;
  for(int i =0; i < 10; i++){
      CircuitPlayground.setPixelColor(i,0,255,0);
  }
  delay(2000);
  for(int i =0; i < 10; i++){
      CircuitPlayground.setPixelColor(i,0,0,0);
  }
}

// incoming https to tell if the manager in meeting (google calendar)
BLYNK_WRITE(V6) {
  managerIsBusy = param.asInt();
}

// if manager is busy - red lights are on else green
void notifyBusy(bool busy){
  for(int i =0; i < 10; i++){
    if(busy){
      CircuitPlayground.setPixelColor(i,255,0,0);
    }
    else{
      CircuitPlayground.setPixelColor(i,0,255,0);
    }
  }
}

// when first worker come to office TV and Lights turning on 
//and if the last worker leaves the office TV and Lights turning off
BLYNK_WRITE(V7) {
  int value = param.asInt();
  Serial.println(value);
  if(value == 1 && !electronicsTured){
    turnOnOffTv();
    turnOnOffLights();
    electronicsTured  = true;
  }
  if(value == 0 && electronicsTured){
    turnOnOffTv();
    turnOnOffLights();
    electronicsTured  = false;
  }
}
