var Hipchatter = require('hipchatter');
var util = require('util');

function BunyanHipchat(options, error) {
    options = options || {};
    if (!options.token) {
        throw new Error('token cannot be null');
    } else if (!options.room_name) {
        throw new Error('room_name cannot be null');
    } else if (!options.room_token) {
        throw new Error('room_token cannot be null');
    } else {
        this.token = options.token;
        this.room_name = options.room_name;
        this.room_token = options.room_token;
        this.color = options.color || 'yellow';
        this.message_format = options.message_format || 'html';
        this.notify = options.notify || false;
        this.error = error || function() {};
        this.hipchatter = new Hipchatter(this.token);
    }
}

BunyanHipchat.prototype.write = function write(record) {
    var self = this,
        message;

    if (typeof record === 'string') {
        record = JSON.parse(record);
    }

    try {
        message = '<b>Hostname:</b> ' + record.hostname + '<br /><b>Thời gian:</b>' + record.time + '<br /><b>Mã lỗi</b>: ' + record.code + '<br /><b>Lý do</b>: ' + record.reason;
    } catch (err) {
        return self.error(err);
    }
    var options = {
        message: message,
        color: self.color,
        token: self.room_token,
        message_format: self.message_format,
        notify: self.notify
    };
    self.hipchatter.notify(self.room_name, options, function() {});
};

var config = require('./config'),
    bunyan = require('bunyan');
if (config.mode === 'pro') {
    var log = bunyan.createLogger({
        name: 'POSS Frontend',
        streams: [{
            stream: process.stdout,
            level: 'info'
        }, {
            level: 'error',
            type: 'raw', // use 'raw' to get raw log record objects
            stream: new BunyanHipchat({
                token: 'XK4Bs30AvRCWfT2Ri9wyBwK8GnSlwxcEGqDnovLv',
                room_token: 'Ro3sV5flsdQkCzqwKg4puONrDLHA0zQZ2zZwFBA3',
                room_name: 'GOS-LOG'
            })
        }, {
            level: 'error',
            path: config.log_file,
            type: 'rotating-file',
            period: '1d', // daily rotation
            count: 10 // keep 10 back copies
        }]
    });
} else {
    var log = bunyan.createLogger({
        name: 'POSS Frontend',
        streams: [{
            stream: process.stdout,
            level: 'info'
        }, {
            stream: process.stderr,
            level: 'error',
        }]
    });
}
exports = module.exports = log;
