/* ESP32 WiFi example */

#include "thingProperties.h"
#include <SoftTimers.h>


int remaininghours = 0;
int remainingminutes = 0;
int remainingseconds = 0;
String remainingtime = "";
int forcepumpswitch = 1;
const int RELAY_PIN = 2;
bool sensorValue = 0;
bool resettime = 0;
SoftTimer countdown;
SoftTimer water;
const unsigned long countdowninterval = 100*1000UL;
const unsigned long watertime = 5*1000UL;

void setup() {
  Serial.begin(115200);
  pinMode(15,INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);
  countdown.setTimeOutTime(countdowninterval);
  countdown.reset();
  water.setTimeOutTime(watertime);
  water.reset();
  digitalWrite(RELAY_PIN, HIGH);
//iot
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void TLC (){
      uint32_t remaining = countdown.getRemainingTime();
      int remaininghours = remaining/60/60/1000;
      int remainingminutes = remaining/60/1000;
      int remainingseconds = remaining/1000;
      sensorValue = digitalRead(15);
      water_sensor = sensorValue;
      remainingtime = ("H:" + String(remaininghours) + " M:" + String(remainingminutes - remaininghours*60) + " S:" + String(remainingseconds - remainingminutes*60));
      time_remaining = remainingtime;
      //Serial.println(remainingtime);
}


void Function1 (){
  sensorValue = digitalRead(15);
    if(sensorValue == 0){
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("No Water");
    }
    else{
  water.reset();
    while (!water.hasTimedOut()) {
    sensorValue = digitalRead(15); 
    ArduinoCloud.update();
    if(sensorValue == 0){
  //    Serial.println("Stop Watering because of no water");  
    digitalWrite(RELAY_PIN, HIGH);
    break;}
    else if(forcepumpswitch == 0){
      forcepumpswitch = 1;
      digitalWrite(RELAY_PIN, HIGH);
  //      Serial.println("Stop Watering because of switch");
      break; 
    }
    else{
  digitalWrite(RELAY_PIN, LOW);
  force_pump_switch = 1;
  //  Serial.println("Watering");
  //  delay(500);
  }}}
  forcepumpswitch = 1;
  force_pump_switch = 0;
  }

void loop() {
    TLC();
    ArduinoCloud.update();
    if (countdown.hasTimedOut()) {
    Function1 ();
    countdown.reset();
  }
  else if(forcepumpswitch == 2){
    Function1 ();
  }
  else if (resettime == 1){
    countdown.reset();
    Serial.println("Time Reset");
    resettime = 0;
  }
  else {
     digitalWrite(RELAY_PIN, HIGH);
  }
    delay(100);
}
/*
  Since TimerReset is READ_WRITE variable, onTimerResetChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTimerResetChange()  {
  // Add your code here to act upon TimerReset change
  if (timer_reset == 1){
    resettime = 1;
  }
}



/*
  Since ForcePumpSwitch is READ_WRITE variable, onForcePumpSwitchChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onForcePumpSwitchChange()  {
  // Add your code here to act upon ForcePumpSwitch change
if (force_pump_switch == 1 && forcepumpswitch == 1){
  forcepumpswitch = 2;
}
else if(force_pump_switch == 0) {
  forcepumpswitch = 0;
}
}