### GRAPHIC USER INTERFACE PER TEST BeamWall ###

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

#inizializzo variabili vuote
$ChosenBit = ""
$ChosenDll = ""
$ChosenDllD = ""
$SelBranch = ""
$SelArch = ""

#Definisco path truenas di origine
$Artifacts = '\\truenas\Artifacts'

#Definisco path test sottocartelle EgtBEAMWALL
$TestProgBWFolder = 'c:\EgtTestProg\EgtBEAMWALL'
$TestDataBWFolder = 'c:\EgtTestData\EgtBEAMWALL'

#numero di copie da effettuare
$CopyCount = 11
#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()
}

#Definisco funzione che pulisce il contenuto delle cartelle prima di copiare i file da Truenas
function ClearFolders($TestProgBWFolder, $TestDataBWFolder){
    #Pulisco cartella EgtTestProg\EgtBEAMWALL se già esistente, poi la creo
if (Test-Path -Path $TestProgBWFolder) {
    "Prog esiste!"
    Remove-Item -Recurse $TestProgBWFolder\*.*
} else {
    new-item $TestProgBWFolder -itemtype directory
}

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

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

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

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

#Definisco funzione che installa il beamwall richiesto, gli passo i parametri per l'architettura e il branch desiderato
function InstallRequiredBeamWall($ChosenBit, $ChosenDll, $ChosenDllD, $SelBranch, $SelArch){
#Copio la cartella Config
ROBOCOPY /E $Artifacts\EgtProg\EgtBEAMWALL_Config\Config\ $TestDataBWFolder\Config
ProgressBarAndPercentageLabel

#Copio la cartella Messages
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtData\Messages $TestDataBWFolder\Config
ProgressBarAndPercentageLabel

#Copio la cartella Fonts
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtProg\Fonts\ $TestDataBWFolder\Fonts
ProgressBarAndPercentageLabel

#Copio DataRoot.ini custom in c:\EgtTestProg\EgtBEAMWALL
ROBOCOPY /E $Artifacts\EgtProg\EgtBEAMWALL_Config\ $TestProgBWFolder DataRoot.ini
ProgressBarAndPercentageLabel

#Copio il file Medium.ini in \Warehouse 
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtData\EgtBEAMWALL\Warehouse\ $TestDataBWFolder\Warehouse Medium.ini
ProgressBarAndPercentageLabel

#Copio Resources
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtData\EgtBEAMWALL\Resources\ $TestDataBWFolder\Resources
ProgressBarAndPercentageLabel

#Copio la sottocartella 32 o 64 bit di Lualibs a seconda della richiesta
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtProg\LuaLibs\$ChosenBit $TestDataBWFolder\Lualibs
ProgressBarAndPercentageLabel

#Copio la cartella Dll32 o Dll64 a seconda della richiesta
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtProg\$ChosenDll $TestProgBWFolder
ProgressBarAndPercentageLabel

#Copio la cartella DllD32 o DllD64 a seconda della richiesta
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtProg\$ChosenDllD $TestProgBWFolder
ProgressBarAndPercentageLabel

#Copio Versione + Architettura di BEAMWALL richieste
ROBOCOPY /E $Artifacts\EgtProg\EgtBEAMWALL\$SelBranch\$SelArch $TestProgBWFolder
ProgressBarAndPercentageLabel

#Cancello tutti i .exe.config e .dll.config perchè problematici in area Test
Remove-Item -Recurse $TestProgBWFolder\*.exe.config
Remove-Item -Recurse $TestProgBWFolder\*.dll.config
ProgressBarAndPercentageLabel
}

#Definisco una finestra in cui è possibile aggiungere controlli.
$BWForm = New-Object System.Windows.Forms.Form
$BWForm.Text = 'BeamWall Tester'
$BWForm.Size = New-Object System.Drawing.Size(400,420)
$BWForm.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 BeamWall da testare:"
$BWForm.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
$BWForm.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'
$BWForm.Controls.Add($x64Button)
$ArchGroupBox.Controls.Add($x64Button)

#Definisco una groupbox per la scelta del branch
$BranchGroupBox = New-Object System.Windows.Forms.GroupBox
$BranchGroupBox.Location = New-Object System.Drawing.Size(20,120)
$BranchGroupBox.size = New-Object System.Drawing.Size(340,80)
$BranchGroupBox.text = "Seleziona branch BeamWall da testare:"
$BWForm.Controls.Add($BranchGroupBox)

#Definisco il bottone per selezionare develop
$DevelopButton = New-Object System.Windows.Forms.RadioButton
$DevelopButton.Location = New-Object System.Drawing.Point(20,20)
$DevelopButton.Size = New-Object System.Drawing.Size(100,23)
$DevelopButton.Text = 'Develop'
$DevelopButton.Checked = $true
$BWForm.Controls.Add($DevelopButton)
$BranchGroupBox.Controls.Add($DevelopButton)

#Definisco il bottone per selezionare master
$MasterButton = New-Object System.Windows.Forms.RadioButton
$MasterButton.Location = New-Object System.Drawing.Point(20,40)
$MasterButton.Size = New-Object System.Drawing.Size(100,23)
$MasterButton.Text = 'Master'
$BWForm.Controls.Add($MasterButton)
$BranchGroupBox.Controls.Add($MasterButton)

#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,220)
$PercentageLabel.Size = New-Object System.Drawing.Size(280,20)
$PercentageLabel.Text = ""
$PercentageLabel.Visible = $True
$BWForm.Controls.Add($PercentageLabel)

#Definisco la barra di avanzamento per mostrare il progresso della copia
$CopyProgressBar = New-Object System.Windows.Forms.ProgressBar
$CopyProgressBar.Width = 340
$CopyProgressBar.Height = 20
$CopyProgressBar.Location = New-Object System.Drawing.Point(20,240)
#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
$BWForm.Controls.Add($CopyProgressBar)

#Definisco il bottone per lanciare installazione di BEAMWALL
$InstallBWButton = New-Object System.Windows.Forms.Button
$InstallBWButton.Location = New-Object System.Drawing.Point(20,280)
$InstallBWButton.Size = New-Object System.Drawing.Size(120,23)
$InstallBWButton.Text = 'Installa BeamWall'
$InstallBWButton.Add_Click({
    if ($x86Button.Checked -eq $true) {
            $ChosenBit = '32'
            $ChosenDll = 'Dll32'
            $ChosenDllD = 'DllD32'
            $SelArch = 'x86'
    }
    if ($x64Button.Checked -eq $true) {
            $ChosenBit = '64'
            $ChosenDll = 'Dll64'
            $ChosenDllD = 'DllD64'
            $SelArch = 'x64'
    }
    if ($DevelopButton.Checked -eq $true) {
            $SelBranch = 'develop'
    }
    if ($MasterButton.Checked -eq $true) {
            $SelBranch = 'master'
    }
    $UserConfirmation = [System.Windows.MessageBox]::Show("Install BeamWall $SelArch branch $SelBranch ?",'Conferma Installazione','YesNo','Info')
    switch ($UserConfirmation){
    'Yes' {
    ClearFolders $TestProgBWFolder $TestDataBWFolder
    InstallRequiredBeamWall $ChosenBit $ChosenDll $ChosenDllD $SelBranch $SelArch
    [System.Windows.MessageBox]::Show("Beamwall $SelArch branch $SelBranch installato!",'Operazione terminata')
    $BWForm.Close()
    $BWForm.Dispose()
    Powershell.exe -executionpolicy remotesigned -File .\GUI\EgtScriptsGUI.ps1
    }
    'No' {}
    }
})
$BWForm.Controls.Add($InstallBWButton)

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

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

#Visualizzo il modulo:
$BWForm.ShowDialog()