# w.c.s. - web application for online forms
# Copyright (C) 2005-2010  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 django.utils.six.moves.urllib import parse as urllib

from quixote import get_publisher
import quixote
from quixote.errors import *
from quixote.html import TemplateIO, htmltext

from . import template


class AccessForbiddenError(AccessError):
    def __init__(self, public_msg=None, private_msg=None, location_hint=None):
        AccessError.__init__(self, public_msg=public_msg, private_msg=private_msg)
        self.location_hint = location_hint

    def render(self):
        from . import _
        if self.public_msg:
            return template.error_page(self.public_msg, _('Access Forbidden'),
                    continue_to = (get_publisher().get_root_url(), _('the homepage')),
                    location_hint = self.location_hint)
        else:
            return template.error_page(_('Access Forbidden'),
                    continue_to = (get_publisher().get_root_url(), _('the homepage')),
                    location_hint = self.location_hint)


class UnknownNameIdAccessForbiddenError(AccessForbiddenError):
    pass


class AccessUnauthorizedError(AccessForbiddenError):
    def render(self):
        session = quixote.get_session()
        request = quixote.get_request()
        if request.user:
            return AccessForbiddenError.render(self)

        if self.public_msg:
            session.message = ('error', self.public_msg)
        login_url = get_publisher().get_root_url() + 'login/'
        login_url += '?' + urllib.urlencode({'next': request.get_frontoffice_url()})
        return quixote.redirect(login_url)

class EmailError(Exception):
    pass

class InternalServerError(object):
    def render(self):
        from . import _
        template.html_top(_('Oops, the server borked severely'))
        r = TemplateIO(html=True)

        r += htmltext('<p>')
        r += _('This is bad bad bad; perhaps you will have more luck if '
               'you retry in a few minutes ? ')
        r += _('Alternatively you could harass the webmaster (who may have '
               'been emailed automatically with this incident but you can\'t '
               'be sure about this.')
        r += htmltext('</p>')
        return r.getvalue()

class FormError(Exception):
    def __init__(self, field, msg):
        self.field = field
        self.msg = msg

    def __str__(self):
        return self.msg

class ConnectionError(Exception):
    def __init__(self, msg):
        self.msg = msg

    def __str__(self):
        return self.msg

class ConfigurationError(Exception):
    def __init__(self, msg):
        self.msg = msg

    def __str__(self):
        return self.msg

class LoginError(Exception):
    pass

class CertificateError(Exception):
    pass

class SMSError(Exception):
    pass


TraversalError.title = N_('Page not found')
TraversalError.description = N_(
    "The requested link does not exist on this site.  If "
    "you arrived here by following a link from an external "
    "page, please inform that page's maintainer.")


def format_publish_error(exc):
    from . import _
    if getattr(exc, 'public_msg', None):
        return template.error_page(exc.format(), _(exc.title))
    else:
        return template.error_page(_(exc.description), _(exc.title))
