
#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("lbanalyse1")
    $Script:POOP_AIDS_COPY = $window.findName("lbanalyse2")

    # 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 Scan_Label_Swtich {
    param(
        [switch]$scan
    )
    if (!$scan) {
        $Script:scanlb.Visibility="Visible"
        $Script:POOP_AIDS_COPY.Visibility="Hidden"
    }else{
        $Script:scanlb.Visibility="Hidden"
        $Script:POOP_AIDS_COPY.Visibility="Visible"
    }
}
 
function Set_Defaults_Scanner {
    param ($array)
    if (!$array[5]) {
        $Script:H_Size=$array[4]
        $Script:bar.Value=$Script:H_Size
        $Script:gesamt.Text=$Array[2]
        $script:belegt.Text=$array[3]
    }else{
        $Script:offline.Visibility="Visible"
    }
}

function Start_Scan {
    param ()
    if (!$Script:switch[0]) {
        Scan_Label_Swtich -scan
        $Script:switch[0]=$true
        Init_Scan_Process
        #Log "### Start Scan ###" -clear
        #### Variablen Deklaration Script ####
        #0=Ordner;1=Datei
        $Script:counter=@(0,0)
        $Script:Scan_prozess = 0

        $Script:belegt.Visibility="Hidden"
        $Script:bar.Foreground="#008000"
        $Script:scantxt.Visibility="Visible"
        $Script:pbloading.Visibility="Visible"
        $Script:scannerscan.Content="Abbrechen"
        $Script:scannerscan.background="$Script:Delcolor"

        renew_DB
        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.Foreground="#00b8d4"
            $Script:scantxt.Visibility="Hidden"
            $Script:pbloading.Visibility="Hidden"
            $Script:scannerscan.Content="Scannen"
            $Script:belegt.Visibility="Visible"
            $Script:bar.Value=$Script:H_Size
            $Script:Scan_prozess = 0
            Scan_process
            Scan_Label_Swtich
            $Script:scannerscan.background="$Script:Standardbtncolor"
            $Script:scanner.hide()

            #Log "### Scan fertig ###" -clear
            $Script:main.ShowDialog() | Out-Null
        }
    }else {
        $Script:switch[0]=$false
        $Script:bar.Foreground="#00b8d4"
        $Script:scantxt.Visibility="Hidden"
        $Script:pbloading.Visibility="Hidden"
        $Script:scannerscan.Content="Scannen"
        $Script:belegt.Visibility="Visible"
        $Script:bar.Value=$Script:H_Size
        Scan_Label_Swtich
        $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 Init_Scan_Process{
    $Script:Scan_prozess = 0
    $Scan_exclude_percent = 0.85
    $Script:Scan_used_exclude=$Script:used*$Scan_exclude_percent
}

function Scan_Process {
    param ($Item)
    $Scan_Percent=0
    $Script:Scan_Prozess=$Script:Scan_Prozess+$Item
    if ($Script:Scan_Prozess -gt $Script:Scan_used_exclude) {
        $Scan_Percent = ($Script:Scan_Prozess/$Script:used)*100
    }else{
        $Scan_Percent = ($Script:Scan_Prozess/$Script:Scan_used_exclude)*100
    }
    $Script:bar.Value = $Scan_Percent
    #Log "Scan prozess: $Scan_Percent" -clear
    #Log "$Script:used $Script:Scan_Prozess" -clear
}

function start_create_database {
    param ()
    $collection = Get-ChildItem -path G:\ -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()
    }
}

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)
            $text = " " + $Script:counter[0] + " " + $Script:folderdb[$Script:counter[0]]
            #Log $text
            $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))
        Scan_Process $item.length
        $text = " " + $Script:counter[1] + " " + $Script:filedb[$Script:counter[1]]
        #Log $text
        $Script:folderdb[$Script:counter[0]] += $Script:counter[1]
        $Script:counter[1]++
        [System.Windows.Forms.Application]::DoEvents()
    }
}

function Add_Root_Folder_Size {
    $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)
    if ($data -match '.') {
        $trimdata = (($data.Split('.'))[1]).ToLower()
        $index=[array]::indexof($icontypes,$trimdata)
        if ($index -eq -1) {
            $return = -1
        }else{
            $return = $index
        }
        return $return
    }else{
        return $data
    }
}

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