# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from django.conf import settings
from django.core.urlresolvers import reverse
from django.core import validators
from django.forms import ValidationError
from django.utils.translation import ugettext_lazy as _

from horizon import exceptions
from horizon import forms
from horizon import messages
from horizon.utils import fields
from horizon.utils.validators import validate_port_range

from openstack_dashboard import api

from ..floating_ips.utils import get_int_or_uuid


class CreateGroup(forms.SelfHandlingForm):
    name = forms.CharField(label=_("Name"),
                           error_messages={
                            'required': _('This field is required.'),
                            'invalid': _("The string may only contain"
                                         " ASCII characters and numbers.")},
                           validators=[validators.validate_slug])
    description = forms.CharField(label=_("Description"))

    def handle(self, request, data):
        try:
            sg = api.nova.security_group_create(request,
                                                data['name'],
                                                data['description'])
            messages.success(request,
                             _('Successfully created security group: %s')
                               % data['name'])
            return sg
        except:
            redirect = reverse("horizon:project:access_and_security:index")
            exceptions.handle(request,
                              _('Unable to create security group.'),
                              redirect=redirect)


class AddRule(forms.SelfHandlingForm):
    id = forms.CharField(widget=forms.HiddenInput())
    ip_protocol = forms.ChoiceField(label=_('Rule'),
                                    widget=forms.Select(attrs={
                                            'class': 'switchable',
                                            'data-slug': 'protocol'}))

    port_or_range = forms.ChoiceField(label=_('Open'),
                                      choices=[('port', _('Port')),
                                               ('range', _('Port Range'))],
                                      widget=forms.Select(attrs={
                                            'class': 'switchable switched',
                                            'data-slug': 'range',
                                            'data-switch-on': 'protocol',
                                            'data-protocol-tcp': _('Open'),
                                            'data-protocol-udp': _('Open')}))

    port = forms.IntegerField(label=_("Port"),
                              required=False,
                              help_text=_("Enter an integer value "
                                          "between 1 and 65535."),
                              widget=forms.TextInput(attrs={
                                   'class': 'switched',
                                   'data-switch-on': 'range',
                                   'data-range-port': _('Port')}),
                              validators=[validate_port_range])

    from_port = forms.IntegerField(label=_("From Port"),
                                   required=False,
                                   help_text=_("Enter an integer value "
                                               "between 1 and 65535."),
                                   widget=forms.TextInput(attrs={
                                        'class': 'switched',
                                        'data-switch-on': 'range',
                                        'data-range-range': _('From Port')}),
                                   validators=[validate_port_range])

    to_port = forms.IntegerField(label=_("To Port"),
                                 required=False,
                                 help_text=_("Enter an integer value "
                                             "between 1 and 65535."),
                                 widget=forms.TextInput(attrs={
                                        'class': 'switched',
                                        'data-switch-on': 'range',
                                        'data-range-range': _('To Port')}),
                                 validators=[validate_port_range])

    icmp_type = forms.IntegerField(label=_("Type"),
                                   required=False,
                                   help_text=_("Enter a value for ICMP type "
                                               "in the range (-1: 255)"),
                                   widget=forms.TextInput(attrs={
                                        'class': 'switched',
                                        'data-switch-on': 'protocol',
                                        'data-protocol-icmp': _('Type')}),
                                   validators=[validate_port_range])

    icmp_code = forms.IntegerField(label=_("Code"),
                                   required=False,
                                   help_text=_("Enter a value for ICMP code "
                                               "in the range (-1: 255)"),
                                   widget=forms.TextInput(attrs={
                                          'class': 'switched',
                                          'data-switch-on': 'protocol',
                                          'data-protocol-icmp': _('Code')}),