# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# 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 3 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, see <http://www.gnu.org/licenses/>.

"""
Configuration page for VMware VM preferences.
"""

import copy

from gns3.qt import QtCore, QtGui, QtWidgets
from gns3.main_window import MainWindow
from gns3.dialogs.configuration_dialog import ConfigurationDialog

from .. import VMware
from ..settings import VMWARE_VM_SETTINGS
from ..ui.vmware_vm_preferences_page_ui import Ui_VMwareVMPreferencesPageWidget
from ..pages.vmware_vm_configuration_page import VMwareVMConfigurationPage
from ..dialogs.vmware_vm_wizard import VMwareVMWizard


class VMwareVMPreferencesPage(QtWidgets.QWidget, Ui_VMwareVMPreferencesPageWidget):

    """
    QWidget preference page for VMware VM preferences.
    """

    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self._main_window = MainWindow.instance()
        self._vmware_vms = {}
        self._items = []

        self.uiNewVMwareVMPushButton.clicked.connect(self._vmwareVMNewSlot)
        self.uiEditVMwareVMPushButton.clicked.connect(self._vmwareVMEditSlot)
        self.uiDeleteVMwareVMPushButton.clicked.connect(self._vmwareVMDeleteSlot)
        self.uiVMwareVMsTreeWidget.itemSelectionChanged.connect(self._vmwareVMChangedSlot)

    def _createSectionItem(self, name):

        section_item = QtWidgets.QTreeWidgetItem(self.uiVMwareVMInfoTreeWidget)
        section_item.setText(0, name)
        font = section_item.font(0)
        font.setBold(True)
        section_item.setFont(0, font)
        return section_item

    def _refreshInfo(self, vmware_vm):

        self.uiVMwareVMInfoTreeWidget.clear()

        # fill out the General section
        section_item = self._createSectionItem("General")
        QtWidgets.QTreeWidgetItem(section_item, ["VM name:", vmware_vm["name"]])
        QtWidgets.QTreeWidgetItem(section_item, ["Server:", vmware_vm["server"]])
        QtWidgets.QTreeWidgetItem(section_item, ["Remote console enabled:", "{}".format(vmware_vm["enable_remote_console"])])
        QtWidgets.QTreeWidgetItem(section_item, ["Headless mode enabled:", "{}".format(vmware_vm["headless"])])
        QtWidgets.QTreeWidgetItem(section_item, ["ACPI shutdown enabled:", "{}".format(vmware_vm["acpi_shutdown"])])
        QtWidgets.QTreeWidgetItem(section_item, ["Linked base VM:", "{}".format(vmware_vm["linked_base"])])

        # fill out the Network section
        section_item = self._createSectionItem("Network")
        QtWidgets.QTreeWidgetItem(section_item, ["Adapters:", str(vmware_vm["adapters"])])
        QtWidgets.QTreeWidgetItem(section_item, ["Name format:", vmware_vm["port_name_format"]])
        if vmware_vm["port_segment_size"]:
            QtWidgets.QTreeWidgetItem(section_item, ["Segment size:", str(vmware_vm["port_segment_size"])])
        if vmware_vm["first_port_name"]:
            QtWidgets.QTreeWidgetItem(section_item, ["First port name:", vmware_vm["first_port_name"]])
        QtWidgets.QTreeWidgetItem(section_item, ["Use uBridge:", "{}".format(vmware_vm["use_ubridge"])])
        QtWidgets.QTreeWidgetItem(section_item, ["Use any adapter:", "{}".format(vmware_vm["use_any_adapter"])])
        QtWidgets.QTreeWidgetItem(section_item, ["Type:", vmware_vm["adapter_type"]])

        self.uiVMwareVMInfoTreeWidget.expandAll()
        self.uiVMwareVMInfoTreeWidget.resizeColumnToContents(0)
        self.uiVMwareVMInfoTreeWidget.resizeColumnToContents(1)

    def _vmwareVMChangedSlot(self):
        """
        Loads a selected VMware VM from the tree widget.
        """

        selection = self.uiVMwareVMsTreeWidget.selectedItems()
        self.uiDeleteVMwareVMPushButton.setEnabled(len(selection) != 0)
        single_selected = len(selection) == 1
        self.uiEditVMwareVMPushButton.setEnabled(single_selected)

        if single_selected:
            key = selection[0].data(0, QtCore.Qt.UserRole)
            vmware_vm = self._vmware_vms[key]
            self._refreshInfo(vmware_vm)
        else:
            self.uiVMwareVMInfoTreeWidget.clear()

    def _vmwareVMNewSlot(self):
        """
        Creates a new VMware VM.
        """

        wizard = VMwareVMWizard(self._vmware_vms, parent=self)
        wizard.show()
        if wizard.exec_():

            new_vm_settings = wizard.getSettings()
            key = "{server}:{name}".format(server=new_vm_settings["server"], name=new_vm_settings["name"])
            self._vmware_vms[key] = VMWARE_VM_SETTINGS.copy()
            self._vmware_vms[key].update(new_vm_settings)

            item = QtWidgets.QTreeWidgetItem(self.uiVMwareVMsTreeWidget)
            item.setText(0, self._vmware_vms[key]["name"])
            item.setIcon(0, QtGui.QIcon(self._vmware_vms[key]["symbol"]))
            item.setData(0, QtCore.Qt.UserRole, key)
            self._items.append(item)
            self.uiVMwareVMsTreeWidget.setCurrentItem(item)

    def _vmwareVMEditSlot(self):
        """
        Edits a VMware VM.
        """

        item = self.uiVMwareVMsTreeWidget.currentItem()
        if item:
            key = item.data(0, QtCore.Qt.UserRole)
            vmware_vm = self._vmware_vms[key]
            dialog = ConfigurationDialog(vmware_vm["name"], vmware_vm, VMwareVMConfigurationPage(), parent=self)
            dialog.show()
            if dialog.exec_():
                # update the icon
                item.setIcon(0, QtGui.QIcon(vmware_vm["symbol"]))
                if vmware_vm["name"] != item.text(0):
                    new_key = "{server}:{name}".format(server=vmware_vm["server"], name=vmware_vm["name"])
                    if new_key in self._vmware_vms:
                        QtWidgets.QMessageBox.critical(self, "VMware VM", "VMware VM name {} already exists for server {}".format(vmware_vm["name"],
                                                                                                                                  vmware_vm["server"]))
                        vmware_vm["name"] = item.text(0)
                        return
                    self._vmware_vms[new_key] = self._vmware_vms[key]
                    del self._vmware_vms[key]
                    item.setText(0, vmware_vm["name"])
                    item.setData(0, QtCore.Qt.UserRole, new_key)
                self._refreshInfo(vmware_vm)

    def _vmwareVMDeleteSlot(self):
        """
        Deletes a VMware VM.
        """

        for item in self.uiVMwareVMsTreeWidget.selectedItems():
            if item:
                key = item.data(0, QtCore.Qt.UserRole)
                del self._vmware_vms[key]
                self.uiVMwareVMsTreeWidget.takeTopLevelItem(self.uiVMwareVMsTreeWidget.indexOfTopLevelItem(item))

    def loadPreferences(self):
        """
        Loads the VMware VM preferences.
        """

        vmware_module = VMware.instance()
        self._vmware_vms = copy.deepcopy(vmware_module.vmwareVMs())
        self._items.clear()

        for key, vmware_vm in self._vmware_vms.items():
            item = QtWidgets.QTreeWidgetItem(self.uiVMwareVMsTreeWidget)
            item.setText(0, vmware_vm["name"])
            item.setIcon(0, QtGui.QIcon(vmware_vm["symbol"]))
            item.setData(0, QtCore.Qt.UserRole, key)
            self._items.append(item)

        if self._items:
            self.uiVMwareVMsTreeWidget.setCurrentItem(self._items[0])
            self.uiVMwareVMsTreeWidget.sortByColumn(0, QtCore.Qt.AscendingOrder)

    def savePreferences(self):
        """
        Saves the VMware VM preferences.
        """

        VMware.instance().setVMwareVMs(self._vmware_vms)
