# Copyright 2012 NEC Corporation
#
#    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.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _

from horizon import exceptions
from horizon import forms
from horizon import tabs
from horizon.utils import memoized

from openstack_dashboard import api

from openstack_dashboard.dashboards.project.networks.ports \
    import forms as project_forms
from openstack_dashboard.dashboards.project.networks.ports \
    import tables as project_tables
from openstack_dashboard.dashboards.project.networks.ports \
    import tabs as project_tabs

STATE_DICT = dict(project_tables.DISPLAY_CHOICES)
STATUS_DICT = dict(project_tables.STATUS_DISPLAY_CHOICES)
VNIC_TYPES = dict(project_forms.VNIC_TYPES)


class CreateView(forms.ModalFormView):
    form_class = project_forms.CreatePort
    form_id = "create_port_form"
    modal_header = _("Create Port")
    submit_label = _("Create Port")
    submit_url = "horizon:project:networks:addport"
    page_title = _("Create Port")
    template_name = 'project/networks/ports/create.html'
    url = 'horizon:project:networks:detail'

    def get_success_url(self):
        return reverse(self.url,
                       args=(self.kwargs['network_id'],))

    @memoized.memoized_method
    def get_network(self):
        try:
            network_id = self.kwargs["network_id"]
            return api.neutron.network_get(self.request, network_id)
        except Exception:
            redirect = reverse(self.url,
                               args=(self.kwargs['network_id'],))
            msg = _("Unable to retrieve network.")
            exceptions.handle(self.request, msg, redirect=redirect)

    def get_context_data(self, **kwargs):
        context = super(CreateView, self).get_context_data(**kwargs)
        context['network'] = self.get_network()
        args = (self.kwargs['network_id'],)
        context['submit_url'] = reverse(self.submit_url, args=args)
        context['cancel_url'] = reverse(self.url, args=args)
        return context

    def get_initial(self):
        network = self.get_network()
        return {"network_id": self.kwargs['network_id'],
                "network_name": network.name}


class DetailView(tabs.TabbedTableView):
    tab_group_class = project_tabs.PortDetailTabs
    template_name = 'horizon/common/_detail.html'
    page_title = "{{ port.name|default:port.id }}"

    @memoized.memoized_method
    def get_data(self):
        port_id = self.kwargs['port_id']

        try:
            port = api.neutron.port_get(self.request, port_id)
            port.admin_state_label = STATE_DICT.get(port.admin_state,
                                                    port.admin_state)
            port.status_label = STATUS_DICT.get(port.status,
                                                port.status)
            if port.get('binding__vnic_type'):
                port.binding__vnic_type = VNIC_TYPES.get(
                    port.binding__vnic_type, port.binding__vnic_type)
        except Exception:
            port = []
            redirect = self.get_redirect_url()
            msg = _('Unable to retrieve port details.')
            exceptions.handle(self.request, msg, redirect=redirect)

        if (api.neutron.is_extension_supported(self.request, 'mac-learning')