import fs, { readFileSync } from "fs";
import path from "path";
import moment from "moment";
// import env from "./env";

const configJson = readFileSync(path.join(__dirname, "../config.json"));
const store = JSON.parse(configJson.toString());

export default store;

const timeLog = path.join(__dirname, "../../logs/time.log");
let syncTime = {};
if (fs.existsSync(path.join(__dirname, "../../logs/time.log"))) {
  try {
    syncTime = readFileSync(timeLog).toJSON();
  } catch (err) {
    syncTime = {};
  }
}
store["syncTime"] = syncTime;

export function updateSyncTime(): void {
  if (env.server && env.exam) {
    syncTime[env.server.host + "_" + env.exam.id] = moment().format(
      "YYYY-MM-DD HH:mm:ss"
    );
    store["syncTime"] = syncTime;
    fs.writeFileSync(timeLog, JSON.stringify(syncTime));
  }
}
