#include "alarm.h"

AlarmStatus* status;

class DoorSensor {
private:
  int DOOR_SENSOR_PIN = 23;
  AlarmStatus* status;
  int lastDoorState=1; 

public:

  static const int DEFAULT_DOOR_SENSOR_PIN=23;

  DoorSensor(AlarmStatus* statusObj, int doorSensorPin){
    DOOR_SENSOR_PIN = doorSensorPin;
    status = statusObj;
  }
  DoorSensor(AlarmStatus* statusObj){
    DOOR_SENSOR_PIN = DEFAULT_DOOR_SENSOR_PIN;
    status = statusObj;
  }
  void Init(){
    pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
    status->doorStatus = digitalRead(DOOR_SENSOR_PIN);
  }

  bool IsDoorOpen(){
    return status->doorStatus == DOOR_OPEN;
  }
  bool IsDoorClosed(){
    return status->doorStatus == DOOR_CLOSED;
  }

  void HandleDoor(){

    //status->doorStatus = digitalRead(DOOR_SENSOR_PIN);
    lastDoorState = status->doorStatus;              // save the last state
    status->doorStatus = digitalRead(DOOR_SENSOR_PIN); // read new state
    // state change: LOW -> HIGH
    if (lastDoorState == DOOR_CLOSED && status->doorStatus == DOOR_OPEN) { 
      //handleOpen();
      status->doorChanged=true;
    }
    else
    if (lastDoorState == DOOR_OPEN && status->doorStatus == DOOR_CLOSED) { // state change: HIGH -> LOW
      // handleClose();
      status->doorChanged=true;
    }
  }
};
