<Page
    Foreground="{DynamicResource TextFillColorPrimaryBrush}"
    Loaded="LaunchPage_OnLoaded"
    Title="LaunchPage"
    d:DataContext="{d:DesignInstance Type=designViewModels:MockLaunchViewModel,
                                     IsDesignTimeCreatable=True}"
    d:DesignHeight="450"
    d:DesignWidth="700"
    mc:Ignorable="d"
    ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
    ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
    x:Class="StabilityMatrix.LaunchPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:designViewModels="clr-namespace:StabilityMatrix.DesignData"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:models="clr-namespace:StabilityMatrix.Core.Models;assembly=StabilityMatrix.Core"
    xmlns:terminal="clr-namespace:Simple.Wpf.Terminal;assembly=Simple.Wpf.Terminal"
    xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Page.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />

        <!--  Manually copied style since can't inherit: https://github.com/lepoco/wpfui/blob/main/src/Wpf.Ui/Styles/Controls/ComboBox.xaml  -->
        <ControlTemplate TargetType="{x:Type ComboBoxItem}" x:Key="SelectPackageDropDownStyle">
            <Grid>
                <Border
                    CornerRadius="{TemplateBinding Border.CornerRadius}"
                    Margin="{TemplateBinding Margin}"
                    Name="ContentBorder"
                    Padding="0"
                    SnapsToDevicePixels="True"
                    VerticalAlignment="Stretch">
                    <Grid>
                        <Rectangle
                            Height="16"
                            HorizontalAlignment="Left"
                            Margin="0"
                            RadiusX="2"
                            RadiusY="2"
                            VerticalAlignment="Center"
                            Visibility="Collapsed"
                            Width="3"
                            x:Name="ActiveRectangle">
                            <Rectangle.Fill>
                                <SolidColorBrush Color="{DynamicResource SystemAccentColorSecondary}" />
                            </Rectangle.Fill>
                        </Rectangle>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <!--  Setup grid rows  -->
                            <StackPanel
                                Grid.Column="0"
                                Margin="10,0,0,0"
                                VerticalAlignment="Top"
                                d:DataContext="{d:DesignInstance Type=models:InstalledPackage,
                                                                 IsDesignTimeCreatable=True}">
                                <TextBlock Margin="0,5,0,5" Text="{Binding DisplayName, Mode=OneWay}" />
                                <TextBlock Margin="0,0,0,5" Text="{Binding FullPath, Mode=OneWay}" />
                            </StackPanel>
                        </Grid>
                    </Grid>
                </Border>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter Property="Background" TargetName="ContentBorder">
                        <Setter.Value>
                            <SolidColorBrush Color="{DynamicResource ControlFillColorSecondary}" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Visibility" TargetName="ActiveRectangle" Value="Visible" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground">
                        <Setter.Value>
                            <SolidColorBrush Color="{DynamicResource TextFillColorTertiary}" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Page.Resources>

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

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.8*" />
                    <ColumnDefinition Width="0.2*" />
                </Grid.ColumnDefinitions>
                <ui:Flyout
                    Background="{DynamicResource SystemAccentColorPrimaryBrush}"
                    FontSize="18"
                    Grid.Column="0"
                    Grid.Row="0"
                    IsOpen="{Binding IsLaunchTeachingTipsOpen, Mode=TwoWay}"
                    Margin="24,8,0,0"
                    Placement="Bottom">
                    <StackPanel Orientation="Horizontal">
                        <Grid HorizontalAlignment="Left">
                            <ui:SymbolIcon Symbol="ArrowCurveUpLeft20" />
                        </Grid>
                        <TextBlock
                            HorizontalAlignment="Left"
                            Text="Click Launch to get started!"
                            TextWrapping="WrapWithOverflow"
                            Width="280" />
                    </StackPanel>

                </ui:Flyout>
                <ui:Button
                    Appearance="Success"
                    Command="{Binding LaunchCommand}"
                    Content="Launch"
                    Grid.Column="0"
                    Grid.Row="0"
                    HorizontalAlignment="Left"
                    Margin="24,8,0,0"
                    VerticalAlignment="Stretch"
                    Visibility="{Binding LaunchButtonVisibility, FallbackValue=Visible}"
                    Width="72"
                    x:Name="LaunchButton" />
                <ui:Button
                    Appearance="Danger"
                    Command="{Binding StopCommand}"
                    Content="Stop"
                    Grid.Column="0"
                    Grid.Row="0"
                    HorizontalAlignment="Left"
                    Margin="24,8,0,0"
                    VerticalAlignment="Stretch"
                    Visibility="{Binding StopButtonVisibility, FallbackValue=Hidden}"
                    Width="72"
                    x:Name="StopButton" />
                <ui:Button
                    Command="{Binding ConfigCommand}"
                    FontSize="16"
                    Grid.Column="1"
                    Grid.Row="0"
                    HorizontalAlignment="Left"
                    Margin="8,8,0,0"
                    VerticalAlignment="Stretch"
                    Width="48"
                    x:Name="ConfigButton">
                    <ui:Button.Icon>
                        <ui:SymbolIcon Symbol="Settings32" />
                    </ui:Button.Icon>
                </ui:Button>
            </Grid>
            <ComboBox
                Grid.Column="1"
                Grid.Row="0"
                HorizontalAlignment="Stretch"
                ItemsSource="{Binding InstalledPackages}"
                Margin="8,8,24,0"
                SelectedValue="{Binding SelectedPackage}"
                VerticalAlignment="Top"
                x:Name="SelectPackageComboBox">
                <ComboBox.ItemContainerStyle>
                    <Style TargetType="ComboBoxItem">
                        <Setter Property="Template" Value="{StaticResource SelectPackageDropDownStyle}" />
                    </Style>
                </ComboBox.ItemContainerStyle>

                <ComboBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type models:InstalledPackage}">
                        <StackPanel Margin="10,0,0,0" VerticalAlignment="Top">
                            <TextBlock
                                Margin="0,5,0,5"
                                Name="NameTextBlock"
                                Text="{Binding DisplayName, Mode=OneWay}" />
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </Grid>
        <terminal:Terminal Grid.Row="1"
                           Margin="24,8,26,10"
                           Background="{DynamicResource ControlFillColorDisabledBrush}"
                           IsReadOnly="True"
                           AutoScroll="True"
                           FontFamily="Consolas"
                           ItemHeight="22"
                           Foreground="White"
                           BorderThickness="0"
                           VerticalScrollBarVisibility="Auto"
                           ItemsSource="{Binding ConsoleHistory, Mode=OneWay}"/>
        
        <ui:Button
            Appearance="Success"
            Command="{Binding LaunchWebUiCommand}"
            Content="Open Web UI"
            FontSize="12"
            Grid.ColumnSpan="2"
            Grid.Row="2"
            HorizontalAlignment="Stretch"
            Margin="24,0,24,8"
            Visibility="{Binding ShowWebUiButton, Converter={StaticResource BoolToVisConverter}}" />
    </Grid>
</Page>
