// Generated by CoffeeScript 2.3.2
(function() {
  var Fs, Mime, Result;

  Fs = require('fs');

  Mime = require('mime');

  Result = class Result {
    constructor() {
      this.result = {};
      // some default result
      // handler static file
      this.register('file', function(file, mime = null) {
        this.response.responded = true;
        return Fs.access(file, Fs.R_OK, (err) => {
          var stream;
          if (err != null) {
            return this.response.status(404).content('File not found.').respond();
          }
          if (mime == null) {
            mime = Mime.lookup(file);
          }
          stream = Fs.createReadStream(file);
          return this.response.header('content-type', mime + '; charset=utf-8').content(stream).respond();
        });
      });
      // blank content
      this.register('blank', function() {
        return this.response.content('');
      });
      // redirect url
      this.register('redirect', function(url, permanently = false) {
        return this.response.status(permanently ? 301 : 302).header('location', url);
      });
      // redirect to referer
      this.register('back', function() {
        var url;
        url = this.request.header('referer', '/');
        return this.response.status(302).header('location', url);
      });
      // handler 404
      this.register('notFound', function() {
        return this.response.status(404).content('File not found.');
      });
      // handler json data
      this.register('json', function(data) {
        return this.response.header('content-type', 'application/json; charset=utf-8').content(JSON.stringify(data));
      });
      // handler html data
      this.register('content', function(content, type = 'text/html') {
        return this.response.header('content-type', type + '; charset=utf-8').content(content);
      });
    }

    
    // register response method
    register(name, fn) {
      return this.result[name] = function(...args) {
        return function(request, response) {
          return fn.apply({request, response}, args);
        };
      };
    }

  };

  module.exports = Result;

}).call(this);
