<ui:FluentWindow
    ExtendsContentIntoTitleBar="True"
    Height="700"
    Icon="pack://application:,,,/Assets/Icon.ico"
    Loaded="InstallPage_OnLoaded"
    Title="Stability Matrix - Installer"
    Width="1100"
    WindowBackdropType="Mica"
    WindowStartupLocation="CenterOwner"
    d:DataContext="{d:DesignInstance Type=viewModels:InstallerViewModel,
                                     IsDesignTimeCreatable=True}"
    d:DesignHeight="700"
    d:DesignWidth="800"
    mc:Ignorable="d"
    ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
    ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
    x:Class="StabilityMatrix.InstallerWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:controls="clr-namespace:StabilityMatrix.Controls"
    xmlns:converters="clr-namespace:StabilityMatrix.Converters"
    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:octokit="clr-namespace:Octokit;assembly=Octokit"
    xmlns:system="clr-namespace:System;assembly=System.Runtime"
    xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
    xmlns:viewModels="clr-namespace:StabilityMatrix.ViewModels"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xaml="clr-namespace:MdXaml;assembly=MdXaml"
    xmlns:packages="clr-namespace:StabilityMatrix.Core.Models.Packages;assembly=StabilityMatrix.Core">

    <ui:FluentWindow.Resources>
        <converters:ValueConverterGroup x:Key="InvertAndVisibilitate">
            <converters:BoolNegationConverter />
            <BooleanToVisibilityConverter />
        </converters:ValueConverterGroup>

        <converters:BoolNegationConverter x:Key="BoolNegationConverter" />
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
        <converters:UriToBitmapConverter x:Key="UriToBitmapConverter" />

        <xaml:Markdown
            AssetPathRoot="{x:Static system:Environment.CurrentDirectory}"
            DocumentStyle="{StaticResource DocumentStyle}"
            Heading1Style="{StaticResource H1Style}"
            Heading2Style="{StaticResource H2Style}"
            Heading3Style="{StaticResource H3Style}"
            Heading4Style="{StaticResource H4Style}"
            ImageStyle="{StaticResource ImageStyle}"
            LinkStyle="{StaticResource LinkStyle}"
            SeparatorStyle="{StaticResource SeparatorStyle}"
            x:Key="Markdown" />
        <xaml:TextToFlowDocumentConverter Markdown="{StaticResource Markdown}" x:Key="TextToFlowDocumentConverter" />
    </ui:FluentWindow.Resources>

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

        <ui:TitleBar Background="{ui:ThemeResource ApplicationBackgroundBrush}">
            <ui:TitleBar.Header>
                <TextBlock Margin="16,8" Text="Stability Matrix" />
            </ui:TitleBar.Header>
        </ui:TitleBar>

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

            <TextBlock
                HorizontalAlignment="Center"
                Padding="8"
                Text="{Binding ProgressText, FallbackValue=Installing...}" />
            <ProgressBar
                IsIndeterminate="{Binding IsIndeterminate, FallbackValue=False}"
                Maximum="100"
                Width="500"
                controls:ProgressBarSmoother.SmoothValue="{Binding ProgressValue, FallbackValue=20}" />
            <TextBlock
                FontSize="10"
                HorizontalAlignment="Center"
                Padding="4"
                Text="{Binding SecondaryProgressText, FallbackValue=Installing...}"
                TextWrapping="Wrap" />
        </StackPanel>

        <Grid Grid.Row="2" HorizontalAlignment="Left">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <ListView
                ItemsSource="{Binding AvailablePackages}"
                Margin="16"
                SelectedItem="{Binding SelectedPackage, Mode=TwoWay}">

                <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 packages:BasePackage}">
                        <StackPanel Margin="8" VerticalAlignment="Top">
                            <TextBlock Margin="0,5,0,5" Text="{Binding DisplayName}" />
                            <TextBlock Margin="0,0,0,5" Text="{Binding ByAuthor}" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

            <StackPanel
                Grid.Column="1"
                Margin="16,16,0,16"
                Orientation="Vertical">
                <TextBlock
                    FontSize="24"
                    FontWeight="Bold"
                    Text="{Binding SelectedPackage.DisplayName, FallbackValue=Stable Diffusion Web UI}" />
                <TextBlock FontSize="12" Text="{Binding SelectedPackage.ByAuthor, FallbackValue=by Automatic111}" />

                <ui:Button
                    Appearance="Transparent"
                    BorderBrush="Transparent"
                    Command="{Binding ShowPreviewCommand}"
                    Content="UI Preview"
                    Margin="0,8,0,0">
                    <ui:Button.Style>
                        <Style BasedOn="{StaticResource {x:Type ui:Button}}" TargetType="{x:Type ui:Button}">
                            <Setter Property="Foreground">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource SystemAccentColorSecondary}" />
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ui:Button.Style>
                </ui:Button>

                <ui:Hyperlink Margin="0,0,0,8" NavigateUri="{Binding SelectedPackage.GithubUrl}">
                    <TextBlock TextWrapping="Wrap">
                        <Run Text="GitHub Page:" />
                        <Run Text="{Binding SelectedPackage.GithubUrl, Mode=OneWay}" TextDecorations="Underline" />
                    </TextBlock>
                </ui:Hyperlink>

                <StackPanel Orientation="Horizontal">
                    <ToggleButton
                        Content="Releases"
                        IsChecked="{Binding IsReleaseMode}"
                        IsEnabled="{Binding IsReleaseModeEnabled, FallbackValue=true}" />
                    <ToggleButton
                        Content="Branches"
                        IsChecked="{Binding IsReleaseMode, Converter={StaticResource BoolNegationConverter}}"
                        Margin="8,0,0,0" />
                </StackPanel>

                <StackPanel Margin="0,16,0,0" Orientation="Horizontal">
                    <StackPanel Orientation="Vertical">
                        <Label Content="{Binding ReleaseLabelText, FallbackValue=Version}" />
                        <ComboBox
                            ItemsSource="{Binding AvailableVersions}"
                            MinWidth="200"
                            SelectedItem="{Binding SelectedVersion}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate DataType="{x:Type models:PackageVersion}">
                                    <StackPanel Margin="8,0,0,0" VerticalAlignment="Top">
                                        <TextBlock
                                            Margin="0,4,0,4"
                                            Name="NameTextBlock"
                                            Text="{Binding TagName}" />
                                    </StackPanel>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </StackPanel>

                    <StackPanel
                        Margin="8,0,0,0"
                        Orientation="Vertical"
                        Visibility="{Binding IsReleaseMode, Converter={StaticResource InvertAndVisibilitate}}">
                        <Label Content="Commit" />
                        <ComboBox
                            ItemsSource="{Binding AvailableCommits}"
                            MinWidth="100"
                            SelectedItem="{Binding SelectedCommit}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate DataType="{x:Type octokit:GitHubCommit}">
                                    <StackPanel Margin="8,0,0,0" VerticalAlignment="Top">
                                        <TextBlock
                                            Margin="0,4,0,4"
                                            Name="NameTextBlock"
                                            Text="{Binding Sha}" />
                                    </StackPanel>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </StackPanel>
                </StackPanel>

                <Label Content="Display Name" Margin="0,16,0,0" />
                <StackPanel Orientation="Horizontal" Visibility="{Binding ShowDuplicateWarning, Converter={StaticResource BoolToVisConverter}}">
                    <ui:SymbolIcon
                        Foreground="{ui:ThemeResource SystemFillColorCriticalBrush}"
                        Margin="8"
                        Symbol="ErrorCircle24" />
                    <TextBlock
                        Foreground="{ui:ThemeResource SystemFillColorCriticalBrush}"
                        Margin="0,8,8,8"
                        TextAlignment="Left"
                        TextWrapping="Wrap">
                        <Run Text="An installation with this name already exists." />
                        <LineBreak />
                        <Run Text="Please choose a different name or select a different Install Location." />
                    </TextBlock>
                </StackPanel>
                <ui:TextBox Margin="0,0,0,8" Text="{Binding InstallName, UpdateSourceTrigger=PropertyChanged}" />

                <StackPanel Orientation="Horizontal">
                    <ui:Button
                        Appearance="Primary"
                        Command="{Binding InstallCommand}"
                        Content="{Binding InstallButtonText, FallbackValue=Install}"
                        Height="50"
                        IsEnabled="{Binding ShowDuplicateWarning, Converter={StaticResource BoolNegationConverter}}"
                        Margin="0,16,0,0"
                        VerticalAlignment="Top"
                        Width="100" />
                    <ui:ProgressRing
                        Height="25"
                        IsIndeterminate="True"
                        Margin="8,16,0,0"
                        VerticalAlignment="Center"
                        Visibility="{Binding ProgressBarVisibility}"
                        Width="25" />
                    <TextBlock
                        Margin="8,16,0,0"
                        Text="Installing..."
                        VerticalAlignment="Center"
                        Visibility="{Binding ProgressBarVisibility}" />
                </StackPanel>
            </StackPanel>

            <FlowDocumentScrollViewer
                Document="{Binding ReleaseNotes, Converter={StaticResource TextToFlowDocumentConverter}}"
                Grid.Column="2"
                HorizontalAlignment="Stretch"
                Margin="16"
                VerticalAlignment="Stretch" />
            <ContentPresenter
                Grid.Column="0"
                Grid.ColumnSpan="3"
                x:Name="InstallerContentDialog" />
        </Grid>
    </Grid>
</ui:FluentWindow>
