const fs = require("fs");
const path = require("path");
const expect = require("chai").expect;
const Ajv = require("ajv").default;
const ajv = new Ajv({ allErrors: true });
require("ajv-errors")(ajv);

const themeSchemaFile = "json_theme_schema.jsonc";
let manifest;
let validator;
let files;
let workingDirectory;

function leftOuterJoin(leftArray, rightArray) {
    return leftArray.filter(function (el) {
        return this.indexOf(el) < 0;
    }, rightArray);
}

function flatReferences(manifest) {
    allImageIds = [
        manifest.dayHighlight,
        manifest.nightHighlight,
        ...manifest.dayImageList,
        ...manifest.nightImageList,
        ...(manifest.sunsetImageList || []),
        ...(manifest.sunriseImageList || [])
    ];
    return allImageIds.filter(el => el !== undefined).map(el => {
        return manifest.imageFilename.replace("*", el);
    });
}

describe('Mandatory Checks', function () {
    before(function () {
        const slug = process.env["PACKAGE_NAME"];
        expect(slug).to.be.a("string").that.is.not.empty;
        const root = process.env["ENTRY_FOLDER"];
        expect(root).to.be.a("string").that.is.not.empty;
        workingDirectory = path.join(root, slug);
        files = fs.readdirSync(workingDirectory);
        expect(files).to.be.an('array').that.is.not.empty;
        validator = ajv.compile(JSON.parse(fs.readFileSync(themeSchemaFile, 'utf8')));
    });

    step('Manifest is an existing json file', function () {
        manifest = JSON.parse(fs.readFileSync(path.join(workingDirectory, "theme.json"), "utf8"));
    });

    step('Manifest passes the schema', function () {
        const isValid = validator(manifest);
        if (!isValid) expect.fail(`\n\t• ${validator.errors.map(el => { return el.message; }).join('\n\t• ')}`);
    });

    step('There are no missing files', function () {
        let references = flatReferences(manifest);
        references.push("theme.json");
        let missings = leftOuterJoin(references, files);
        expect(missings,
            `The following reference${(missings.length > 1) ? "s" : ""} from theme.json ${(missings.length > 1) ? "are" : "is"} missing in the pack. Consider adding ${(missings.length > 1) ? "them" : " it"} to the pack or removing the reference${(missings.length > 1) ? "s" : ""} from the theme.json:\n\t• ${missings.join('\n\t• ')}\n`
        ).to.be.lengthOf(0,);
    });

    step('There are no orphan files', function () {
        let references = flatReferences(manifest);
        let orphans = leftOuterJoin(files.filter(x => x !== "theme.json"), references);
        expect(orphans,
            `The following orphan file${(orphans.length > 1) ? "s are" : " is"} not referenced in this theme.json. Consider removing the file${(orphans.length > 1) ? "s" : ""} or referencing ${(orphans.length > 1) ? "them" : "it"} in the theme.json:\n\t• ${orphans.join('\n\t• ')}\n`
        ).to.be.lengthOf(0);
    });

});