﻿# api: multitool
# version: 0.3
# title: Plugin Manager
# description: Install/delete user plugins in %APPDATA%
# type: inline
# depends: menu
# category: config
# hidden: 1
# key: plugins?
# config:
#    {"disabled- name: plugin_online_repo, type: str, value: "http://fossil.include-once.org/repo.json/clickycoloury/contrib/*.ps1", description: online repository of additional plugins }
# img: tools
#
#
# Scan contrib/*.ps1 and allow copying files to %APPDATA%/multitool
# Not overly pretty. Just lists some file details with [Install] or [Delete] button.
#


Param(
    $repodir = ".\contrib\",
    $userdir = "$env:APPDATA\multitool\"
)

# scan plugins
Write-Host "❏ Scanning available plugins.."
$p_filter = { $_.title -and $_.api -match "ps|powershell|clicky|multitool" -and $_.id -ne "config" }
$p_users = dir $userdir\*.ps1 | % { Extract-PluginMeta $_ } | ? $p_filter
$p_avail = dir $repodir\*.ps1 | % { Extract-PluginMeta $_ } | ? $p_filter | ? { @($p_users) -notcontains @($_) }



#-- Decorative entry for plugin
function Out-PluginDesc($e) {
    $e.Values = $e.Values | % { [Security.SecurityElement]::Escape($_) }
    $xaml = @"
<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Padding="5,2" TextWrapping="Wrap">
<TextBlock.ToolTip>
 <ToolTip Background="#eeeeeeee" Width="400"><TextBlock Width="380" TextWrapping="Wrap">
   <Run Background="Black" Foreground="White">$($e.doc)</Run>
   <LineBreak/>
   <Italic>api</Italic>: <Run Foreground="Red">$($e.api)</Run> <LineBreak/>
   <Italic>state</Italic>: <Bold Foreground="Orange">$($e.state)</Bold> <LineBreak/>
   <Italic>version</Italic>: <Bold Foreground="#ff997711">$($e.version)</Bold> <LineBreak/>
   <Italic>category</Italic>: <Run Foreground="DarkBlue">$($e.category)</Run> <LineBreak/>
   <Italic>icon</Italic>: <Run>$($e.icon)</Run> <LineBreak/>
   <Italic>vars</Italic>: <Run Foreground="Green">$($e.vars | % { $_.name })</Run>
 </TextBlock></ToolTip>
</TextBlock.ToolTip>
 <Bold FontSize="14">$($e.title)</Bold>
 - <Bold Background="#ffffddaa">$($e.version)</Bold>
 - <Italic Foreground="#ff777777">$($e.type)</Italic>
 - <Bold Foreground="#ff222277">$($e.category)</Bold>
 <LineBreak/>
 <Run Foreground="#ff111155" Background="#fff0f3ff">$($e.description)</Run>
</TextBlock>
"@
#    Write-Host $xaml
    [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader ([xml]$xaml)))
}

#-- calbacks
function Install-Plugin($fn) {
    copy $fn $userdir
    $this.Content = "✔"
    #$this.visibility = "Hidden"
}
function Uninstall-Plugin($fn) {
    rm $fn
    $this.Content = '✘';
}


#-- Plugin window
function Run-PluginManager {
    Param(
       $p_avail, $p_users
    )
    $is_inst = $p_users | % { $_.id }
    

    #-- create action widgets
    $installable = @($p_avail | ? {$_} | % {
        $label = "Install"; if ($is_inst -contains $_.id) { $label = "Reinstall" }
        W Separator
        W StackPanel @{Width=500; Background="White"; Orientation="Horizontal"; Add=@(
            (W Button @{Content="$label"; Width=65; Height=35; Add_Click = [scriptblock]::create("Install-Plugin '{0}'" -f $_.fn)}),
            (Out-PluginDesc $_)
        )}
        W Separator
    })
    $removable = @($p_users |  ? {$_} | % {
        W Separator
        W StackPanel @{Width=500; Background="White"; Orientation="Horizontal"; Add=@(
            (W Button @{Content="Delete"; Width=65; Height=35; Add_Click = [scriptblock]::create("Uninstall-Plugin '{0}'" -f $_.fn)}),
            (Out-PluginDesc $_)
        )}
        W Separator
    })

    #-- window
    $w = (W (New-Object System.Windows.Window) @{
      Title="Plugin installation/removal";
      Width=520; Height=450; Top=350; Left=600; TopMost=$true;
      Background="#eef7f7ff";
      Content=(
        W ScrollViewer @{
           Padding="10,5"; HorizontalAlignment="Left";
          Content=(
            W StackPanel @{
              Add=@(
                 @(W Label @{Content="✎ Tooltips contain additional script/plugin details.`r`n☛ Close window when done."}) +
                 @(W Label @{Content="Install new plugins/tools"; FontSize=20}) +
                 $installable +
                 @(W Label @{Content="Remove from %APPDATA% dir"; FontSize=20}) +
                 $removable
                 @(W Label @{Content="Manually manage"; FontSize=20}) +
                 @(W Button @{Content="open %APPDATA%\multitool config dir"; Add_Click={II $userdir}; Width=250; HorizontalAlignment="Left"; Margin=5}),
                 @(W Button @{Content="browse contrib/ plugin dir"; Add_Click={II $repodir}; Width=250; HorizontalAlignment="Left"; Margin=5}),
                 @(W Button @{Content="show ClickyColoury online repo"; Add_Click={Start "http://fossil.include-once.org/clickycoloury/wiki/contrib"}; Width=250; HorizontalAlignment="Left"; Margin=5})
              )
            }
          )
        }
      )
    })#=window
    [void]$w.ShowDialog()
}

Write-Host "❏ Show plugin manager.."
Run-PluginManager $p_avail $p_users

Write-Host "☛ Restart for changes to take effect."
