"use strict";

const express = require("express"),
  fs = require("fs"),
  url = require("url"),
  config = require("config"),
  moment = require("moment");

let photoDB;

require("../db/photos").then(function(db) {
  photoDB = db;
});

const router = express.Router();
let scanner = null;

router.get("/", function(req, res/*, next*/) {
  console.log("/scan.");

  if (!req.user.maintainer) {
    return res.status(401).send("Only maintainers can start a scan");
  }

  let responded = false;

  if (!scanner) {
    return res.status(500).send("Not yet initialized.");
  }

  scanner.scan().then(function(results) {
    if (responded) {
      return;
    }

    responded = true;
    return res.status(200).send(results);
  });

  setTimeout(function() {
    if (responded) {
      return;
    }
    responded = true;
    return res.status(200).send("scan initiated");
  }, 50);
});

module.exports = function(_scanner) {
  scanner = _scanner;
  return router;
};
  