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

# Copyright 2011 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2011 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.

"""
Views for managing Quantum networks.
"""

import logging
import warnings

from django import shortcuts
from django import template
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.utils.translation import ugettext as _

from horizon import api
from horizon.dashboards.nova.networks.forms import (CreateNetwork,
        DeleteNetwork, RenameNetwork, AttachPort, CreatePort, DeletePort,
        DetachPort, TogglePort)


LOG = logging.getLogger(__name__)


def index(request):
    tenant_id = request.user.tenant_id
    delete_form, delete_handled = DeleteNetwork.maybe_handle(request)

    networks = []
    instances = []

    try:
        networks_list = api.quantum_list_networks(request)
        details = []
        for network in networks_list['networks']:
            net_stats = _calc_network_stats(request, network['id'])
            # Get network details like name and id
            details = api.quantum_network_details(request, network['id'])
            networks.append({
                'name': details['network']['name'],
                'id': network['id'],
                'total': net_stats['total'],
                'available': net_stats['available'],
                'used': net_stats['used'],
                'tenant': tenant_id})

    except Exception, e:
        LOG.exception("Unable to get network list.")
        messages.error(request,
                       _('Unable to get network list: %s') % e.message)

    return shortcuts.render(request,
                            'nova/networks/index.html', {
                                'networks': networks,
                                'delete_form': delete_form})


def create(request):
    network_form, handled = CreateNetwork.maybe_handle(request)
    if handled:
        return shortcuts.redirect('horizon:nova:networks:index')

    return shortcuts.render(request,
                            'nova/networks/create.html',
                            {'network_form': network_form})


def detail(request, network_id):
    tenant_id = request.user.tenant_id
    delete_port_form, delete_handled = DeletePort.maybe_handle(request,
                                            initial={"network": network_id})
    detach_port_form, detach_handled = DetachPort.maybe_handle(request,
                                            initial={"network": network_id})
    toggle_port_form, port_toggle_handled = TogglePort.maybe_handle(request,
                                            initial={"network": network_id})

    network = {}
    network['id'] = network_id

    try:
        network_details = api.quantum_network_details(request, network_id)
        network['name'] = network_details['network']['name']
        network['ports'] = _get_port_states(request, network_id)
    except Exception, e:
        LOG.exception("Unable to get network details.")
        messages.error(request,