
const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;

const ACCESS_TYPE = {
    unknown: 0,
    ldapAccount: 1,
    email: 2,
    link: 3,
    everyOne: 4
};

const ACCESS_TO = {
    unknown: 0,
    item: 1,
    tag: 2,
    meta: 3,
    everything: 4,
    admin: 5
};

const ACCESS_GRANT = {
    none: 0,
    read: 1,
    write: 2,
    readNoMeta: 3
};

function AccessModel() {
    DatabaseModel.call(this);
    this.id = null;
    this.type = ACCESS_TYPE.unknown;
    this.typeData = "";
    this.accessTo = ACCESS_TO.unknown;
    this.accessToData = "";
    this.accessToDataDeserialized = null;
    this.grant = ACCESS_GRANT.none;
}

AccessModel.prototype = Object.create(DatabaseModel.prototype);

AccessModel.prototype.getTableName = function() {
    return "access";
}

AccessModel.prototype.createOrUpdateBase = async function(dbHelper) {
    await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'access' (
        id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        type TINYINT NOT NULL,
        typeData STRING NOT NULL,
        accessTo TINYINT NOT NULL,
        accessToData STRING NOT NULL,
        grant TINYINT NOT NULL
        )`);
}

AccessModel.prototype.describe = function() {
    return {
        "id": this.id,
        "type": this.type,
        "typeData": this.typeData,
        "accessTo": this.accessTo,
        "accessToData": this.accessToData,
        "grant": this.grant
    };
}

AccessModel.prototype.versionColumn = function() { return ""; }

AccessModel.prototype.fromDb = function(dbObj) {
    this.id = dbObj["id"];
    this.type = dbObj["type"];
    this.typeData = dbObj["typeData"];
    this.accessTo = dbObj["accessTo"];
    this.accessToData = dbObj["accessToData"];
    try {
        if (this.accessTo === ACCESS_TO.meta)
            this.accessToDataDeserialized = JSON.parse(this.accessToData);
    }
    catch (err) {}
    this.grant = dbObj["grant"];
}

module.exports.AccessModel = AccessModel;
module.exports.ACCESS_TYPE = ACCESS_TYPE;
module.exports.ACCESS_TO = ACCESS_TO;
module.exports.ACCESS_GRANT = ACCESS_GRANT;

