/**
 * Created by jps123 on 8/2/16.
 */
"use strict";

const FILE_UPLOAD_CONFIG = "fileUploader";
const DEFAULT_ALLOWED_FILE_TYPES = [
  "jpeg",
  "jpg",
  "png",
  "tiff",
  "bmp",
  "gif",
  //docs
  "pdf",
  "txt",
  "doc",
  "docx",
  "ppt",
  "pptx",
  "xls",
  "xlsx",
  //compressed
  "zip",
  "rar",
  "7zip",
  "gzip"
];


var whiteList = null;

function whiteListed(fileType, fileUploadConfig) {

  if (!fileType) fileType = 'unknown';

  fileType = fileType.toLowerCase();

  //if not strict then allow file types with the unknown extension (set at Uploader.js when it can't get
  // the extension from filename. Also set at PlainDirectoryProvider when it can't get the magic number from file's contents)
  if (fileType === 'unknown') {
    return true;
  }

  if (whiteList == null) {
    var fileUpload = fileUploadConfig ? fileUploadConfig : config.get(FILE_UPLOAD_CONFIG);

    if (fileUpload && fileUpload.extensionWhitelist && Array.isArray(fileUpload.extensionWhitelist)) {
      whiteList = fileUpload.extensionWhitelist;
      logger.warn("DEPRECATED: please use allowlist and denylist instead of whitelist and blacklist.");
    } else if (fileUpload && fileUpload.extensionAllowlist && Array.isArray(fileUpload.extensionAllowlist)) {
      whiteList = fileUpload.extensionAllowlist;
    } else {
      whiteList = DEFAULT_ALLOWED_FILE_TYPES;
    }

  }

  return whiteList.indexOf(fileType) > -1;
}


var EX_CONSTANTS = {
    "documentExtensionNameMap": {
        "DOC": "Word 97-2003 Document",
        "DOCX": "Word Document",
        "PPT": "PowerPoint 97-2003 Presentation",
        "PPTX": "PowerPoint Presentation",
        "XLS": "Excel 97-2003 Workbook",
        "XLSX": "Excel Workbook"
    },
    "supportedImageExtensions": [
        "png",
        "jpg",
        "jpeg",
        "gif",
        "tif",
        "tiff",
        "bmp",
        "png",
        "psd",
        "psp",
        "svg"
    ],
    "supportedVideoExtensions": [
        "ogg",
        "mp4",
        "webm"
    ]
};


/**
 * Checks if file is an image.
 * @param file
 * @returns {boolean}
 */
function isImageFile(fileName) {
    var ext = getExtension(fileName).toLowerCase();
    return filterImage(ext);
};

/**
 * Returns true if the extension is an image
 * @param ext
 * @returns {boolean}
 */
function filterImage(ext) {
    return arrayContains(ext.toLowerCase(), EX_CONSTANTS.supportedImageExtensions);
};

/**
 * Looks for the needle in the array haystack
 * @param needle
 * @param arrHayStack
 * @returns {boolean}
 */
function arrayContains(needle, arrHayStack) {
    return getItemIndex(needle, arrHayStack) > -1;
}

/**
 * Looks for the needle in the array haystack and returns the index
 * @param needle
 * @param arrHayStack
 * @returns The index of needle in arrHayStack else -1
 */
function getItemIndex(needle, arrHayStack) {
    return arrHayStack.indexOf(needle);
}

/**
 * Given a file name, returns the extension in uppercase
 * @param fileName
 * @returns {string}
 */
function getExtension(fileName) {
    var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
    return ext.toUpperCase();
};

module.exports = {
    isImageFile: isImageFile,
    getExtension: getExtension,
    arrayContains: arrayContains,
    getItemIndex: getItemIndex,
    whiteListed: whiteListed
};
