/*global rendr*/

var Handlebars, fs, layoutTemplate, _;

fs = require('fs');
_ = require('underscore');
Handlebars = require('handlebars');

module.exports = exports = viewEngine;

// Expose this, i.e. for registering view helpers.
exports.Handlebars = Handlebars;

function viewEngine(viewPath, data, callback) {
  var app, layoutData;

  data.locals = data.locals || {};
  app = data.app;
  layoutData = _.extend({}, data, {
    body: getViewHtml(viewPath, data.locals, app),
    appData: app.toJSON(),
    bootstrappedData: getBootstrappedData(data.locals, app),
    _app: app
  });
  renderWithLayout(layoutData, callback);
}

/**
 * render with a layout
 */
function renderWithLayout(locals, callback) {
  getLayoutTemplate(function(err, templateFn) {
    if (err) return callback(err);
    var html = templateFn(locals);
    callback(null, html);
  });
}

layoutTemplate = null;

/**
 * Cache layout template function.
 */
function getLayoutTemplate(callback) {
  var layoutPath;

  if (layoutTemplate) {
    return callback(null, layoutTemplate);
  }
  layoutPath = rendr.entryPath + "/app/templates/__layout.hbs";
  fs.readFile(layoutPath, 'utf8', function(err, str) {
    if (err) return callback(err);
    layoutTemplate = Handlebars.compile(str);
    callback(null, layoutTemplate);
  });
}

function getViewHtml(viewPath, locals, app) {
  var BaseView, View, name, view, basePath;

  basePath = 'app/views';
  BaseView = require('../shared/base/view');
  locals = _.clone(locals);

  // Pass in the app.
  locals.app = app;
  name = viewPath.substr(viewPath.indexOf(basePath) + basePath.length + 1);
  View = BaseView.getView(name);
  view = new View(locals);
  return view.getHtml();
}

function getBootstrappedData(locals, app) {
  var bootstrappedData, modelUtils;

  modelUtils = require('../shared/modelUtils');
  bootstrappedData = {};
  _.each(locals, function(modelOrCollection, name) {
    if (modelUtils.isModel(modelOrCollection) || modelUtils.isCollection(modelOrCollection)) {
      bootstrappedData[name] = {
        summary: app.fetcher.summarize(modelOrCollection),
        data: modelOrCollection.toJSON()
      };
    }
  });
  return bootstrappedData;
}

@@ -48,16 +48,22 @@
 plate, _
+, path
 ;%0A%0Afs = 
@@ -139,16 +139,40 @@
 ebars');
+%0Apath = require('path');
 %0A%0Amodule
@@ -1435,19 +1435,33 @@
 h = 
-'app/
+path.join('app', '
 views'
+)
 ;%0A  

Fix view path for windows