from quixote import get_response, redirect
from quixote.directory import Directory

from qommon.form import *

from form_prefill import FormPrefill
from fields_prefill import FieldsDirectory
from menu import html_top, command_icon

class FormUI:
    def __init__(self, form_prefill):
        self.form_prefill = form_prefill

    def form_edit(self):
        form = Form(enctype='multipart/form-data')
        form.add(StringWidget, 'name', title = _('Form name'), required = True,
            size = 50, value = self.form_prefill.name, hint=_('Only used for display'))
        form.add(UrlWidget, 'url', title = _('Form address'), required = True,
            size = 50, value = self.form_prefill.url)
        form.add(StringWidget, 'profile', title = _('ID-WSF data profile'), required = True,
            size = 50, value = self.form_prefill.profile, hint=_('Example: urn:liberty:id-sis-pp:2005-05'))
        form.add(StringWidget, 'prefix', title = _('ID-WSF data XML prefix'), required = True,
            size = 50, value = self.form_prefill.prefix, hint=_('Example: pp'))
        form.add_submit('submit', _('Submit'))
        form.add_submit('cancel', _('Cancel'))
        return form

    def submit_edit(self, form):
        for f in ('name', 'url', 'profile', 'prefix'):
            widget = form.get_widget(f)
            setattr(self.form_prefill, f, widget.parse())
        self.form_prefill.store()

class FormPage(Directory):
    _q_exports = ['', 'edit', 'delete']

    def __init__(self, form_id):
        self.form_prefill = FormPrefill.get(form_id)
        self.form_ui = FormUI(self.form_prefill)
        get_response().breadcrumb.append((form_id + '/', form_id))

    def _q_index [html] (self):
        html_top('forms_prefill', title = 'Form prefilling')

        '<h2>%s</h2>' % _('Form prefilling configuration')
        '<dl>'
        '<dt><a href="edit">%s</a></dt> <dd>%s</dd>' % (
                _('Edit'), _('Configure this form'))
        '<dt><a href="fields/">%s</a></dt> <dd>%s</dd>' % (
                _('Fields'), _('Configure the fields of this form'))
        '</dl>'

    def _q_lookup(self, component):
        if component == 'fields':
            return FieldsDirectory(self.form_prefill)

    def edit [html] (self):
        form = self.form_ui.form_edit()
        if form.get_widget('cancel').parse():
            return redirect('.')

        if form.is_submitted() and not form.has_errors():
            self.form_ui.submit_edit(form)
            return redirect('.')

        get_response().breadcrumb.append( ('edit', _('Edit')) )
        html_top('edit', title = _('Edit'))
        '<h2>%s</h2>' % _('Edit')

        form.render()

    def delete [html] (self):
        form = Form(enctype='multipart/form-data')
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
                        'You are about to irrevocably delete this form.')))
        form.add_submit('submit', _('Submit'))
        form.add_submit('cancel', _('Cancel'))
        if form.get_widget('cancel').parse():
            return redirect('..')
        if not form.is_submitted() or form.has_errors():
            get_response().breadcrumb.append(('delete', _('Delete')))
            html_top('delete_form', title = _('Delete Form'))
            '<h2>%s : %s</h2>' % (_('Delete Form'), self.form_prefill.id)
            form.render()
        else:
            self.form_prefill.remove_self()
            return redirect('..')


class FormsDirectory(Directory):
    _q_exports = ['', 'new']

    def __init__(self, host):
        get_response().breadcrumb.append(('forms_prefill/', _('Forms')))
        self.host = host

    def _q_lookup(self, component):
        return FormPage(component)

    def _q_index [html] (self):
        html_top('forms', title = _('Forms'))
        """<ul id="nav-forms-admin">
          <li><a href="new">%s</a></li>
        </ul>""" % _('New Form')

        '<ul class="biglist">'

        for form_prefill in FormPrefill.select(lambda x: x.host_id == self.host.id):
            if not form_prefill.name:
                continue
            '<li>'
            '<strong class="label">%s</strong>' % form_prefill.name
            url = form_prefill.url
            '<br /><a href="%s">%s</a>' % (url, url)
            '<p class="commands">'
            command_icon('%s/' % form_prefill.id, 'edit')
            command_icon('%s/delete' % form_prefill.id, 'remove')
            '</p></li>'
        '</ul>'

    def new [html] (self):
        get_response().breadcrumb.append(('new', _('New')) )
        form_prefill = FormPrefill()
        form_prefill.host_id = self.host.id
        form_prefill.store()
        return redirect('%s/edit' % form_prefill.id)

