# -*- coding: utf-8 -*-
"""policies for ITicketSubmitPolicy"""

from genshi.filters import Transformer
from interface import ITicketSubmitPolicy
from trac.core import *
from trac.ticket.api import TicketSystem
from trac.util.text import javascript_quote


class TicketRequires(Component):
    """bits for requiring a field"""
    implements(ITicketSubmitPolicy)

    def name(self):
        return 'requires'

    def label(self):
        return u'必須チェック'

    def javascript(self):
        return 'ticketsubmitpolicy/js/requires.js'

    def onload(self, policy, condition, *args):
        return

    def onsubmit(self, policy, condition, *requiredfields):
        fields = ','.join("'%s'" % javascript_quote(i) for i in requiredfields)
        labels = TicketSystem(self.env).get_ticket_field_labels()
        labels = ','.join(
            "'%s':'%s'" % (javascript_quote(name), javascript_quote(label))
            for name, label in labels.iteritems()
            if name in requiredfields)
        requires = "requires(%s,[%s],{%s});" % (policy, fields, labels)
        return requires

    def filter_stream(self, stream, policy, condition, *args):
        return stream


### 

class TicketExcludes(Component):
    """bits for exluding field under a condition"""
    implements(ITicketSubmitPolicy)

    def name(self):
        return 'excludes'

    def label(self):
        return u'非表示項目'

    def javascript(self):
        return 'ticketsubmitpolicy/js/exclude.js'

    def onload(self, policy, condition, *excludedfields):
        fields = repr([ str(i) for i in excludedfields ])
        return "exclude(%s, %s);" % (policy, fields )

    def onsubmit(self, policy, condition, *excludedfields):
        fields = repr([ str(i) for i in excludedfields ])
        excludesubmit = "excludeSubmit(%s, %s);" % (policy, fields)
        return excludesubmit

    def filter_stream(self, stream, policy, condition, *excludedfields):
#        fields = repr([ str(i) for i in excludedfields ])
#        exclude = "exclude(%s, %s)" % ( policy, fields )

        # XXX this is unsafe, in the case onchange is already specified on this field;
        # see http://trac-hacks.org/ticket/3128
#        for c in condition:
#            field = c['field']
#            stream |= Transformer("//select[@id='field-%s']" % field).attr('onchange', exclude)

        return stream

###

class TicketReadOnly(Component):
    """bits for readonly field"""
    implements(ITicketSubmitPolicy)

    def name(self):
        return 'readonly'

    def label(self):
        return u'入力禁止'

    def javascript(self):
        return 'ticketsubmitpolicy/js/readonly.js'

    def onload(self, policy, condition, *readonlyfields):
        fields = repr([ str(i) for i in readonlyfields ])
        return "readonly(%s, %s);" % (policy, fields )

    def onsubmit(self, policy, condition, *readonlyfields):
        fields = repr([ str(i) for i in readonlyfields ])
        readonly = "readonly(%s, %s);" % (policy, fields)
        return readonly

    def filter_stream(self, stream, policy, condition, *readonlyfields):
#        fields = repr([ str(i) for i in readonlyfields ])
#        readonly = "readonly(%s, %s)" % ( policy, fields )

        # XXX this is unsafe, in the case onchange is already specified on this field;
        # see http://trac-hacks.org/ticket/3128
#        for c in condition:
#            field = c['field']
#            stream |= Transformer("//select[@id='field-%s']" % field).attr('onchange', readonly)

        return stream

