#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif

#include "conn.h"

#include "ThingsBoard.h"
#include <ESP32Servo.h>
Servo myservoIn;
Servo myservoOut;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

const int In=19, Out=25;
const int P1=4, P2=15, P3=13, P4=14;
const int L1=5, L2=2, L3=12, L4=27, GATEIn=18, GATEOut=26;

int S1=0, S2=0, S3=0, S4=0 ;
int flag1=0, flag2=0; 
int slot = 4;
int total;
int countSlot;
bool flag = false;

#define WIFI_SSID     "Wokwi-GUEST"
#define WIFI_PASSWORD ""

#define TOKEN               "DSztMpAALiglz5GZmWr2"
#define THINGSBOARD_SERVER  "thingsboard.cloud"

WiFiClient espClient;
ThingsBoard tb(espClient);

int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(115200);
  pinMode(In, INPUT); pinMode(Out, INPUT);
  pinMode(P1, INPUT); pinMode(P2, INPUT); pinMode(P3, INPUT); pinMode(P4, INPUT);
  pinMode(L1, OUTPUT); pinMode(L2, OUTPUT); pinMode(L3, OUTPUT); pinMode(L4, OUTPUT);
  pinMode(GATEIn, OUTPUT); pinMode(GATEOut, OUTPUT);
  myservoIn.attach(GATEIn);
  myservoIn.write(0); //set 0 derajat
  myservoOut.attach(GATEOut);
  myservoOut.write(0); //set 0 derajat
  lcd.init();
  lcd.backlight();
  lcd.begin(20, 4);  
  lcd.setCursor (0,1);
  lcd.print("    Car  Parking  ");
  lcd.setCursor (0,2);
  lcd.print("       System     ");
  delay (1000);
  lcd.clear();
  connect();
  // readSensor();
  // int total = S1+S2+S3+S4;
  // slot = slot-total;
  // Serial.print(String(total) + " Total pada setup \t");
  // Serial.print(String(slot) + " Slot pada setup \n");
}

void loop() {
  lcd.setCursor (0,0);
  lcd.print("   Sisa Slot: "); 
  lcd.print(slot);
  // Serial.print(String(slot) + " Slot dalam void loop \n");
  lcd.print("    ");

  // if (WiFi.status() != WL_CONNECTED) {
  //   reconnect();
  // }

  // if (!tb.connected()) {
  //   Serial.print("Connecting to: ");
  //   Serial.print(THINGSBOARD_SERVER);
  //   Serial.print(" with token ");
  //   Serial.println(TOKEN);
  //   if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
  //     Serial.println("Failed to connect");
  //     return;
  //   }
  // }
  // Serial.println("Sending data...");

  delay(2000);

  // readSensor();
  
  // tb.sendTelemetryInt("P1", digitalRead(P1));
  // tb.sendTelemetryInt("P2", digitalRead(P2));
  // tb.sendTelemetryInt("P3", digitalRead(P3));
  // tb.sendTelemetryInt("P4", digitalRead(P4));

  if(digitalRead(In)==1){
    countSlot = countSlot+1;
    if(countSlot > 4){
      lcd.setCursor (0,0);
      lcd.print(" Sorry Parking Full ");  
      delay(1500);
    } else {
      myservoIn.write(90);
      delay(5000);
      myservoIn.write(0);
    }
  }

  if(digitalRead(Out)==1){
    countSlot = countSlot-1;
    myservoOut.write(90);
    delay(5000);
    myservoOut.write(0);
  }
  
  total = S1+S2+S3+S4;
  if(countSlot > 4){ countSlot = 4; }
  if(countSlot == 4){ slot = 0; }
  if(countSlot == 3){ slot = 1; }
  if(countSlot == 2){ slot = 2; }
  if(countSlot == 1){ slot = 3; }
  if(countSlot == 0){ slot = 4; }
  if(countSlot < 0){ countSlot = 0; }

  // HIGH when it's dark, LOW when there's light.
  lcd.setCursor(0,1);
  if(digitalRead(P1)==1){
    lcd.print("P1:Full ");
    S1 = 1;
    digitalWrite(L1, HIGH);
    // Serial.print("P1 : " + String(digitalRead(P1)) + " (Full) \t"); //dark
    // tb.sendTelemetryString("P1", "Terisi");
  } else {
    S1= 0;
    lcd.print("P1:Empty");
    digitalWrite(L1, LOW);
    // Serial.print("P1 : " + String(digitalRead(P1)) + " (Empty) \t"); //Light
    // tb.sendTelemetryString("P1", "Kosong");
  }

  lcd.setCursor(10,1);
  if(digitalRead(P2)==1){
    S2 = 1;
    lcd.print("P2:Full ");
    digitalWrite(L2, HIGH);
    // Serial.print("P2 : " + String(digitalRead(P2)) + " (Full) \t");
    // tb.sendTelemetryString("P2", "Terisi");
  } else {
    S2 = 0;
    lcd.print("P2:Empty");
    digitalWrite(L2, LOW);
    // Serial.print("P2 : " + String(digitalRead(P2)) + " (Empty) \t");
    // tb.sendTelemetryString("P2", "Kosong");
  }
  
  lcd.setCursor(0,2);
  if(digitalRead(P3)==1){
    S3 = 1;
    lcd.print("P3:Full ");
    digitalWrite(L3, HIGH);
    // Serial.print("P3 : " + String(digitalRead(P3)) + " (Full) \t");
    // tb.sendTelemetryString("P3", "Terisi");
  } else {
    S3 = 0;
    lcd.print("P3:Empty");
    // tb.sendTelemetryString("P3", "Kosong");
    digitalWrite(L3, LOW);
    // Serial.print("P3 : " + String(digitalRead(P3)) + " (Empty) \t");
  }
  
  lcd.setCursor(10,2);
  if(digitalRead(P4)==1){
    S4 = 1;
    lcd.print("P4:Full ");
    // tb.sendTelemetryString("P4", "Terisi");
    digitalWrite(L4, HIGH);
    // Serial.print("P4 : " + String(digitalRead(P4)) + " (Full) \n");
  } else {
    S4 = 0;
    lcd.print("P4:Empty");
    // tb.sendTelemetryString("P4", "Kosong");
    digitalWrite(L4, LOW);
    // Serial.print("P4 : " + String(digitalRead(P4)) + " (Empty) \n");
  }

  // Serial.println(countSlot);


  // if(slot == 0){
  //   myservo.write(180);
  // }

  // tb.loop();
}

// void readSensor(){
//   S1=0, S2=0, S3=0, S4=0;
//   if(digitalRead(P1) == 0) {
//     S1 = 1;
//     Serial.print("S1 : " + String(S1) + " \t");
//   }
//   if(digitalRead(P2) == 0) {
//     S2 = 1;
//     Serial.print("S2 : " + String(S2) + " \t");
//   }
//   if(digitalRead(P3) == 0) {
//     S3 = 1;
//     Serial.print("S3 : " + String(S3) + " \t");
//   }
//   if(digitalRead(P4) == 0) {
//     S4 = 1;
//     Serial.print("S4 : " + String(S4) + " \n");
//   }
// }