
#0=ScanAktiv; 
$Script:switch=@($false,$false)
function Load_scanner {
    param ($root)

    # Lädt das Fenster
    [xml]$Form = Get-Content "$PSScriptRoot\scanner.xaml" -Encoding utf8
    $NR=(New-Object System.Xml.XmlNodeReader $Form)
    $window=[Windows.Markup.XamlReader]::Load($NR)

    # Objekt - Variablen Verknüpfung
    $pb=$window.findName("pb")
    $Logo=$window.findName("Logo")
    $Laufwerk=$window.findName("Laufwerk")
    $Script:bar = $window.FindName("pbbar")
    $Script:offline = $window.findName("Offline")
    $Script:gesamt = $window.FindName("tbgesamt")
    $Script:belegt = $window.FindName("tbbelegt")
    $Script:scantxt = $window.findName("Scantxt")
    $Script:pbloading = $window.findName("pbLoading")
    $Script:scannerscan = $window.findName("btnscan")
    $Script:scanlb = $window.findName("lbanalyse")

    # Verknüpft feste Elemente
    $window.Title = $Script:Version
    $window.icon="$root/Source/Logo.png"
    $Logo.Source="$root/Source/Logowhite.png"
    $Laufwerk.Source="$root/Source/HDD1.png"

    ### GIF Sanduhr
    $file = (get-item "$root/Source/hourglas.gif")
    $img = [System.Drawing.Image]::Fromfile($file);
    $pb.Image = $img

    return $window
}
 
function Set_Defaults_Scanner {
    param ($array)
    if (!$array[5]) {
        $Script:bar.Value=$array[4]
        $Script:gesamt.Text=$Array[2]
        $script:belegt.Text=$array[3]
    }else{
        $Script:offline.Visibility="Visible"
    }
}

function Start_Scan {
    param ()
    if (!$Script:switch[0]) {
        Log "### Start Scan ###" -clear
        #### Variablen Deklaration Script ####
        #0=Ordner;1=Datei
        $Script:counter=@(0,0)
        $Script:Scanprocess = 0
        renew_DB
        $Script:scanlb.Content = "Die Analyse des H-Laufwerks läuft..."
        $Script:switch[0]=$true
        $Script:bar.Background="#ff6400" 
        $Script:bar.Foreground="#008000"
        $Script:scantxt.Visibility="Visible"
        $Script:pbloading.Visibility="Visible"
        $Script:scannerscan.Content="Abbrechen"
        $Script:scannerscan.background="$Script:Delcolor"
        start_create_database
        Add_Root_Folder_Size
        Get_TreeView
        Browser_Path -path 0
        $maintree.Items[0].IsExpanded = $true
        $maintree.Items[0].IsSelected = $true
        if ($Script:switch[0]) {
            $Script:switch[0]=$false
            $Script:bar.Background="#008000" 
            $Script:bar.Foreground="#ff6400"
            $Script:scantxt.Visibility="Hidden"
            $Script:pbloading.Visibility="Hidden"
            $Script:scannerscan.Content="Scannen"
            $Script:scanlb.Content = "Starte jetzt die Analyse deines H-Laufwerks"
            $Script:scannerscan.background="$Script:Standardbtncolor"
            $Script:scanner.hide()
            Log "### Scan fertig ###" -clear
            $Script:main.ShowDialog() | Out-Null
        }
    }else {
        $Script:switch[0]=$false
        $Script:bar.Background="#008000" 
        $Script:bar.Foreground="#ff6400"
        $Script:scantxt.Visibility="Hidden"
        $Script:pbloading.Visibility="Hidden"
        $Script:scannerscan.Content="Scannen"
        $Script:scanlb.Content = "Die Analyse des H-Laufwerks läuft..."
        $Script:scannerscan.background="$Script:Standardbtncolor"

    }
}

function renew_DB {
    Log "### Datenbank zurückgesetzt ###" -clear
    $Script:folderdb = @{
        Legende = @("Name","Größe","Pfad","Checked","Deleted","Vorgänger","Inhalt")
    }
    $Script:filedb = @{
        Legende = @("Name","Größe","Pfad","Checked","Deleted","Endung")
    }
}

function Scan_Process {
    param ($process)
    $Value= (($Script:Scanprocess + $process)/$Script:used)*100
    $Script:bar.Value = $Value
    Log "Scan prozess: $Value" -clear
}

function start_create_database {
    param ()
    $collection = Get-ChildItem -path H:\ -Directory
    $Script:folderdb[$Script:counter[0]] = @("H-Laufwerk",1,"H:",$false,$false)
    create_file_database "H:\"
    foreach ($item in $collection){
        $Script:switch[1]=$false
        foreach ($itemex in $Script:exclude){
            if ($item.FullName -eq $itemex) {
                $Script:switch[1]=$true
            }
            [System.Windows.Forms.Application]::DoEvents()
        }
        if (!$Script:switch[1] -and $Script:switch[0]) {
            $Script:counter[0]++
            $Script:folderdb[$Script:counter[0]] = @($item.Name,$item.length,$item.FullName,$false,$false)
            $Script:folderdb[0] += "f"+$Script:counter[0]
            create_file_database $item.FullName
            create_folder_database $item.FullName $Script:counter[0]
            [System.Windows.Forms.Application]::DoEvents()
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    for ($i = 0; $i -lt $Script:folderdb.Count; $i++) {
        $text = "$i " + $Script:folderdb.$i
        Log $text
        [System.Windows.Forms.Application]::DoEvents()
    }
    for ($i = 0; $i -lt $Script:filedb.Count; $i++) {
        $text = "$i " + $Script:filedb.$i
        Log $text
        [System.Windows.Forms.Application]::DoEvents()
    }
}

function create_folder_database {
    param ($folder,$davor)
    if ($Script:switch[0]) {
        $collection = Get-ChildItem -path $folder -Directory
        foreach ($item in $collection){
            $Script:counter[0]++
            $Script:folderdb[$Script:counter[0]] = @($item.Name,$item.length,$item.FullName,$false,$false)
            $Script:folderdb[$davor] += "f"+$Script:counter[0]
            create_file_database $item.FullName
            create_folder_database $item.FullName $Script:counter[0]
            [System.Windows.Forms.Application]::DoEvents()
        }
    }
}

function create_file_database {
    param ($folder)
    $collection = Get-ChildItem -path $folder -File
    foreach ($item in $collection) {
        $Script:filedb[$Script:counter[1]] = @($item.Name,$item.length,$item.FullName,$false,$false,(get_icon $item.Name))
        $Script:folderdb[$Script:counter[0]] += $Script:counter[1]
        $Script:counter[1]++
        [System.Windows.Forms.Application]::DoEvents()
    }
}

function Add_Root_Folder_Size {
    param ()
    $size=0
    for ($i = 5; $i -lt $Script:folderdb[0].count; $i++) {
        if ($Script:folderdb[0][$i] -match "f") {
            $size=$size+(Add_Folder_Size $Script:folderdb[0][$i])
        }else {
            $size=$size+$Script:filedb[$Script:folderdb[0][$i]][1]
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    $Script:folderdb[0][1]=$Size
}

function Add_Folder_Size {
    param ($Folder)
    $string=$Folder.Substring(1)
    $Folder = [int]$string
    $size=0
    for ($i = 5; $i -lt $Script:folderdb[$Folder].count; $i++) {
        if ($Script:folderdb[$Folder][$i] -match "f") {
            $size=$size+(Add_Folder_Size $Script:folderdb[$Folder][$i])
        }else {
            $size=$size+$Script:filedb[$Script:folderdb[$Folder][$i]][1]
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    $Script:folderdb[$Folder][1]=$size
    return $size
}

function get_icon {
    param ($data)
    $data = $data -replace '\s',''
    $found = $true
    for ($c = -1; $c -gt -10; $c--) {
        $char = $data[$c]
        if ($char -eq ".") {
            $trim=$data.Length+$c
            $trimdata=$data.Remove(0,$trim+1)
            $c=-10 
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    for ($i = 0; $i -lt $script:icontypes.Count; $i++) {
        if ($trimdata -eq $script:icontypes[$i]) {
            $return = $i
            $i = $script:icontypes.Count
            $found = $false
        }
        [System.Windows.Forms.Application]::DoEvents()
    }
    if ($found) {
        $return = 0
    }
    return $return
}

Log "### Scanner geladen ###" -clear