var fs = require('fs');
////////////////////////////////////////////////////////////////////////////
// CONTENT

function Content(path) {
    this._path = path;
    this._body = '';

    // TODO:
    this._status = 200;
    this._header = {'Content-Type': 'text/plain'};

    return this;
}

Content.prototype.readAll = function(onsuccess, onerror) {
    var self = this;
    self._onsuccess = onsuccess;
    self._onerror = onerror;
    var reader = fs.createReadStream(self._path);
    reader.on('error', function(e) { onerror(); });
    reader.on('end', function() { self._onend(); });
    reader.on('data', function(data) { self._ondata(data); });
}

Content.prototype._ondata = function(data) {
    // TODO: redirect to the response object for speed.
    this._body += data;
}

Content.prototype._onend = function() {
    // TODO: split and expand template.
    this._onsuccess(this._status, this._header, this._body);
}

////////////////////////////////////////////////////////////////////////////
// CONTEXT

function Context() {
    this.htdocsDir = './htdocs';
    return this;
}

Context.prototype.handle = function(req, res) {
    var path = this._dispatch(req, res);
    if (path) {
        this._render(path, req, res);
    } else {
        res.writeHead(404, {'Context-Type': 'text/html'});
        res.end('<h1>404 Not found</h1>');
    }
}

Context.prototype._dispatch = function(req, res) {
    // TODO: choice correct object by path.
    return './htdocs/_folder';
}

Context.prototype._render = function(path, req, res) {
    var content = new Content(path);
    content.readAll(function(status, header, body) {
        res.writeHead(status, header, body);
        res.write(body);
        res.end();
    }, function() {
        res.writeHead(500, {'Content-Type': 'text/html'});
        res.end('<h1>500 Internal server error</h1>');
    });
}

////////////////////////////////////////////////////////////////////////////
// MODULE EXPORTS

module.exports = Context;
// vim:set ts=8 sts=4 sw=4 tw=0 et:

var fs = require('fs');
////////////////////////////////////////////////////////////////////////////
// CONTENT

function Content(path) {
    this._path = path;
    this._onsuccessCallback = null;
    this._onerrorCallback = null;
    this._body = '';

    return this;
}

Content.prototype.readAll = function(onsuccess, onerror) {
    var self = this;
    self._onsuccessCallback = onsuccess;
    self._onerrorCallback
    var reader = fs.createReadStream(self._path);
    reader.on('error', function(e) { self._onerror(e); });
    reader.on('end', function() { self._onend(); });
    reader.on('data', function(data) { self._ondata(data); });
}

Content.prototype._ondata = function(data) {
    // FIXME: redirect to the response object for speed.
    this._body += data;
}

Content.prototype._onend = function() {
    // TODO: split and expand template.
    this._status = 200;
    this._header = {'Content-Type': 'text/plain'};
    if (this._onsuccessCallback) {
        this._onsuccessCallback(this._status, this._header, this._body);
    }
}

Content.prototype._onerror = function(exception) {
    if (this._onerrorCallback) {
        // FIXME: log exception.
        this._onerrorCallback();
    }
}

////////////////////////////////////////////////////////////////////////////
// CONTEXT

function Context() {
    this.htdocsDir = './htdocs';
    return this;
}

Context.prototype.handle = function(req, res) {
    var path = this._dispatch(req, res);
    if (path) {
        this._render(path, req, res);
    } else {
        res.writeHead(404, {'Context-Type': 'text/html'});
        res.end('<h1>404 Not found</h1>');
    }
}

Context.prototype._dispatch = function(req, res) {
    // TODO: choice correct object by path.
    return './htdocs/_folder';
}

Context.prototype._render = function(path, req, res) {
    var content = new Content(path);
    content.readAll(function(status, header, body) {
        res.writeHead(status, header, body);
        res.write(body);
        res.end();
    }, function() {
        res.writeHead(500, {'Content-Type': 'text/html'});
        res.end('<h1>500 Internal server error</h1>');
    });
}

////////////////////////////////////////////////////////////////////////////
// MODULE EXPORTS

module.exports = Context;
// vim:set ts=8 sts=4 sw=4 tw=0 et:

fix handlers