### GRAPHIC USER INTERFACE PER TEST EGTCAM5 ###

#Caricamento classi di .NET Framework
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationFramework

#Inizializzo variabili vuote
$BitChoice = ""
$ChosenBit = ""
$ChosenDll = ""

#Definisco path truenas di origine
$Artifacts = '\\truenas\Artifacts'
$SrvEgtTech = '\\truenas\Artifacts\EGT_SRV\EgtTech'

#Definisco path test cartelle Egt
$TestProgFolder = 'c:\EgtTestProg'
$TestDataFolder = 'c:\EgtTestData'

#Definisco path test sottocartelle EgtCAM5
$TestProgCam5Folder = 'c:\EgtTestProg\EgtCAM5'
$TestDataCam5Folder = 'c:\EgtTestData\EgtCAM5'

#numero di copie da effettuare
$CopyCount = 12
#assegno il range di valori min max della barra di progresso in base al numero delle macchine trovate
$MinMax = 0..$CopyCount | Measure-Object -Minimum -Maximum

function ProgressBarAndPercentageLabel{
    # avanti uno step nella progress bar
    $CopyProgressBar.PerformStep()
    # calcolo il progresso in %
    $ProgressFactor = ($CopyProgressBar.Value/$CopyProgressBar.Maximum)*100
    $RoundedPercentage = [math]::Round($ProgressFactor)
    #scrivo nel label dedicato la percentuale di avanzamento e faccio refresh progressbar
    $PercentageLabel.Text = "$RoundedPercentage %"
    $CopyProgressBar.Refresh()
}

function ClearFolders($TestProgCam5Folder, $TestDataCam5Folder){
    #Pulisco cartella c:\EgtTestProg\EgtCAM5 se già esistente, poi la creo
if (Test-Path -Path $TestProgCam5Folder) {
    "EgtTestProg\EgtCAM5 esiste!"
    Remove-Item -Recurse $TestProgCam5Folder\*.*
} else {
    new-item $TestProgCam5Folder -itemtype directory
}

#Pulisco cartella c:\EgtTestData\EgtCAM5 se già esistente, poi la creo
if (Test-Path -Path $TestDataCam5Folder) {
    "EgtTestData\EgtCAM5 esiste!"
    Remove-Item -Recurse $TestDataCam5Folder\*.*
} else {
    new-item $TestDataCam5Folder -itemtype directory
}
}

#Definisco funzione che installa il CAM5 richiesto, gli passo i parametri per l'architettura desiderata
function InstallRequiredCAM5($BitChoice, $ChosenBit, $ChosenDll){
#Copio la cartella EgtCAM5
ROBOCOPY /E $SrvEgtTech\EgtProg\EgtCAM5\ $TestProgCam5Folder
ProgressBarAndPercentageLabel

#Copio la cartella Dll desiderata
ROBOCOPY /E $SrvEgtTech\EgtProg\$ChosenDll\ $TestProgCam5Folder
ProgressBarAndPercentageLabel

#Copio EgtCAM5.ini custom in C:\EgtTestProg\EgtCAM5\Config
ROBOCOPY /E $Artifacts\EgtProg\EgtCAM5\Config\ $TestDataCam5Folder\Config EgtCAM5.ini
ProgressBarAndPercentageLabel

#Copio DataRoot.ini custom in C:\EgtTestProg\EgtCAM5
ROBOCOPY /E $Artifacts\EgtProg\EgtCAM5\Config\ $TestProgCam5Folder DataRoot.ini
ProgressBarAndPercentageLabel

#Copio la cartella Lualibs
ROBOCOPY /E $SrvEgtTech\EgtProg\LuaLibs\ $TestProgFolder\LuaLibs
ProgressBarAndPercentageLabel

#Copio la cartella Fonts
ROBOCOPY /E $SrvEgtTech\EgtProg\Fonts\ $TestProgFolder\Fonts
ProgressBarAndPercentageLabel

#Copio la cartella Messages
ROBOCOPY /E $SrvEgtTech\EgtData\Messages\ $TestDataFolder\Messages
ProgressBarAndPercentageLabel

#Copio la cartella ToolMakers
ROBOCOPY /E $SrvEgtTech\EgtData\ToolMakers\ $TestDataCam5Folder\ToolMakers
ProgressBarAndPercentageLabel

#Copio cartella 3dPrinting
ROBOCOPY /E $Artifacts\EgtData\3dPrinting\$ChosenBit\ $TestDataCam5Folder\3dPrinting
ProgressBarAndPercentageLabel

#Copio cartella Beam
ROBOCOPY /E $Artifacts\EgtData\Beam\$ChosenBit\ $TestDataCam5Folder\Beam
ProgressBarAndPercentageLabel

#Copio cartella Wall
ROBOCOPY /E $Artifacts\EgtData\Wall\$ChosenBit\ $TestDataCam5Folder\Wall
ProgressBarAndPercentageLabel

#Copio cartella BeamWall
ROBOCOPY /E $Artifacts\EgtData\BeamWall\$ChosenBit\ $TestDataCam5Folder\BeamWall
ProgressBarAndPercentageLabel
}

#Definisco una finestra in cui è possibile aggiungere controlli.
$Cam5Form = New-Object System.Windows.Forms.Form
$Cam5Form.Text = 'EgtCAM5 Tester'
$Cam5Form.Size = New-Object System.Drawing.Size(400,300)
$Cam5Form.StartPosition = 'CenterScreen'

#Definisco una groupbox per la scelta dell'architettura
$ArchGroupBox = New-Object System.Windows.Forms.GroupBox
$ArchGroupBox.Location = New-Object System.Drawing.Size(20,20)
$ArchGroupBox.size = New-Object System.Drawing.Size(340,80)
$ArchGroupBox.text = "Seleziona architettura EgtCAM5 da testare:"
$Cam5Form.Controls.Add($ArchGroupBox)

#Definisco il bottone per selezionare architettura x86
$x86Button = New-Object System.Windows.Forms.RadioButton
$x86Button.Location = New-Object System.Drawing.Point(20,20)
$x86Button.Size = New-Object System.Drawing.Size(100,23)
$x86Button.Text = 'x86'
$x86Button.Checked = $true
$Cam5Form.Controls.Add($x86Button)
$ArchGroupBox.Controls.Add($x86Button)

#Definisco il bottone per selezionare architettura x64
$x64Button = New-Object System.Windows.Forms.RadioButton
$x64Button.Location = New-Object System.Drawing.Point(20,40)
$x64Button.Size = New-Object System.Drawing.Size(100,23)
$x64Button.Text = 'x64'
$Cam5Form.Controls.Add($x64Button)
$ArchGroupBox.Controls.Add($x64Button)

#Definisco un label per mostrare l'avanzamento dell'operazione in corso
$PercentageLabel = New-Object System.Windows.Forms.Label
$PercentageLabel.Location = New-Object System.Drawing.Point(180,120)
$PercentageLabel.Size = New-Object System.Drawing.Size(280,20)
$PercentageLabel.Text = ""
$PercentageLabel.Visible = $True
$Cam5Form.Controls.Add($PercentageLabel)

#Definisco la barra di avanzamento per mostrare il progresso della copia
$CopyProgressBar = New-Object System.Windows.Forms.ProgressBar
$CopyProgressBar.Width = 320
$CopyProgressBar.Height = 20
$CopyProgressBar.Location = New-Object System.Drawing.Point(20,160)
#assegno i valori minimi e massimi della barra progresso in base al range definito precedentemente
$CopyProgressBar.Minimum = $MinMax.Minimum
$CopyProgressBar.Maximum = $MinMax.Maximum
#inizializzo il value della progress bar al minimo
$CopyProgressBar.Value = $MinMax.Minimum
#definisco lo step della progress bar a 1 (1 step=1 cartella copiata)
$CopyProgressBar.Step = 1
$CopyProgressBar.Style = 'Continuous'
$CopyProgressBar.Visible = $True
$Cam5Form.Controls.Add($CopyProgressBar)

#Definisco il bottone per lanciare installazione richiesta di EgtCAM5
$InstallCAM5Button = New-Object System.Windows.Forms.Button
$InstallCAM5Button.Location = New-Object System.Drawing.Point(20,220)
$InstallCAM5Button.Size = New-Object System.Drawing.Size(100,23)
$InstallCAM5Button.Text = 'Installa EgtCAM5'
$InstallCAM5Button.Add_Click({
    if ($x86Button.Checked -eq $true) {
        $BitChoice = 'x86'
        $ChosenBit = '32'
        $ChosenDll = 'Dll32'
    }
    if ($x64Button.Checked -eq $true) {
        $BitChoice = 'x64'
        $ChosenBit = '64'
        $ChosenDll = 'Dll64'
    }
    $UserConfirmation = [System.Windows.MessageBox]::Show("Install EgtCAM5 $BitChoice ?",'Conferma Architettura','YesNo','Info')
    switch ($UserConfirmation){
    'Yes' {
    ClearFolders $TestProgCam5Folder $TestDataCam5Folder
    InstallRequiredCAM5 $BitChoice $ChosenBit $ChosenDll
    [System.Windows.MessageBox]::Show("EgtCAM5 $BitChoice installato!",'Operazione terminata')
    $Cam5Form.Close()
    $Cam5Form.Dispose()
    Powershell.exe -executionpolicy remotesigned -File .\GUI\EgtScriptsGUI.ps1
    }
    'No'{}
    }
})
$Cam5Form.Controls.Add($InstallCAM5Button)

#Definisco il bottone per navigazione al menu precedente
$MenuReturn = New-Object System.Windows.Forms.Button
$MenuReturn.Location = New-Object System.Drawing.Point(260,220)
$MenuReturn.Size = New-Object System.Drawing.Size(100,23)
$MenuReturn.Text = 'Torna alla home'
$MenuReturn.Add_Click({
    $Cam5Form.Close()
    $Cam5Form.Dispose()
    Powershell.exe -executionpolicy remotesigned -File .\GUI\EgtScriptsGUI.ps1
})
$Cam5Form.Controls.Add($MenuReturn)

#Proprietà Topmost su $true per forzare l'apertura della finestra sopra altre finestre e finestre di dialogo aperte.
$Cam5Form.Topmost = $true

#Visualizzo il modulo:
$Cam5Form.ShowDialog()