﻿# api: ps
# title: init screen
# description: writes stats+MOTD into output window on startup
# version: 0.5
# type: init-gui
# depends: wpf
# category: misc
# hidden: 1
# priority: core


#-- some stats
$sigma_ver = ($menu | % { $_.version } | ? { $_ -match "\d" } | % { $_ -replace "-.+$","" -replace "\D","" } | Measure -Sum).Sum -replace "(?<=\d)(?!$)","."

#-- load RTF over temporary FlowDocument
function Load-RTF($fn) {
    $temp = WD FlowDocument
    $tr = New-Object System.Windows.Documents.TextRange -ArgumentList @($temp.ContentStart,$temp.ContentEnd)
    $fs = [System.IO.FileStream]::new($_, "Open")
    $tr.Load($fs, "Rich Text Format");
    $fs.close()
    ForEach ($b in (@()+@($temp.Blocks))) {
        $GUI.output.Blocks.Add($b)
    }
}

#-- only text output
if ($cfg.main.version -lt "0.9") {
    Out-Gui -f Yellow -b "#223388" "ClickyColoury frontend to Multi-Tools  Σ ≈ $sigma_ver"
    Out-Gui -f "#88bb22" (Get-Content data/motd.txt | Out-String)
}
#-- clicky >= 0.9
else {
    # header
    Out-Gui -xaml @"
    <Paragraph xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
       <Paragraph.Background><LinearGradientBrush><GradientStop Color="#112233" Offset="0.5"/><GradientStop Color="#112288" Offset="0.0"/></LinearGradientBrush></Paragraph.Background>
       <Run Foreground="#ccaa44" FontSize="36" FontFamily="Verdana">Clicky 0.9</Run><LineBreak/>
       <Span Foreground="Gray">ui = <Run Foreground="White">1.2.0</Run> / tools Σ ≈ <Bold Foreground="White">$sigma_ver</Bold></Span>
       <!--Figure Width="200" HorizontalAnchor="ContentLeft" >
         <BlockUIContainer>
           <Path StrokeThickness="40" Stroke="#FFEF8900" HorizontalAlignment="Center" Margin="50,20,0,20" Data="M30,10 A1,1 0 0 0 20,70" />
         </BlockUIContainer>
       </Figure-->
    </Paragraph>
"@
    # extra message files
    dir data/MOTD*.txt | % { Out-Gui -f "#88bb22" (Get-Content -Raw $_) }
    dir data/MOTD*.xaml| % { Out-Gui -XAML (Get-Content -Raw $_) }
    dir data/MOTD*.rtf | % { Load-RTF $_ }
}
