### codetube
    Copyright (C) 2011 payload payload@lavabit.com
    Copyright (C) 2011 dodo dodo.the.last@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
###

require('colors')
util = require('util')
gt = require('gettext')
formatdate = require('formatdate')
config = require('../config')
appconfig = require('../appconfig')
{ obj_merge } = require('../helper')
Path = require('path')

router = null
process.nextTick ->
    router = require('../routes')

User = require('../models/user')
LiftState = require 'lift'

class Controller

    constructor: () ->
        @codetube_name = config.name

    check_err: (err, req, res) ->
        if err
            err
            @db_failed(err, req)
            @home(req, res)
        !!err

    render: (req, res, template, args) ->
        https = config.https and "s" or ""
        codetube =
            href: @home.url()
            name: @codetube_name
            descline: "hosting your git since soon"
            gitline:
                http: "http#{https}://#{config.url}/team/codetube.git"
                git : "git://#{config.url}/team/codetube.git"
        me = req.user
        context = {me, codetube}
        { _, _s, _t } = gt
        { skip, limit } = req.query
        more_context = { _, _s, _t, appconfig, config, skip, limit }
        more_context.javascript = res.app.dynamicViewHelpers?.javascript or ""
        moar_context =
            fdate: formatdate
            flash: req.flash()
            obj_merge: obj_merge
            partial: (name, context...) ->
                console.log 23, partial, name, context
                partial name, context: obj_merge(this, context...)
            render: (args...) -> res.render(args...)
        context = obj_merge( more_context, context, args, moar_context )
        res.render(template, context)

    logged_in: (req, text) ->
        authenticated = req.isAuthenticated()
        req.flash('warn', text or gt._("already logged in")) if authenticated
        return authenticated

    not_logged_in: (req, text) ->
        authenticated = req.isAuthenticated()
        req.flash('warn', text or gt._("not logged in")) unless authenticated
        return not authenticated

    db_failed: (err, req) ->
        if err
            console.error("db failed:".bold.magenta, err)
            if req.session
                text = gt._("a db error occured:") + " " + util.orginspect(err)
                req.flash("error", text)
        !!err

    home: (req, res) ->
        if req.isAuthenticated()
            req.params.userid = req.session.auth.user.name
            router.controller.user.show(req, res)
        else
            router.controller.authentication.login(req, res)

# exports

module.exports = Controller

