"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const MysqlLib = require("mysql");
const settings_1 = require("./settings");
/**
 * better control of mysql connection status
 */
const CONNECTION_STATUS = {
    CONNECTED: 'Connected',
    RECONNECTED: 'Reconnected',
    DISCONNECTED: 'Disconnected',
    ERROR: 'Error',
    ERROR_CONNECT: 'Error. Could not connect.',
    DEFAULT: ''
};
class Mysql {
    constructor(tag = "tag") {
        this.mysql = null;
        this.waitForStatus = false;
        this.config = settings_1.default.Instance.getMysql(tag);
        let statusInterval = this.config.statusCheckInterval;
        if (statusInterval == null || typeof statusInterval != "number" || statusInterval < 1) {
            statusInterval = 5000;
        }
        this.status = CONNECTION_STATUS.DEFAULT;
        if (this.config.autoConnect) {
            this.connect();
        }
        this.statusCheck();
        this.interval = setInterval(this.statusCheck.bind(this), statusInterval);
    }
    statusCheck() {
        if (this.waitForStatus)
            return;
        if (this.mysql == null) {
            this.status = CONNECTION_STATUS.DISCONNECTED;
            return;
        }
        this.waitForStatus = true;
        let self = this;
        this.mysql.ping({
            sql: "",
            timeout: 30000,
        }, function (error) {
            self.waitForStatus = false;
            if (error != null) {
                self.status = CONNECTION_STATUS.ERROR;
            }
            else {
                self.status = CONNECTION_STATUS.CONNECTED;
            }
        });
    }
    destroy() {
        clearInterval(this.interval);
        this.closeConnection();
    }
    closeConnection() {
        if (this.mysql == null)
            return;
        this.mysql.destroy();
        this.mysql = null;
    }
    connect() {
        this.closeConnection();
        const self = this;
        let settings = {
            multipleStatements: this.config.multipleStatements,
            host: this.config.host,
            port: this.config.port,
            database: this.config.database,
            user: this.config.user,
            password: this.config.password
        };
        const connection = MysqlLib.createConnection(settings);
        connection.connect(function (err) {
            if (err) {
                self.status = CONNECTION_STATUS.ERROR_CONNECT;
            }
            else {
                if (self.status == CONNECTION_STATUS.CONNECTED || self.status == CONNECTION_STATUS.DEFAULT)
                    self.status = CONNECTION_STATUS.CONNECTED;
                else
                    self.status = CONNECTION_STATUS.RECONNECTED;
            }
        });
        connection.on("end", function (err) {
            self.destroy();
        });
        connection.on("error", function (err) {
            self.status = CONNECTION_STATUS.ERROR;
        });
        connection.on("connect", function () {
            if (self.status == CONNECTION_STATUS.CONNECTED || self.status == CONNECTION_STATUS.DEFAULT)
                self.status = CONNECTION_STATUS.CONNECTED;
            else
                self.status = CONNECTION_STATUS.RECONNECTED;
        });
        self.mysql = connection;
    }
    /**
     * return the status of the inner redis connection
     */
    getStatus() {
        return this.status;
    }
    isFunctional() {
        let _status = this.getStatus();
        return _status == CONNECTION_STATUS.CONNECTED || _status == CONNECTION_STATUS.RECONNECTED;
    }
    tryToConnect() {
        if (this.mysql == null) {
            if (this.config.autoConnect) {
                this.connect();
                return true;
            }
            else {
                return false;
            }
        }
        return true;
    }
    query(options, values, callback) {
        if (!this.tryToConnect())
            return null;
        if (this.mysql != null) {
            return this.mysql.query(options, values, callback);
        }
        else
            return null;
    }
}
exports.default = Mysql;
//# sourceMappingURL=mysql.js.map