<Page
    Foreground="{DynamicResource TextFillColorPrimaryBrush}"
    Loaded="InstallPage_OnLoaded"
    d:DataContext="{d:DesignInstance Type=viewModels:PackageManagerViewModel,
                                     IsDesignTimeCreatable=True}"
    d:DesignHeight="450"
    d:DesignWidth="700"
    ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
    ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
    mc:Ignorable="d"
    x:Class="StabilityMatrix.PackageManagerPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:models="clr-namespace:StabilityMatrix.Core.Models;assembly=StabilityMatrix.Core"
    xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
    xmlns:viewModels="clr-namespace:StabilityMatrix.ViewModels"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Page.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
    </Page.Resources>

    <Grid Margin="16">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel
            Grid.Row="0"
            Orientation="Vertical"
            Visibility="{Binding ProgressBarVisibility, FallbackValue=Visible}">

            <ProgressBar
                IsIndeterminate="{Binding IsIndeterminate, FallbackValue=False}"
                Maximum="100"
                Value="{Binding ProgressValue, FallbackValue=10}"
                Width="500" />
            <TextBlock
                HorizontalAlignment="Center"
                Padding="8"
                Text="{Binding ProgressText, FallbackValue=Installing...}" />
        </StackPanel>

        <StackPanel
            Grid.Row="1"
            HorizontalAlignment="Left"
            Margin="16"
            Orientation="Horizontal">
            <StackPanel Orientation="Vertical">
                <ui:Button
                    Appearance="Success"
                    Command="{Binding ShowInstallWindowCommand}"
                    Foreground="{ui:ThemeResource TextFillColorPrimaryBrush}"
                    Height="40"
                    HorizontalAlignment="Stretch"
                    IsEnabled="{Binding InstallButtonEnabled, FallbackValue=true}"
                    Margin="0,0,0,16"
                    VerticalContentAlignment="Center">
                    <StackPanel Orientation="Horizontal">
                        <ui:SymbolIcon Symbol="Add24" />
                        <Label
                            Content="Add Package"
                            Margin="8,4,0,0"
                            VerticalContentAlignment="Center" />
                    </StackPanel>
                </ui:Button>

                <ListView
                    ItemsSource="{Binding Packages}"
                    SelectedItem="{Binding SelectedPackage, Mode=TwoWay}"
                    Visibility="{Binding InstallButtonVisibility, FallbackValue=Visible}">

                    <ListView.Style>
                        <Style TargetType="ListView">
                            <Setter Property="Background" Value="#191919" />
                        </Style>
                    </ListView.Style>

                    <ListView.Template>
                        <ControlTemplate>
                            <Border
                                BorderBrush="{ui:ThemeResource KeyboardFocusBorderColorBrush}"
                                BorderThickness="1"
                                CornerRadius="5">
                                <ItemsPresenter />
                            </Border>
                        </ControlTemplate>
                    </ListView.Template>

                    <ListView.ItemTemplate>
                        <DataTemplate DataType="{x:Type models:InstalledPackage}">
                            <StackPanel Margin="8" VerticalAlignment="Top">
                                <TextBlock Margin="0,5,0,5" Text="{Binding DisplayName}" />
                                <TextBlock Margin="0,0,0,5" Text="{Binding DisplayVersion}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackPanel>

            <StackPanel Margin="32,0,0,0" Orientation="Vertical">
                <TextBlock
                    FontSize="24"
                    FontWeight="Bold"
                    Text="{Binding SelectedPackage.DisplayName, FallbackValue=Stable Diffusion Web UI}" />
                <TextBlock FontSize="12" Margin="0,5,0,5">
                    <Run Text="{Binding SelectedPackage.PackageName, FallbackValue=stable-diffusion-webui}" />
                    <Run Text="{Binding SelectedPackage.DisplayVersion, FallbackValue=v1.3.37}" />
                </TextBlock>
                <TextBlock
                    FontSize="12"
                    Margin="0,5,0,5" 
                    Text="{Binding SelectedPackage.FullPath, FallbackValue=Packages\stable-diffusion}" />
                    <StackPanel Margin="0,16,0,0" Orientation="Horizontal">
                    <ui:Button
                        Appearance="Success"
                        Command="{Binding InstallCommand}"
                        Content="{Binding InstallButtonText, FallbackValue=Install}"
                        Height="50"
                        IsEnabled="{Binding InstallButtonEnabled, FallbackValue=true}"
                        VerticalAlignment="Top"
                        Visibility="{Binding InstallButtonVisibility, FallbackValue=Visible}"
                        Width="100" />
                    <ui:Button
                        Appearance="Danger"
                        Command="{Binding UninstallCommand}"
                        Content="Uninstall"
                        Height="50"
                        Margin="16,0,0,0"
                        VerticalAlignment="Top"
                        Visibility="{Binding InstallButtonVisibility, FallbackValue=Visible}"
                        Width="100" />
                </StackPanel>
                <StackPanel Orientation="Horizontal" Visibility="{Binding IsUninstalling,
                                                        Converter={StaticResource BoolToVisConverter}}">
                    <ui:ProgressRing Height="24" Width="24" Margin="8, 16, 8, 8"
                                     VerticalAlignment="Center"
                                     IsIndeterminate="True" HorizontalAlignment="Left"/>
                    <TextBlock Text="Uninstalling..." VerticalAlignment="Center" Margin="0,8,0,0"/>
                </StackPanel>
            </StackPanel>
        </StackPanel>
    </Grid>
</Page>
