# -*- coding: utf-8 -*-
#
# w.c.s. - web application for online forms
# Copyright (C) 2005-2019  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/>.

import time

from quixote import get_publisher, get_request, get_response, get_session, redirect
from quixote.html import TemplateIO, htmltext

from ..qommon import _, misc
from ..qommon.misc import C_

from wcs.carddef import CardDef
from wcs.roles import Role
from wcs.workflows import Workflow
from wcs.admin.forms import FormsDirectory, FormDefPage, FormDefUI, html_top
from wcs.admin.logged_errors import LoggedErrorsDirectory


class CardDefUI(FormDefUI):
    formdef_class = CardDef

    def get_categories(self):
        return []


class CardDefPage(FormDefPage):
    formdef_class = CardDef
    formdef_export_prefix = 'card'
    formdef_ui_class = CardDefUI
    formdef_default_workflow = '_carddef_default'

    delete_message = N_('You are about to irrevocably delete this card model.')
    delete_title = N_('Deleting Card Model:')
    overwrite_message = N_(
        'You can replace this card model by uploading a file '
        'or by pointing to a form URL.')
    overwrite_success_message = N_(
        'The card model has been successfully overwritten. '
        'Do note it kept its existing address and role and workflow parameters.')

    def html_top(self, title):
        return html_top('cards', title)

    def _q_index(self):
        self.html_top(title=self.formdef.name)
        r = TemplateIO(html=True)
        get_response().filter['sidebar'] = self.get_sidebar()
        get_response().add_javascript(['jquery.js', 'widget_list.js', 'qommon.wysiwyg.js'])

        r += htmltext('<div id="appbar">')
        r += htmltext('<h2>%s</h2>') % self.formdef.name
        r += htmltext('<span class="actions">')
        r += htmltext('<a rel="popup" href="title">%s</a>') % _('change title')
        r += htmltext('</span>')
        r += htmltext('</div>')

        if self.formdef.last_modification_time:
            warning_class = ''
            if (time.time() - time.mktime(self.formdef.last_modification_time)) < 600:
                if get_request().user and str(get_request().user.id) != self.formdef.last_modification_user_id:
                    warning_class = 'recent'
            r += htmltext('<p class="last-modification %s">') % warning_class
            r += _('Last Modification:')
            r += ' '
            r += misc.localstrftime(self.formdef.last_modification_time)
            r += ' '
            if self.formdef.last_modification_user_id:
                try:
                    r += _('by %s') % get_publisher().user_class.get(
                                    self.formdef.last_modification_user_id).display_name
                except KeyError:
                    pass
            r += htmltext('</p>')

        r += get_session().display_message()

        def add_option_line(link, label, current_value):
            return htmltext(
                    '<li><a rel="popup" href="%(link)s">'
                    '<span class="label">%(label)s</span> '
                    '<span class="value">%(current_value)s</span>'
                    '</a></li>' % {
                              'link': link,
                              'label': label,
                              'current_value': current_value})

        r += htmltext('<div class="splitcontent-left">')
        r += htmltext('<div class="bo-block">')
        r += htmltext('<h3>%s</h3>') % _('Workflow')
        r += htmltext('<ul class="biglist optionslist">')

        if get_publisher().get_backoffice_root().is_accessible('workflows'):
            # custom option line to also include a link to the workflow itself.
            r += htmltext(
                '<li><a rel="popup" href="%(link)s">'
                '<span class="label">%(label)s</span> '
                '<span class="value offset">%(current_value)s</span>'
                '</a>'
                '<a class="extra-link" title="%(title)s" href="../../workflows/%(workflow_id)s/">↗</a>'
                '</li>' % {
                          'link': 'workflow',
                          'label': _('Workflow'),
                          'title': _('Open workflow page'),
                          'workflow_id': self.formdef.workflow.id,
                          'current_value': self.formdef.workflow.name or '-'})
        else:
            r += add_option_line('workflow', _('Workflow'),
                    self.formdef.workflow and self.formdef.workflow.name or '-')

        if self.formdef.workflow_id:
            pristine_workflow = Workflow.get(self.formdef.workflow_id)
            if pristine_workflow.variables_formdef:
                r += add_option_line('workflow-variables', _('Options'), '')

        r += add_option_line('backoffice-submission-roles',
                _('Creation Role'),
                self._get_roles_label('backoffice_submission_roles'))

        if self.formdef.workflow.roles:
            if not self.formdef.workflow_roles:
                self.formdef.workflow_roles = {}
            for (wf_role_id, wf_role_label) in self.formdef.workflow.roles.items():
                role_id = self.formdef.workflow_roles.get(wf_role_id)
                if role_id:
                    try:
                        role = Role.get(role_id)
                        role_label = role.name
                    except KeyError:
                        # removed role ?
                        role_label = _('Unknown role (%s)') % role_id
                else:
                    role_label = '-'
                r += add_option_line('role/%s' % wf_role_id,
                        wf_role_label, role_label)

        r += htmltext('</ul>')
        r += htmltext('</div>')
        r += htmltext('</div>')

        r += htmltext('<div class="splitcontent-right">')
        r += htmltext('<div class="bo-block">')
        r += htmltext('<h3>%s</h3>') % _('Options')
        r += htmltext('<ul class="biglist optionslist">')

        r += add_option_line('options/geolocations',
                _('Geolocation'),
                self.formdef.geolocations and
                C_('geolocation|Enabled') or C_('geolocation|Disabled'))

        if self.formdef.digest_template:
            digest_template_status = C_('template|Custom')
        else:
            digest_template_status = C_('template|None')
        r += add_option_line('options/templates',
                _('Digest Template'), digest_template_status)

        r += htmltext('</ul>')
        r += htmltext('</div>')
        r += htmltext('</div>')

        r += htmltext('<div class="bo-block clear">')
        r += htmltext('<h3 class="clear">%s <span class="change">(<a href="fields/">%s</a>)</span></h3>') % (
                        _('Fields'), _('edit'))
        r += self.get_preview()
        r += htmltext('</div>')
        return r.getvalue()

    def duplicate(self):
        response = super(CardDefPage, self).duplicate()
        self.formdefui.formdef.disabled = False
        self.formdefui.formdef.store()
        return response

    def get_sidebar(self):
        r = TemplateIO(html=True)
        r += htmltext('<ul id="sidebar-actions">')
        r += htmltext('<li><a href="delete" rel="popup">%s</a></li>') % _('Delete')
        r += htmltext('<li><a href="duplicate">%s</a></li>') % _('Duplicate')
        r += htmltext('<li><a rel="popup" href="overwrite">%s</a></li>') % _(
                'Overwrite with new import')
        r += htmltext('<li><a href="export">%s</a></li>') % _('Export')
        r += htmltext('</ul>')

        r += LoggedErrorsDirectory.errors_block(formdef_id=self.formdef.id)
        return r.getvalue()


class CardsDirectory(FormsDirectory):
    _q_exports = ['', 'new', ('import', 'p_import')]
    formdef_class = CardDef
    formdef_page_class = CardDefPage
    formdef_ui_class = CardDefUI

    import_title = N_('Import Card Model')
    import_submit_label = N_('Import Card Model')
    import_paragraph = N_(
        'You can install a new card model by uploading a file '
        'or by pointing to the card model URL.')
    import_loading_error_message = N_('Error loading card model (%s).')
    import_success_message = N_(
        'This card model has been successfully imported. ')
    import_error_message = N_(
        'Imported card model contained errors and has been automatically fixed, '
        'you should nevertheless check everything is ok. ')

    def html_top(self, title):
        return html_top('cards', title)

    def _q_index(self):
        get_response().breadcrumb.append(('cards/', _('Card Models')))
        self.html_top(title=_('Card Models'))
        r = TemplateIO(html=True)

        r += htmltext('<div id="appbar">')
        r += htmltext('<h2>%s</h2>') % _('Card Models')
        r += htmltext('<span class="actions">')
        r += htmltext('<a href="import" rel="popup">%s</a>') % _('Import')
        r += htmltext('<a class="new-item" href="new" rel="popup">%s</a>') % _('New Card Model')
        r += htmltext('</span>')
        r += htmltext('</div>')

        formdefs = self.formdef_class.select(order_by='name', ignore_errors=True, lightweight=True)
        r += self.form_list(formdefs, title='')
        return r.getvalue()

    def new(self):
        get_response().breadcrumb.append(('cards/', _('Card Models')))
        get_response().breadcrumb.append(('new', _('New')))
        formdefui = self.formdef_ui_class(None)
        form = formdefui.new_form_ui()
        if form.get_widget('cancel').parse():
            return redirect('.')

        if form.is_submitted() and not form.has_errors():
            try:
                formdef = formdefui.submit_form(form)
                formdef.disabled = False
                formdef.store()
            except ValueError:
                pass
            else:
                return redirect(str(formdef.id) + '/')

        self.html_top(title=_('New Card Model'))
        r = TemplateIO(html=True)
        r += htmltext('<h2>%s</h2>') % _('New Card Model')
        r += form.render()
        return r.getvalue()

    def import_submit(self, form):
        response = super(CardsDirectory, self).import_submit(form)
        if self.imported_formdef:
            self.imported_formdef.disabled = False
            self.imported_formdef.store()
        return response

    def _q_lookup(self, component):
        get_response().breadcrumb.append(('cards/', _('Card Models')))
        return self.formdef_page_class(component)
