from django import forms
from django.forms.utils import ErrorList

from crits.campaigns.campaign import Campaign
from crits.core.forms import add_bucketlist_to_form, add_ticket_to_form
from crits.core.handlers import get_item_names, get_source_names
from crits.core.user_tools import get_user_organization
from crits.core import form_consts

from crits.vocabulary.relationships import RelationshipTypes


relationship_choices = [(c, c) for c in RelationshipTypes.values(sort=True)]


class AddBackdoorForm(forms.Form):
    """
    Django form for adding a Backdoor to CRITs.
    """

    error_css_class = 'error'
    required_css_class = 'required'

    name = forms.CharField(label=form_consts.Backdoor.NAME, required=True)
    aliases = forms.CharField(label=form_consts.Backdoor.ALIASES,
                              required=False)
    version = forms.CharField(label=form_consts.Backdoor.VERSION,
                                  required=False)
    description = forms.CharField(label=form_consts.Backdoor.DESCRIPTION,
                                  required=False)
    campaign = forms.ChoiceField(widget=forms.Select,
                                 label=form_consts.Backdoor.CAMPAIGN,
                                 required=False)
    confidence = forms.ChoiceField(label=form_consts.Backdoor.CAMPAIGN_CONFIDENCE,
                                   required=False)
    source = forms.ChoiceField(widget=forms.Select(attrs={'class': 'bulknoinitial'}),
                               label=form_consts.Backdoor.SOURCE,
                               required=True)
    source_method = forms.CharField(label=form_consts.Backdoor.SOURCE_METHOD,
                                    required=False)
    source_reference = forms.CharField(widget=forms.TextInput(attrs={'size': '90'}),
                                       label=form_consts.Backdoor.SOURCE_REFERENCE,
                                       required=False)
    related_id = forms.CharField(widget=forms.HiddenInput(), required=False, label=form_consts.Common.RELATED_ID)
    related_type = forms.CharField(widget=forms.HiddenInput(), required=False, label=form_consts.Common.RELATED_TYPE)
    relationship_type = forms.ChoiceField(required=False,
                                          label=form_consts.Common.RELATIONSHIP_TYPE,
                                          widget=forms.Select(attrs={'id':'relationship_type'}))


    def __init__(self, username, *args, **kwargs):
        super(AddBackdoorForm, self).__init__(*args, **kwargs)

        self.fields['campaign'].choices = [('', '')] + [
            (c.name, c.name) for c in get_item_names(Campaign, True)]
        self.fields['confidence'].choices = [
            ('', ''),
            ('low', 'low'),
            ('medium', 'medium'),
            ('high', 'high')]
        self.fields['source'].choices = [
            (c.name, c.name) for c in get_source_names(True, True, username)]
        self.fields['source'].initial = get_user_organization(username)
        self.fields['relationship_type'].choices = relationship_choices
        self.fields['relationship_type'].initial = RelationshipTypes.RELATED_TO

        add_bucketlist_to_form(self)
        add_ticket_to_form(self)

    def clean(self):
        cleaned_data = super(AddBackdoorForm, self).clean()
        campaign = cleaned_data.get('campaign')

        if campaign:
            confidence = cleaned_data.get('confidence')

            if not confidence or confidence == '':
                self._errors.setdefault('confidence', ErrorList())
                self._errors['confidence'].append(u'This field is required if campaign is specified.')

        return cleaned_data

[(24, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (25, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (27, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (29, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (31, 'django.forms.ChoiceField', 'forms.ChoiceField', False, 'from django import forms\n'), (34, 'django.forms.ChoiceField', 'forms.ChoiceField', False, 'from django import forms\n'), (39, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (13, 'crits.vocabulary.relationships.RelationshipTypes.values', 'RelationshipTypes.values', False, 'from crits.vocabulary.relationships import RelationshipTypes\n'), (63, 'crits.core.user_tools.get_user_organization', 'get_user_organization', False, 'from crits.core.user_tools import get_user_organization\n'), (67, 'crits.core.forms.add_bucketlist_to_form', 'add_bucketlist_to_form', False, 'from crits.core.forms import add_bucketlist_to_form, add_ticket_to_form\n'), (68, 'crits.core.forms.add_ticket_to_form', 'add_ticket_to_form', False, 'from crits.core.forms import add_bucketlist_to_form, add_ticket_to_form\n'), (36, 'django.forms.Select', 'forms.Select', False, 'from django import forms\n'), (41, 'django.forms.TextInput', 'forms.TextInput', False, 'from django import forms\n'), (44, 'django.forms.HiddenInput', 'forms.HiddenInput', False, 'from django import forms\n'), (45, 'django.forms.HiddenInput', 'forms.HiddenInput', False, 'from django import forms\n'), (48, 'django.forms.Select', 'forms.Select', False, 'from django import forms\n'), (62, 'crits.core.handlers.get_source_names', 'get_source_names', False, 'from crits.core.handlers import get_item_names, get_source_names\n'), (55, 'crits.core.handlers.get_item_names', 'get_item_names', False, 'from crits.core.handlers import get_item_names, get_source_names\n'), (78, 'django.forms.utils.ErrorList', 'ErrorList', False, 'from django.forms.utils import ErrorList\n')]