from __future__ import print_function
# -----------------------------------------------------------------------
# OpenXenManager
#
# Copyright (C) 2009 Alberto Gonzalez Rodriguez alberto@pesadilla.org
# Copyright (C) 2014 Daniel Lintott <daniel@serverb.co.uk>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
# USA.
#
# -----------------------------------------------------------------------


class oxcSERVERhostnics:
    def delete_nic(self, ref_nic, ref_vm, delete_network=True):
        ref_bond = self.all['PIF'][ref_nic]['bond_master_of'][0]
        ref_network = self.all['PIF'][ref_nic]['network']
        res = self.connection.Bond.destroy(self.session_uuid, ref_bond)
        if "Value" in res:
            self.track_tasks[res['Value']] = ref_vm
        else:
            print(res)
        if delete_network:
            res = self.connection.network.destroy(self.session_uuid,
                                                  ref_network)
            if "Value" in res:
                self.track_tasks[res['Value']] = ref_vm
            else:
                print(res)

    def fill_available_nics(self, list1, list2):
        list1.clear()
        list2.clear()
        for pif_key in self.all['PIF'].keys():
            if self.all['PIF'][pif_key]['metrics'] != "OpaqueRef:NULL":
                pif_metric = {}
                if self.all['PIF'][pif_key]['metrics'] in \
                        self.all['PIF_metrics']:
                    pif_metric = self.all['PIF_metrics'][self.all['PIF'][pif_key]['metrics']]
                else:
                    pif_metric["pci_bus_path"] = "N/A"
            pif = self.all['PIF'][pif_key]
            if self.all['PIF'][pif_key]['metrics'] != "OpaqueRef:NULL" \
                    and pif_metric['pci_bus_path'] != "N/A":
                nic = "NIC %s" % pif['device'][-1:]
                error = ""
                if len(self.all['network'][pif['network']]['VIFs']):
                    error = "in use by VMs"
                if pif['bond_slave_of'] != "OpaqueRef:NULL" \
                        and pif['bond_slave_of'] in self.all['Bond']:
                    devices = []
                    for slave in \
                            self.all['Bond'][pif['bond_slave_of']]['slaves']:
                            devices.append(self.all['PIF'][slave]['device'][-1:])
                    devices.sort()
                    error = "already in Bond %s" % ('+'.join(devices))
                list1.append([pif_key, nic, error, error == ""])

    def create_bond(self, ref, ref2, name, name2, auto=False):
        network_cfg = {'uuid': '',
                       'name_label': "Bond %s+%s" % (name[-1:], name2[-1:]),
                       'name_description': '',
                       'VIFs': [],
                       'PIFs': [],
                       'other_config': {'XenCenterCreateInProgress': "true"},
                       'bridge': '',
                       'blobs': {}}
        if auto:
            network_cfg['other_config']['automatic'] = "true"
        else:
            network_cfg['other_config']['automatic'] = "false"
        res = self.connection.network.create(self.session_uuid, network_cfg)
        if "Value" in res:
            self.track_tasks[res['Value']] = ref
            network = res['Value']
        else:
            print(res)

        res = self.connection.Async.Bond.create(self.session_uuid,
                                                network, [ref, ref2], "")
        if "Value" in res:
            self.track_tasks[res['Value']] = ref
        else:
            print(res)

    def fill_nic_info(self, ref):
        pif = self.all['PIF'][ref]
        pif_metric = self.all['PIF_metrics'][self.all['PIF'][ref]['metrics']]
        if pif_metric['duplex']:
            duplex = "full"
        else:
            duplex = "half"
        if "mac" in pif:
                mac = pif['mac']
        else:
                mac = ""
        connected = "Disconnected"
        if pif_metric['carrier']:
                connected = "Connected"
        labels = {}
        labels['lblnicname'] = "NIC %s" % pif['device'][-1:]
        labels['lblnicvendor'] = pif_metric['vendor_name']
        labels['lblnicdevice'] = pif_metric['device_name']
        labels['lblnicmac'] = mac
        labels['lblnicpcibus'] = pif_metric['pci_bus_path']
        labels['lblniclinkstatus'] = connected
        if connected == "Connected":
            labels['lblnicspeed'] = pif_metric['speed'] + " mbit/s"
        else:
            labels['lblnicspeed'] = ""
        labels['lblnicduplex'] = duplex
        for label in labels.keys():
            self.wine.builder.get_object(label).set_label(labels[label])
