### 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/>
###

util = require('util')
gt = require('gettext')
forms = require('../forms')
config = require('../config')
appconfig = require('../appconfig')
Controller = require('./skeleton')

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

strobj = (text, opts) ->
    str = new String(text)
    for own key, value of (opts or {})
        str[key] = value
    str

form_subpages =
    user:
        basic: strobj(gt._("basic"), symbol:'⚙')
        password: strobj(gt._("pass"), symbol:'⌨')
        email: strobj(gt._("e-mail"), symbol:'✉')
        group: strobj(gt._("groups"), symbol:'⌘')
        ssh: strobj(gt._("ssh"), symbol:'…')
        'delete': strobj(gt._("delete"), symbol:'☠')
    project:
        basic: strobj(gt._("basic"), symbol:'⚙')
        collaborators: strobj(gt._("comrades"), symbol:'☭')
        privacy: strobj(gt._("privacy"), symbol:'☻')
        'delete': strobj(gt._("delete"), symbol:'☠')

class HelperRoutes extends Controller

    settings: (sub, req, res, subpage, defaults, args..., callback) =>
        dummy = _subpages:form_subpages[sub], _exists:no, _show:no
        return callback(null, dummy) unless subpage
        subpager = forms["#{sub}_#{subpage}"]
        unless subpager
            return callback(gt._s(gt._("setting '%1' not found."), subpage), dummy)
        add_subpages = (form) ->
            return dummy unless form
            form._subpages = dummy._subpages
            form._exists = yes
            form._show = yes
            form
        form = add_subpages(subpager.apply(forms, [req, res].concat(args)))
        return callback(null, form) if form is dummy
        if req.method is 'GET'
            form = add_subpages(form.bind(defaults))
            form._exists = no
            return callback(null, form)
        else if req.method is 'POST'
            form = form.bind(req.body)
            form.validate(() ->)
            form.clear_data = form.clear_data?(req, defaults) or form.data
            form.post_validate?()
            return callback(null, add_subpages(form))
        form._show = no
        form._exists = no
        callback(gt._("unknown request method."), form)


    about: (req, res) =>
        @render req, res, 'about'

    welcome: (req, res) =>
        @render req, res, 'welcome'


# exports

module.exports = HelperRoutes

