const db = require('../database.js');

const TABLE_NAME = "accounts";

function AccountManager() {
}

AccountManager.prototype.fromGoogleIdAuth = function(id, cb) {
    var self = this;
    db.database.queryFirst(TABLE_NAME, { authGoogleUserId: id }, (err, result) => {
        cb(self.fromDb(result));
    });
};

AccountManager.prototype.fromFacebookIdAuth = function(id, cb) {
    var self = this;
    db.database.queryFirst(TABLE_NAME, { authFacebookUserId: id }, (err, result) => {
        cb(self.fromDb(result));
    });
};

AccountManager.prototype.fromSlackIdAuth = function(id, cb) {
    var self = this;
    db.database.queryFirst(TABLE_NAME, { authSlackUserEmailAndTeam: id }, (err, result) => {
        cb(self.fromDb(result));
    });
};

AccountManager.prototype.fromId = function(id, cb) {
    var self = this;
    db.database.queryFirst(TABLE_NAME, { id: id }, (err, result) => {
        cb(self.fromDb(result));
    });
};

AccountManager.prototype.fromDb = function(dbResult) {
    if (!dbResult)
        return null;
    return new Account(dbResult);
};

function Account(dbResult) {
    this.id;

    this.authGoogleUserId;
    this.authFacebookUserId;
    this.authSlackUserEmailAndTeam;

    this.certificates;
    this.cguReadAndAccepted;
    this.dirty;

    //TODO permanent login token array
    if (dbResult) {
        this.id = dbResult.id;

        this.authSlackUserEmailAndTeam = dbResult.authSlackUserEmailAndTeam;
        this.authGoogleUserId = dbResult.authGoogleUserId;
        this.authFacebookUserId = dbResult.authFacebookUserId;

        this.certificates = dbResult.certificates;
        this.cguReadAndAccepted = !!dbResult.cguReadAndAccepted;
        this.dirty = false;
    } else {
        this.id = null;

        this.authSlackUserEmailAndTeam = null;
        this.authGoogleUserId = null;
        this.authFacebookUserId = null;

        this.createCertificate();
        this.cguReadAndAccepted = false;
        this.dirty = true;
    }
}

Account.prototype.toDb = function() {
    return {
        authGoogleUserId: this.authGoogleUserId
        ,authFacebookUserId: this.authFacebookUserId
        ,authSlackUserEmailAndTeam: this.authSlackUserEmailAndTeam
        ,certificates: this.certificates
        ,cguReadAndAccepted: this.cguReadAndAccepted ? 1 : 0
        //TODO permanent login token array
    };
};

Account.prototype.createCertificate = function() {
    //TODO
    this.certificates = null;
};

AccountManager.prototype.createAccount = function() {
    return new Account();
};

AccountManager.prototype.save = function(account, cb) {
    if (!account.id) {
        // insert and save id
        var data = account.toDb();
        data.id = null;
        db.database.insert(TABLE_NAME, data, (newId) => {
            account.id = newId;
            account.dirty = false;
            cb(account);
        });
    } else if (account.dirty) {
        // update
        db.database.update(TABLE_NAME, account.id, account.toDb(), () => {
            account.dirty = false;
            cb(account);
        });
    } else {
        cb(account);
    }
};

module.exports.accountManager = new AccountManager();

module.exports.createTable = function(dbObject, cb) {
    console.info("Creating table " +TABLE_NAME);
    dbObject.run('CREATE TABLE IF NOT EXISTS `' +TABLE_NAME +'` ('
        +"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
        +"`authGoogleUserId` STRING UNIQUE,"
        +"`authFacebookUserId` STRING UNIQUE,"
        +"`authSlackUserEmailAndTeam` STRING UNIQUE,"
        +"`certificates` STRING,"
        +"`cguReadAndAccepted` BOOLEAN NOT NULL DEFAULT FALSE"
        +')', cb);
    //TODO permanent login token array
};

