# w.c.s. - web application for online forms
# Copyright (C) 2005-2020  Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from quixote import get_publisher, get_response, redirect
from quixote.directory import Directory
from quixote.html import TemplateIO, htmltext

from wcs.qommon import _, errors, template
from wcs.qommon.backoffice.menu import html_top
from wcs.qommon.form import (Form, HtmlWidget, StringWidget, TextWidget,
        ComputedExpressionWidget, WidgetList)
from wcs.mail_templates import MailTemplate


class MailTemplatesDirectory(Directory):
    _q_exports = ['', 'new']
    do_not_call_in_templates = True

    def _q_traverse(self, path):
        get_response().breadcrumb.append(('workflows/', _('Workflows')))
        get_response().breadcrumb.append(('mail-templates/', _('Mail Templates')))
        return super(MailTemplatesDirectory, self)._q_traverse(path)

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

    def _q_index(self):
        html_top('mail_templates', title=_('Mail Templates'))
        return template.QommonTemplateResponse(
                templates=['wcs/backoffice/mail-templates.html'],
                context={'view': self, 'mail_templates': MailTemplate.select(order_by='name')})

    def new(self):
        form = Form(enctype='multipart/form-data')
        form.add(StringWidget, 'name', title=_('Name'), required=True, size=50)
        form.add_submit('submit', _('Add'))
        form.add_submit('cancel', _('Cancel'))
        if form.get_widget('cancel').parse():
            return redirect('.')

        if form.is_submitted() and not form.has_errors():
            mail_template = MailTemplate(name=form.get_widget('name').parse())
            mail_template.store()
            return redirect('%s/edit' % mail_template.id)

        get_response().breadcrumb.append(('new', _('New Mail Template')))
        html_top('mail_templates', title=_('New Mail Template'))
        r = TemplateIO(html=True)
        r += htmltext('<h2>%s</h2>') % _('New Mail Template')
        r += form.render()
        return r.getvalue()


class MailTemplatePage(Directory):
    _q_exports = ['', 'edit', 'delete']
    do_not_call_in_templates = True

    def __init__(self, mail_template_id):
        try:
            self.mail_template = MailTemplate.get(mail_template_id)
        except KeyError:
            raise errors.TraversalError()
        get_response().breadcrumb.append((mail_template_id + '/', self.mail_template.name))

    def _q_index(self):
        html_top('mail_templates', title=self.mail_template.name)
        return template.QommonTemplateResponse(
                templates=['wcs/backoffice/mail-template.html'],
                context={'view': self, 'mail_template': self.mail_template})

    def get_form(self):
        form = Form(enctype='multipart/form-data', advanced_label=_('Additional options'))
        form.add(StringWidget, 'name', title=_('Name'), required=True, size=30,
                value=self.mail_template.name)
        form.add(TextWidget, 'description', title=_('Description'),
                cols=80, rows=3,
                value=self.mail_template.description)
        form.add(StringWidget, 'subject', title=_('Subject'), required=True, size=40,
                value=self.mail_template.subject,
                 validation_function=ComputedExpressionWidget.validate_template)
        form.add(TextWidget, 'body', title=_('Body'),
                 value=self.mail_template.body, cols=80, rows=15, require=True,
                 validation_function=ComputedExpressionWidget.validate_template)

        if self.mail_template.slug and not self.mail_template.is_in_use():
            form.add(StringWidget, 'slug',
                    value=self.mail_template.slug,
                    title=_('Identifier'),
                    required=True, advanced=True,
                    )

        form.add(WidgetList, 'attachments',
                title=_('Attachments (Python expressions)'),
                element_type=StringWidget,
                value=self.mail_template.attachments,
                add_element_label=_('Add attachment'),
                element_kwargs={'render_br': False, 'size': 50},
                advanced=True)

        form.add_submit('submit', _('Submit'))
        form.add_submit('cancel', _('Cancel'))
        return form

    def submit_form(self, form):
        name = form.get_widget('name').parse()
        slug_widget = form.get_widget('slug')
        if slug_widget:
            slug = form.get_widget('slug').parse()

        for mail_template in MailTemplate.select():
            if mail_template.id == self.mail_template.id:
                continue
            if slug_widget and slug == mail_template.slug:
                slug_widget.set_error(_('This value is already used.'))
        if form.has_errors():
            raise ValueError()

        self.mail_template.name = name
        self.mail_template.description = form.get_widget('description').parse()
        self.mail_template.subject = form.get_widget('subject').parse()
        self.mail_template.body = form.get_widget('body').parse()
        self.mail_template.attachments = form.get_widget('attachments').parse()
        if slug_widget:
            self.mail_template.slug = slug
        self.mail_template.store()

    def edit(self):
        form = self.get_form()
        if form.get_submit() == 'cancel':
            return redirect('.')

        if form.get_submit() == 'submit' and not form.has_errors():
            try:
                self.submit_form(form)
            except ValueError:
                pass
            else:
                return redirect('.')

        get_response().breadcrumb.append(('edit', _('Edit')))
        html_top('mail_templates', title=_('Edit Mail Template'))
        r = TemplateIO(html=True)
        r += htmltext('<h2>%s</h2>') % _('Edit Mail Template')
        r += form.render()
        r += get_publisher().substitutions.get_substitution_html_table()

        return r.getvalue()

    def delete(self):
        form = Form(enctype='multipart/form-data')
        if not self.mail_template.is_in_use():
            form.widgets.append(HtmlWidget('<p>%s</p>' % _(
                        'You are about to irrevocably delete this mail template.')))
            form.add_submit('delete', _('Submit'))
        else:
            form.widgets.append(HtmlWidget('<p>%s</p>' % _(
                        'This mail template is still used, it cannot be deleted.')))
        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('mail_templates', title=_('Delete Mail Template'))
            r = TemplateIO(html=True)
            r += htmltext('<h2>%s %s</h2>') % (_('Deleting Mail Template:'), self.mail_template.name)
            r += form.render()
            return r.getvalue()
        else:
            self.mail_template.remove_self()
            return redirect('..')
