#include <Arduino.h>
#include "alarm.h"
#include "DoorSensor.h"
#include "LedController.h"
#include "Siren.h"
// #include <WiFi.h>

AlarmStatus s;
DoorSensor doorSensor(&s);
LedController led(&s);
Siren siren(&s);

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);

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

  s.isArmed = doorSensor.IsDoorClosed();
  // Serial.println("Alarm is " + String(s.isArmed ? "Armed" : "Disarmed"));
  // WiFi.mode(WIFI_STA);
  // WiFi.disconnect();
  // delay(100);

  printStatus();
  
}

void loop() {
  doorSensor.HandleDoor();

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


  if(s.isFired){
    s.silent = !s.silent;
    siren.SoundSiren();
  }

  led.Update();

  
  // delay(1000);

}

void DoorEvent(){
  if(doorSensor.IsDoorOpen()){
    Serial.println("The door-opening");
    if(!s.isArmed)
    {
      Serial.println("Alarm is dissarmed, not fireing");
      return;
    }
    Serial.println("Alarm is armed, firing");
    s.isFired=true;
  }else{
    // delay(500);
    Serial.println("The door-closing");
    s.isArmed=true;
  }
}
