const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const layout = require("express-layout");
const compression = require("compression");
const forceSSL = require("./app/helpers/forceSSL");

const {
  APP_PORT,
  CRAWLER_INTERVAL,
  STOP_CRAWLER
} = require("./app/config/appConfig");
const routes = require("./app/routes");
const { crawlAll } = require("./app/crawler/crawl");
const { checkUpNotify } = require("./app/services/notificationService");
const {
  notifyForNewRealEstates
} = require("./app/services/notificationService");

const app = express();

app.use(forceSSL());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.set("views", path.join(__dirname, "/app/views"));
app.set("view engine", "ejs");
app.use(layout());

app.use(compression());
app.use("/", routes);

app.use("/assets", express.static("./app/public"));

app.listen(APP_PORT, () =>
  console.log(`Example app listening on port ${APP_PORT}!`)
);

let crawlerRunning = STOP_CRAWLER;
const crawl = () => {
  if (!crawlerRunning) {
    crawlerRunning = true;
    crawlAll().then(newRealEstates => {
      crawlerRunning = false;
      notifyForNewRealEstates(newRealEstates);
    });
  }
};

setInterval(crawl, CRAWLER_INTERVAL * 1000);

setInterval(checkUpNotify, 1000 * 60 * 60 * 24);
