const path = require("path");
const fs = require("fs");
const { getLocalDate } = require("./utils");

const homePath = path.join(__dirname, "../../");
const __static = "";

function initPath() {
  const paths = [
    getInImgPath(),
    getOurDirPath(),
    getOutImgPath(),
    getTmpImgPath()
  ];
  paths.forEach(path => {
    if (!fs.existsSync(path)) fs.mkdirSync(path);
  });
}

function getPath(name) {
  return path.join(homePath, name);
}
function getInImgPath() {
  return getPath("in");
}
function getOutImgPath() {
  const outPath = getOurDirPath();
  return path.join(outPath, getLocalDate());
}
function getOurDirPath() {
  return getPath("out");
}

function getTmpImgPath() {
  return getPath("tmp");
}

function getToolPath(name) {
  return path.join(__static, "tools", name, path.sep);
}

function getDatabasePath() {
  return path.join(__static, "database");
}

function getImgDecodeTool() {
  return path.join(getToolPath("zxing"), "zxing.exe");
}

/**
 *
 * @param {Object} studentInfo 学生信息
 * @param {String} type 路径类型
 */
function getOutputImagePath(fileName, type) {
  return path.join(getOutImgPath(), type, fileName);
}

/**
 *
 * @param {String} pathContent 目录路径
 */
function makeDirSync(pathContent) {
  let mkPathList = [];
  let curPath = pathContent;

  while (!fs.existsSync(curPath)) {
    mkPathList.unshift(curPath);
    let pos = curPath.lastIndexOf("\\");
    curPath = curPath.substring(0, pos);
  }

  mkPathList.forEach(path => {
    fs.mkdirSync(path);
  });
}

export {
  initPath,
  getPath,
  getToolPath,
  getInImgPath,
  getOutImgPath,
  getTmpImgPath,
  getOutputImagePath,
  makeDirSync,
  getDatabasePath,
  getImgDecodeTool
};
