<winuiex:WindowEx
    xmlns:winuiex="using:WinUIEx"
    x:Class="Clippy.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Clippy"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    IsResizable="False"
    IsShownInSwitchers="False"
    Title="Clippy"
    IsMinimizable="False"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:controls="using:Clippy.Controls"
    xmlns:icons="using:CubeKit.UI.Icons" 
    xmlns:classes="using:Clippy.Core.Classes" 
    xmlns:helpers="using:Clippy.Helpers"
    IsAlwaysOnTop="{x:Bind Clippy.IsPinned, Mode=OneWay}"
    mc:Ignorable="d">
    <Grid x:Name="Background" BorderBrush="{ThemeResource MicaBorderBrush}" BorderThickness="0">
        <Grid.Resources>
            <DataTemplate x:Key="ClippyTemplate" x:DataType="classes:ClippyMessage">
                <controls:ClippyMessage Message="{x:Bind Message, Mode=OneWay}" IsSendEnabled="{x:Bind IsLatest, Mode=OneWay}"/>
            </DataTemplate>

            <DataTemplate x:Key="UserTemplate" x:DataType="classes:UserMessage">
                <controls:UserMessage Message="{x:Bind Message, Mode=OneWay}"/>
            </DataTemplate>

            <DataTemplate x:Key="AnnouncementTemplate" x:DataType="classes:AnnouncementMessage">
                <controls:AnnouncementMessage Message="{x:Bind Message, Mode=OneWay}"/>
            </DataTemplate>

            <helpers:MessageSelector x:Key="MessageTemplateSelector"
                Clippy="{StaticResource ClippyTemplate}"
                Announcement="{StaticResource AnnouncementTemplate}"
                User="{StaticResource UserTemplate}"/>
        </Grid.Resources>
        <Grid Canvas.ZIndex="0" Background="{ThemeResource LayerFillColorDefaultBrush}" Visibility="{x:Bind BtoV(Settings.TranslucentBackground), Mode=OneWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        <Grid x:Name="Content" Canvas.ZIndex="1" Padding="12, 8, 12, 8">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="100"/>
            </Grid.RowDefinitions>
            <ListView Grid.Row="0" Visibility="{x:Bind BtoV(Clippy.IsClippyEnabled), Mode=OneWay}" Margin="0, 0, 0, 8" ItemTemplateSelector="{StaticResource MessageTemplateSelector}" SelectionMode="None" ItemsSource="{x:Bind Clippy.ChatService.Messages, Mode=OneWay}">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <ItemsStackPanel ItemsUpdatingScrollMode="KeepLastItemInView"
                              VerticalAlignment="Bottom"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="Margin" Value="0,8,0,8" />
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.ItemContainerTransitions>
                    <TransitionCollection>
                        <EntranceThemeTransition IsStaggeringEnabled="True" />
                        <AddDeleteThemeTransition />
                        <ContentThemeTransition />
                    </TransitionCollection>
                </ListView.ItemContainerTransitions>
            </ListView>
            <Grid Grid.Row="2" VerticalAlignment="Bottom" HorizontalAlignment="Stretch">
                <ToggleButton IsChecked="{x:Bind Clippy.IsClippyEnabled, Mode=TwoWay}" Style="{ThemeResource ClippyToggleButton}" Padding="0" HorizontalAlignment="Right" Checked="Clippy_Checked" Unchecked="Clippy_Unchecked">
                    <Image Source="ms-appx:///Assets/Clippy/Clippy.png" Height="100" Width="100"/>
                </ToggleButton>
                <StackPanel Visibility="{x:Bind BtoV(Clippy.IsClippyEnabled), Mode=OneWay}" Orientation="Horizontal" Spacing="4" HorizontalAlignment="Left" VerticalAlignment="Bottom">
                    <Button Click="Settings_Click" Foreground="{ThemeResource SystemAccentColorLight2}" Style="{ThemeResource SettingsToolbarButton}"/>
                    <ToggleButton IsChecked="{x:Bind Clippy.IsPinned, Mode=TwoWay}" Style="{ThemeResource PinToggleButton}" Height="32" Width="40"/>
                    <Button Command="{x:Bind Clippy.RefreshChatCommand}" Style="{ThemeResource RefreshButton}" Height="32" Width="40"/>
                </StackPanel>
            </Grid>
        </Grid>
    </Grid>
</winuiex:WindowEx>
