### GRAPHIC USER INTERFACE PER MACHINE GRABBER ###

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

#inizializzo variabili vuote
$SelDir=""
$SelMach=""
$TestDirectory="C:\EgtTestData\EgtCAM5\Machines"
$StableDirectory="C:\ProgramData\EgalTech\EgtCAM5\Machines"

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

#funzione MachineCopy: date le macchine da copiare e la folder di destinazione, fa la copia da truenas al pc locale e mostra l'avanzamento nella progress bar
function MachineCopy($SelMach, $SelDir){
    #inizializzo counter a zero per avanzamento barra progresso
    $Counter = 0
    #Cerco le cartelle delle macchine richieste
    $MachinesList = Get-ChildItem -Path $Artifacts\EGT_SRV\EgtTech\EgtData\Machines\ -Directory | Select-String -Pattern $SelMach
    #ricavo il numero delle macchine richieste trovate
    $MachinesCount = $MachinesList.Count
    #assegno il range di valori min max della barra di progresso in base al numero delle macchine trovate
    $MinMax = 0..$MachinesCount | Measure-Object -Minimum -Maximum
    #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
    #Per ogni macchina richiesta trovata procedo a copiarla in EgtTestData\EgtCAM5\Machines
    foreach($Machine in $MachinesList){
        ROBOCOPY $Artifacts\EGT_SRV\EgtTech\EgtData\Machines\$Machine $SelDir\$Machine /MIR /FFT
        $Counter++
        # 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 una finestra in cui è possibile aggiungere controlli.
$MachGrabForm = New-Object System.Windows.Forms.Form
$MachGrabForm.Text = 'Machine Grabber'
$MachGrabForm.Size = New-Object System.Drawing.Size(500,500)
$MachGrabForm.StartPosition = 'CenterScreen'

#Definisco una groupbox per la scelta delle macchine
$MachGroupBox = New-Object System.Windows.Forms.GroupBox
$MachGroupBox.Location = New-Object System.Drawing.Size(20,20)
$MachGroupBox.size = New-Object System.Drawing.Size(440,140)
$MachGroupBox.text = "Seleziona macchine da copiare:"
$MachGrabForm.Controls.Add($MachGroupBox)

#Definisco il bottone per selezionare macchine Belotti
$BelottiButton = New-Object System.Windows.Forms.RadioButton
$BelottiButton.Location = New-Object System.Drawing.Point(20,20)
$BelottiButton.Size = New-Object System.Drawing.Size(100,23)
$BelottiButton.Text = 'Belotti'
$BelottiButton.Checked = $true
$MachGrabForm.Controls.Add($BelottiButton)
$MachGroupBox.Controls.Add($BelottiButton)

#Definisco il bottone per selezionare macchine Cms
$CmsButton = New-Object System.Windows.Forms.RadioButton
$CmsButton.Location = New-Object System.Drawing.Point(20,40)
$CmsButton.Size = New-Object System.Drawing.Size(100,23)
$CmsButton.Text = 'Cms'
$MachGrabForm.Controls.Add($CmsButton)
$MachGroupBox.Controls.Add($CmsButton)

#Definisco il bottone per selezionare macchine Essetre
$EssetreButton = New-Object System.Windows.Forms.RadioButton
$EssetreButton.Location = New-Object System.Drawing.Point(20,60)
$EssetreButton.Size = New-Object System.Drawing.Size(100,23)
$EssetreButton.Text = 'Essetre'
$MachGrabForm.Controls.Add($EssetreButton)
$MachGroupBox.Controls.Add($EssetreButton)

#Definisco il bottone per selezionare macchine Multiax
$MultiaxButton = New-Object System.Windows.Forms.RadioButton
$MultiaxButton.Location = New-Object System.Drawing.Point(20,80)
$MultiaxButton.Size = New-Object System.Drawing.Size(100,23)
$MultiaxButton.Text = 'Multiax'
$MachGrabForm.Controls.Add($MultiaxButton)
$MachGroupBox.Controls.Add($MultiaxButton)

#Definisco il bottone per selezionare macchine Omag
$OmagButton = New-Object System.Windows.Forms.RadioButton
$OmagButton.Location = New-Object System.Drawing.Point(20,100)
$OmagButton.Size = New-Object System.Drawing.Size(100,23)
$OmagButton.Text = 'Omag'
$MachGrabForm.Controls.Add($OmagButton)
$MachGroupBox.Controls.Add($OmagButton)

#Definisco una groupbox per la scelta della cartella di destinazione
$FolderGroupBox = New-Object System.Windows.Forms.GroupBox
$FolderGroupBox.Location = New-Object System.Drawing.Size(20,180)
$FolderGroupBox.size = New-Object System.Drawing.Size(440,120)
$FolderGroupBox.text = "Seleziona directory di destinazione:"
$MachGrabForm.Controls.Add($FolderGroupBox)

#Definisco il bottone per selezionare folder Test
$TestFolderButton = New-Object System.Windows.Forms.RadioButton
$TestFolderButton.Location = New-Object System.Drawing.Point(20,20)
$TestFolderButton.Size = New-Object System.Drawing.Size(400,23)
$TestFolderButton.Text = "Test $TestDirectory"
$TestFolderButton.Checked = $true
$FolderGroupBox.Controls.Add($TestFolderButton)

#Definisco il bottone per selezionare folder Stable
$StableFolderButton = New-Object System.Windows.Forms.RadioButton
$StableFolderButton.Location = New-Object System.Drawing.Point(20,50)
$StableFolderButton.Size = New-Object System.Drawing.Size(400,23)
$StableFolderButton.Text = "Stable $StableDirectory"
$FolderGroupBox.Controls.Add($StableFolderButton)

#Definisco il bottone per selezionare folder Custom
$CustomFolderButton = New-Object System.Windows.Forms.RadioButton
$CustomFolderButton.Location = New-Object System.Drawing.Point(20,80)
$CustomFolderButton.Size = New-Object System.Drawing.Size(80,23)
$CustomFolderButton.Text = 'Custom'
$CustomFolderButton.Add_Click({
    $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
    $FolderBrowser.SelectedPath = "C:\"
    [void]$FolderBrowser.ShowDialog()
    $FolderBrowser.SelectedPath
    $CustomFolderText.Text = $FolderBrowser.SelectedPath
})
$FolderGroupBox.Controls.Add($CustomFolderButton)

#Definisco il textbox che mostra la folder custom
$CustomFolderText = New-Object System.Windows.Forms.TextBox
$CustomFolderText.Location = New-Object System.Drawing.Point(100,80)
$CustomFolderText.Size = New-Object System.Drawing.Size(280,23)
$CustomFolderText.Text = ''
$CustomFolderText.Add_Click({
    $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
    $FolderBrowser.SelectedPath = "C:\"
    [void]$FolderBrowser.ShowDialog()
    $FolderBrowser.SelectedPath
    $CustomFolderText.Text = $FolderBrowser.SelectedPath
})
$FolderGroupBox.Controls.Add($CustomFolderText)

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

#Definisco la barra di avanzamento per mostrare il progresso della copia
$CopyProgressBar = New-Object System.Windows.Forms.ProgressBar
$CopyProgressBar.Width = 440
$CopyProgressBar.Height = 20
$CopyProgressBar.Location = New-Object System.Drawing.Point(20,360)
$CopyProgressBar.Style = 'Continuous'
$CopyProgressBar.Visible = $True
$MachGrabForm.Controls.Add($CopyProgressBar)

#Definisco il bottone per lanciare la copia delle macchine richieste
$GrabMachButton = New-Object System.Windows.Forms.Button
$GrabMachButton.Location = New-Object System.Drawing.Point(20,400)
$GrabMachButton.Size = New-Object System.Drawing.Size(100,23)
$GrabMachButton.Text = 'Copia Macchine'
#Quando l'utente clicca il copia macchine verifico la selezione richiesta e la cartella di destinazione
$GrabMachButton.Add_Click({
    if ($BelottiButton.Checked -eq $true) {
        $SelMach = 'Belotti'
    }
    if ($CmsButton.Checked -eq $true) {
        $SelMach = 'CMS'
    }
    if ($EssetreButton.Checked -eq $true) {
        $SelMach = 'Essetre'
    }
    if ($MultiaxButton.Checked -eq $true) {
        $SelMach = 'Multiax'
    }
    if ($OmagButton.Checked -eq $true) {
        $SelMach = 'Omag'
    }
    if ($TestFolderButton.Checked -eq $true) {
        $SelDir = $TestDirectory
    }
    if ($StableFolderButton.Checked -eq $true) {
        $SelDir = $StableDirectory
    }
    if ($CustomFolderButton.Checked -eq $true) {
        $SelDir = $CustomFolderText.Text
    }
    #chiedo all'utente conferma della copia richiesta
    $UserConfirmation = [System.Windows.MessageBox]::Show("Copia macchine $SelMach in folder $SelDir ?",'Conferma copia','YesNo','Info')
    switch ($UserConfirmation){
    'Yes' {
        #chiamo funzione MachineCopy che fa il robocopy e mostra la barra di avanzamento in base al numero di cartelle da copiare
        MachineCopy $SelMach $SelDir
        [System.Windows.MessageBox]::Show("Macchine $SelMach copiate in $SelDir",'Operazione terminata')
        $CopyProgressBar.Value = $CopyProgressBar.Minimum
        $PercentageLabel.Text = ""
    }
    'No'{}
    }
})
$MachGrabForm.Controls.Add($GrabMachButton)

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

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

#Visualizzo il modulo:
$MachGrabForm.ShowDialog()