#include <Arduino.h>
// #include "DictionaryDeclarations.h"
#include <Dictionary.h>
#include "alarm.h"
#include "DoorSensor.h"
#include "LedController.h"
#include "Siren.h"
#include "CardReader.h"
#include "WiegandReader.h"
#include "soc/rtc_wdt.h"
//#include <rtc_wdt.h>
#include <WiFi.h>
#include "ServerConnector.h"
// #include <WiFiClientSecure.h>
// #include "FrancelsoftCert.h"


#include "Times.h"
// #include <Dictionary.h>

AlarmStatus s;
DoorSensor doorSensor(&s);
LedController led(&s);
Siren siren(&s);
// CardReader cardReader(&s);
WiegandReader cardReader(&s, 18, 19);
ServerConnector serverConnector(&s, &cardReader);

const int SILENCE_JMP_PIN = 12;
//const char*  server = "push.francelsoft.com";  // Server URL



void printStatus()
{
  String msg = String("Door: ") + String(doorSensor.IsDoorOpen() ? "Open" : "Closed");
  msg = msg + " | Alarm: " + String(s.isArmed ? "Armed" : "Disarmed");
  msg = msg + " | Is Fired: " + String(s.isFired ? "Fired" : "Not Fired");
  msg = msg + " | Led: " + String(s.ledOn ? "On" : "Off");
  msg = msg + " | Silent: " + String(s.silent ? "Silent" : "Loud");
  msg = msg + " | Muted: " + String(s.muted ? "Muted" : "Not Muted");
  Serial.println(msg);
}

void setup()
{
  Serial.begin(115200);

  doorSensor.Init();
  led.Init();
  siren.Init();

  cardReader.SetLed(5);
  cardReader.SetBeep(23);
  cardReader.Init();

  pinMode(SILENCE_JMP_PIN, INPUT_PULLUP);

  s.silent = false;
  s.isArmed = doorSensor.IsDoorClosed();

  addUsers();

  // WiFi.mode(WIFI_STA);
  // WiFi.disconnect();
  // delay(100);
  SoundAlarmAsync();
  // StartNotifierAsync();

  printStatus();

  ConnectToWifi();

  // setClock();
  serverConnector.StartNotifierAsync();
}

void addUsers(){
  cardReader.AddUser("0438768a2c6a80", "pin verizure");
  cardReader.AddUser("23141f2d", "pin azul");
  cardReader.AddUser("91cf3e02", "card access");

  cardReader.AddUser("2911999", "nuevo pin 1");
  cardReader.AddUser("2648564", "nuevo pin 2");
}

void loop()
{
  //Inputs
  s.silent = digitalRead(SILENCE_JMP_PIN) == LOW;

  doorSensor.HandleDoor();

  // if(cardReader.IsInit()){
  //   cardReader.HandleReader();
  // }else{
  //   cardReader.Init();
  // }
  cardReader.HandleReader();

  if(s.allowed){
    HandleUserPresent();
    s.allowed=false;
  }

  if (s.doorChanged)
  {
    DoorEvent();
    s.doorChanged = false;
  }

  AutoRearm();

  //Output
  if(s.showError){
    led.Error();
    s.showError=false;
  }
  led.Update();

  if(!serverConnector.IsRunning())
  {
    Serial.println("Restarting communication thread.");
    serverConnector.StartNotifierAsync();
  }
}

void DoorEvent()
{
  if (doorSensor.IsDoorOpen())
  {
    Serial.print("[Door] The door-opening: ");
    
    if (!s.isArmed)
    {
      Serial.println("Alarm not armed");
      return;
    }
    if(s.isFired){ // Already fired.
      return;
    }
    Serial.println("Alarm is armed, firing");
    s.isFired = true;
    s.sendNotif=true;
  }
  else
  {
    // delay(500);
    Serial.println("[Door] The door-closing");
    s.isArmed = true;
  }
}

void HandleUserPresent(){
  Serial.print("User Allowed: ");
  Serial.println(s.userAllowed);
  s.lastEntrance=millis();
  if(s.isFired){
    s.isFired=false;
    s.isArmed=s.doorStatus==DOOR_CLOSED;
  }else{
    s.isArmed=false;
  }
}

void AutoRearm(){
  if(s.isArmed){
    return;
  }
  if(s.lastEntrance + FromMinutes(5) < millis()){

    //Auto Rearm if door is closed.
    if(s.doorStatus == DOOR_CLOSED){
      Serial.println("[Door] 5m Passed, AutoArming again");
      s.isArmed= true;
    }

  }
}

TaskHandle_t SoundTask;
void SoundAlarmAsync(){
  xTaskCreate(SoundAlarmIntAsync, "task1", 1000, NULL, 1, NULL);
}

void SoundAlarmIntAsync( void * pvParameters ){
  for(;;){
      if (!s.isFired)
      {
        vTaskDelay( 200 / portTICK_PERIOD_MS );
        continue;
      }
      siren.SoundSiren();
      vTaskDelay( 1 / portTICK_PERIOD_MS );
  }
}


void ConnectToWifi(){
  // We start by connecting to a WiFi network
    WiFi.begin("Guile&Andre", "popolitoproducciones");

    Serial.println();
    Serial.println();
    Serial.print("Waiting for WiFi... ");

    while(WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
}


void setClock() {
  // configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  configTime(0, 0, "pool.ntp.org", "time.nist.gov");

  Serial.print("Waiting for NTP time sync: ");
  time_t now = time(nullptr);
  while (now < 8 * 3600 * 2) {
    delay(500);
    Serial.print(".");
    now = time(nullptr);
  }
  Serial.println("");
  struct tm timeinfo;
  gmtime_r(&now, &timeinfo);
  Serial.print("Current time: ");
  Serial.print(asctime(&timeinfo));
}