# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'packager_GUI.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
from ExportProject import *
from ImportProject import *
import virtualbox


class Ui_ABS_Packager(object):

    def setupUi(self, ABS_Packager):
        self.ABS_Packager = ABS_Packager
        self.ABS_Packager.setObjectName("ABS_Packager")
        self.ABS_Packager.resize(662, 246)
        self.ABS_Packager.setMinimumSize(QtCore.QSize(662,220))
        font = QtGui.QFont()

        self.scroll_bar = QtWidgets.QScrollBar()
        self.scroll_bar.setStyleSheet("background: white;")
        self.scroll_bar2 = QtWidgets.QScrollBar()
        self.scroll_bar2.setStyleSheet("background: white;")

        self.machine_list = QtWidgets.QListWidget(self.ABS_Packager)
        self.machine_list.setGeometry(QtCore.QRect(30, 50, 231, 161))
        self.machine_list.setVerticalScrollBar(self.scroll_bar)
        self.machine_list.setStyleSheet("background-color: rgb(255, 255, 255); border-radius: 10px; border: 1px solid #D2D6E0; color: #13333F;")
        self.machine_list.setObjectName("machine_list")

        self.export_button = QtWidgets.QPushButton(self.ABS_Packager)
        self.export_button.setGeometry(QtCore.QRect(280, 140, 101, 61))

        font.setPointSize(12)
        self.export_button.setFont(font)
        self.export_button.setStyleSheet("background-color: #13333F; color: #FFFFFF; border-radius: 5px;")
        self.export_button.setObjectName("export_button")

        self.select_files_button = QtWidgets.QPushButton(self.ABS_Packager)
        self.select_files_button.setGeometry(QtCore.QRect(280, 60, 101, 61))
        font.setPointSize(11)
        self.select_files_button.setFont(font)
        self.select_files_button.setStyleSheet("background-color: #13333F; color: #FFFFFF; border-radius: 5px;")
        self.select_files_button.setObjectName("select_files_button")
        

        self.file_List = QtWidgets.QListWidget(self.ABS_Packager)
        self.file_List.setGeometry(QtCore.QRect(400, 50, 231, 161))
        self.file_List.setVerticalScrollBar(self.scroll_bar2)
        self.file_List.setStyleSheet("background-color: rgb(255, 255, 255); border-radius: 10px; border: 1px solid #D2D6E0; color: #13333F;")
        self.file_List.setObjectName("file_List")
        self.file_List.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.file_List.customContextMenuRequested[QtCore.QPoint].connect(self.contextMenuEvent)

        self.VMs_label = QtWidgets.QLabel(self.ABS_Packager)
        self.VMs_label.setGeometry(QtCore.QRect(40, 30, 181, 16))
        self.VMs_label.setStyleSheet("color:black;")
        font.setPointSize(12)
        self.VMs_label.setFont(font)
        self.VMs_label.setObjectName("VMs_label")
        self.files_label = QtWidgets.QLabel(self.ABS_Packager)
        self.files_label.setStyleSheet("color:black;")
        self.files_label.setGeometry(QtCore.QRect(410, 30, 181, 16))
        self.files_label.setFont(font)
        self.files_label.setObjectName("files_label")

        ################################### TOP BAR ###########################################
        self.menubar = QtWidgets.QMenuBar(self.ABS_Packager)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 679, 21))
        self.menubar.setObjectName("menubar")
        self.menuProject = QtWidgets.QMenu(self.menubar)
        self.menuProject.setObjectName("menuProject")
        self.menuHelp = QtWidgets.QMenu(self.menubar)
        self.menuHelp.setObjectName("menuHelp")
        self.ABS_Packager.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(self.ABS_Packager)
        self.statusbar.setObjectName("statusbar")
        self.ABS_Packager.setStatusBar(self.statusbar)

        ################################## TOP BAR EXPORT ######################################
        self.actionExport = QtWidgets.QAction(self.ABS_Packager)
        self.actionExport.setObjectName("actionExport")
        self.actionExport.triggered.connect(self.export)

        self.actionImport = QtWidgets.QAction(self.ABS_Packager)
        self.actionImport.setObjectName("actionImport")
        self.actionREADME = QtWidgets.QAction(self.ABS_Packager)
        self.actionREADME.setObjectName("actionREADME")
        
        self.menuProject.addAction(self.actionExport)
        self.menuProject.addSeparator()
        self.menuProject.addAction(self.actionImport)
        self.menuHelp.addAction(self.actionREADME)
        self.menubar.addAction(self.menuProject.menuAction())
        self.menubar.addAction(self.menuHelp.menuAction())

        
    ################# Entry Actions ##################################
        # When Packager is executed, first gather VMs in host and show their names.
        self.populate_machineList()
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self.ABS_Packager)

    ############ Creating Objects to execute Export Project Window #####
        self.Form = QtWidgets.QDialog()
        self.sP = ExportProject(self.Form,self.ABS_Packager)

    ############ Create Object to execute Import Project Window
        self.ImportWindow = QtWidgets.QMainWindow()
        self.ui = ImportProject()
        self.ui.setupUi(self.ImportWindow,self.ABS_Packager)
        
    ################# Button Actions #################################
        self.export_button.clicked.connect(self.export)    
        self.select_files_button.clicked.connect(self.select_files)

    ################# Top Menu Actions ###############################

        self.actionExport.triggered.connect(self.export)
        self.actionImport.triggered.connect(self.import_project)

    ##################################################################    
    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.ABS_Packager.setWindowTitle(_translate("ABS_Packager", "ABS_Packager"))
        self.export_button.setText(_translate("ABS_Packager", "Export"))
        self.select_files_button.setText(_translate("ABS_Packager", "Select Files"))
        self.VMs_label.setText(_translate("ABS_Packager", "Virtual Machines:"))
        self.files_label.setText(_translate("ABS_Packager", "Files:"))
        self.menuProject.setTitle(_translate("MainWindow", "Project"))
        self.menuHelp.setTitle(_translate("MainWindow", "Help"))
        self.actionExport.setText(_translate("MainWindow", "Export"))
        self.actionImport.setText(_translate("MainWindow", "Import"))
        self.actionREADME.setText(_translate("MainWindow", "README"))

    ################ Right Click Menu ###################################
    '''For now, right click menu only needs delete selected file functionality'''
    def contextMenuEvent(self):
        rightMenu = QtWidgets.QMenu(self.file_List)
        removeAction = QtWidgets.QAction("Delete")
        row_temp = self.file_List.selectedItems()
        for temp in row_temp:
            row_num = self.file_List.row(temp)
            removeAction.triggered.connect(lambda: self.file_List.takeItem(row_num))
            print("Deleting item in file list")
        rightMenu.addAction(removeAction)
        rightMenu.exec_(QtGui.QCursor.pos())

    ####################### Gather Available VMs from Host###############################
    def populate_machineList(self):
        font = QtGui.QFont()
        font.setPointSize(10)
        vbox = virtualbox.VirtualBox()
        for m in vbox.machines:
            self.machine_name = QtWidgets.QListWidgetItem()
            self.machine_name.setFont(font)
            self.machine_name.setText(m.name)
            self.machine_name.setFlags(self.machine_name.flags() | QtCore.Qt.ItemIsUserCheckable)
            self.machine_name.setCheckState(QtCore.Qt.Unchecked)
            self.machine_list.addItem(self.machine_name)

    ###################### Select and Import Files Functionality ########################
    def select_files(self):
        try:
            file_names = QFileDialog.getOpenFileNames()
            print(file_names)
            print("Selected Files:")
            i = 0
            while i < len(file_names[0]):
                if file_names[0][i] == "All Files (*)":
                   continue 
                self.file_List.addItem(file_names[0][i])
                print(file_names[0][i])
                i = i + 1
        except Exception as e:
            print(e)

    ####################### Export Button Functionality #################################
    def export(self):
        self.Form.show()
        checked_vms = []
        '''Add Selected VM's Name to New List'''
        for i in range(self.machine_list.count()):
            if self.machine_list.item(i).checkState() == Qt.Checked:
                checked_vms.append(self.machine_list.item(i).text())
        '''Send File & VM List to ExportProject.py'''        
        self.sP.pass_objects(self.file_List,checked_vms)

    def import_project(self):
        
        self.ImportWindow.show()

######################### PACKAGER WINDOW EXECUTION CALLS ###############################
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ABS_Packager = QtWidgets.QMainWindow()
    ui = Ui_ABS_Packager()
    ui.setupUi(ABS_Packager)
    ABS_Packager.show()
    sys.exit(app.exec_())
