# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301  USA

from quixote import get_publisher

from wcs.fields import WidgetField, register_field_class
from qommon.form import *

class VoteAnonymityWidget(CheckboxWidget):
    vote_anonymity = 'anonymous'

    def __init__(self, name, value = None, elements = None, **kwargs):
        if kwargs and kwargs.get('vote_anonymity'):
            self.vote_anonymity = kwargs.get('vote_anonymity')
        if kwargs and 'required' in kwargs:
            del kwargs['required']
        CheckboxWidget.__init__(self, name, value=value, elements=elements, **kwargs)

    def render(self):
        if self.vote_anonymity != 'choice':
            # XXX: include info for user ?
            return ''
        return CheckboxWidget.render(self)

    def render_title(self, title):
        return CheckboxWidget.render_title(self, _('Anonymous Voting'))

    def render_content(self):
        value = True
        if self.value:
            if self.value[0] == 'anonymous':
                value = True
            else:
                value = False
        return htmltag('input', xml_end=True,
                       type='checkbox',
                       name=self.name,
                       value='yes',
                       checked=value and 'checked' or None,
                       **self.attrs)

    def _parse(self, request):
        if self.vote_anonymity == 'anonymous':
            self.value = ('anonymous', None)
        elif self.vote_anonymity == 'public':
            self.value = ('public', request.user.id)
        else:
            if type(request.form.get(self.name)) is tuple:
                self.value = request.form.get(self.name)
            else:
                CheckboxWidget._parse(self, request)
                if self.value is True:
                    self.value = ('anonymous', None)
                else:
                    self.value = ('public', request.user.id)


class VoteAnonymityField(WidgetField):
    key = 'vote-anonymity'
    description = N_('Vote Anonymity')

    widget_class = VoteAnonymityWidget
    vote_anonymity = 'anonymous'
        # possible values:
        #  'anonymous': anonymous vote
        #  'public': public vote
        #  'choice': anonymous or public, choice of voter

    def fill_admin_form(self, form):
        form.add(StringWidget, 'label', title = _('Label'), value = self.label,
                required = True, size = 50)
        form.add(SingleSelectWidget, 'vote_anonymity', title = _('Vote Anonymity'),
                value = self.vote_anonymity,
                options = [('anonymous', _('Anonymous Vote')),
                    ('public', _('Public Vote')),
                    ('choice', _('Choice of Voter'))])

    def get_admin_attributes(self):
        return ['label', 'vote_anonymity']

    def perform_more_widget_changes(self, form, kwargs, edit = True):
        kwargs['vote_anonymity'] = self.vote_anonymity

    def get_view_value(self, value):
        public = False
        if self.vote_anonymity == 'choice':
            if value and value[0] == 'public':
                public = True
        if public:
            return get_publisher().user_class.get(value[1]).get_display_name()
        else:
            return _('Anonymous') # XXX: include token?

register_field_class(VoteAnonymityField)
