<Page x:Class="OpenGptChat.Views.Pages.ChatPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="clr-namespace:OpenGptChat.Views.Pages"
      xmlns:controls="clr-namespace:OpenGptChat.Controls"
      xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
      xmlns:utilities="clr-namespace:OpenGptChat.Utilities"
      mc:Ignorable="d"
      d:DesignHeight="600" d:DesignWidth="730" 
      d:Background="White" d:DataContext="{d:DesignInstance Type=local:ChatPage}"
      Title="ChatPage"
      FocusVisualStyle="{x:Null}">

    <Page.Resources>
        <utilities:BindingProxy x:Key="PageProxy" Data="{Binding}"/>
    </Page.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="130"/>
        </Grid.RowDefinitions>

        <ScrollViewer Name="messagesScrollViewer"
                      HorizontalScrollBarVisibility="Disabled"
                      VirtualizingPanel.ScrollUnit="Pixel"
                      VirtualizingPanel.CacheLengthUnit="Pixel">
            <behaviors:Interaction.Triggers>
                <behaviors:EventTrigger EventName="Loaded">
                    <behaviors:CallMethodAction MethodName="ScrollToEnd"/>
                </behaviors:EventTrigger>
                <behaviors:EventTrigger EventName="ScrollChanged">
                    <behaviors:InvokeCommandAction Command="{Binding ScrollToEndWhileReceivingCommand}"/>
                </behaviors:EventTrigger>
            </behaviors:Interaction.Triggers>

            <ItemsControl Name="messages" 
                          Padding="10 10 10 20" 
                          ItemsSource="{Binding ViewModel.Messages}"
                          d:ItemsSource="{d:SampleData ItemCount=5}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <controls:ChatBubble BorderThickness="1" CornerRadius="{Binding SelfCornorRadius}" 
                                             HorizontalAlignment="{Binding SelfAlignment}" HorizontalContentAlignment="{Binding SelfAlignment}"
                                             Username="{Binding DisplayName}" Content="{Binding Content,Mode=TwoWay}" IsReadonly="{Binding IsReadOnly}"
                                             ContentForeground="{DynamicResource FontForeground}" ContentBackground="{DynamicResource ChatBubbleBackground}">
                            <controls:ChatBubble.Style>
                                <Style TargetType="controls:ChatBubble" BasedOn="{StaticResource AnimatedChatBubble}">
                                    <Setter Property="HeaderForeground" Value="Gray"/>
                                    <Setter Property="BorderBrush" Value="{DynamicResource ChatBubbleBorder}"/>
                                    <Style.Triggers>
                                        <Trigger Property="IsReadonly" Value="False">
                                            <Setter Property="HeaderForeground" Value="#569de5"/>
                                            <Setter Property="BorderBrush" Value="#569de5"/>
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </controls:ChatBubble.Style>
                            <behaviors:Interaction.Triggers>
                                <behaviors:EventTrigger EventName="MouseDoubleClick">
                                    <behaviors:InvokeCommandAction Command="{Binding StartEditCommand}"/>
                                </behaviors:EventTrigger>
                                <behaviors:EventTrigger EventName="LostFocus">
                                    <behaviors:InvokeCommandAction Command="{Binding EndEditCommand}"/>
                                </behaviors:EventTrigger>
                            </behaviors:Interaction.Triggers>
                            <controls:ChatBubble.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="{DynamicResource StrCopy}" Command="{Binding CopyCommand}"/>
                                    <MenuItem Header="{DynamicResource StrEdit}" Command="{Binding StartEditCommand}"/>
                                    <MenuItem Header="{DynamicResource StrDelete}">
                                        <MenuItem Header="{DynamicResource StrThis}"
                                                  Command="{Binding Source={StaticResource PageProxy},Path=Data.ViewModel.DeleteMessageCommand}"
                                                  CommandParameter="{Binding}"/>
                                        <MenuItem Header="{DynamicResource StrAllAbove}"
                                                  Command="{Binding Source={StaticResource PageProxy},Path=Data.ViewModel.DeleteMessagesAboveCommand}"
                                                  CommandParameter="{Binding}"/>
                                        <MenuItem Header="{DynamicResource StrAllBelow}"
                                                  Command="{Binding Source={StaticResource PageProxy},Path=Data.ViewModel.DeleteMessagesBelowCommand}"
                                                  CommandParameter="{Binding}"/>
                                    </MenuItem>
                                </ContextMenu>
                            </controls:ChatBubble.ContextMenu>
                        </controls:ChatBubble>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>

        <Grid Grid.Row="1">
            <TextBox Name="inputBox" utilities:FocusUtils.IsAutoLogicFocus="True"
                     Padding="5" FontSize="14" BorderThickness="0 1 0 0" AcceptsReturn="True" TextWrapping="Wrap"
                     Text="{Binding ViewModel.InputBoxText,UpdateSourceTrigger=PropertyChanged}">
                <TextBox.InputBindings>
                    <KeyBinding Modifiers="Ctrl" Key="Return" Command="{Binding ChatCommand}"/>
                    <KeyBinding Modifiers="Ctrl+Shift" Key="Return" Command="{Binding CancelCommand}"/>
                </TextBox.InputBindings>
            </TextBox>

            <controls:ConditionalControl Condition="{Binding ChatCommand.IsRunning}"
                                         VerticalAlignment="Bottom" HorizontalAlignment="Right">
                <controls:ConditionalControl.ElementWhileFalse>
                    <Button Content="{DynamicResource StrSend}" Command="{Binding ChatCommand}"
                            Margin="10" Padding="10,5">
                        <Button.ToolTip>
                            <TextBlock>
                                <Run Text="{DynamicResource StrSendMessage}"/>
                                <Run Text="(Ctrl+Enter)"/>
                            </TextBlock>
                        </Button.ToolTip>
                        <Button.Resources>
                            <Style TargetType="Border">
                                <Setter Property="CornerRadius" Value="3"/>
                            </Style>
                        </Button.Resources>
                    </Button>
                </controls:ConditionalControl.ElementWhileFalse>
                <controls:ConditionalControl.ElementWhileTrue>
                    <Button Content="{DynamicResource StrCancel}" Command="{Binding CancelCommand}"
                            Margin="10" Padding="10,5" Style="{StaticResource SecondaryButton}" utilities:UiUtils.CornerRadius="3">
                        <Button.ToolTip>
                            <TextBlock>
                                <Run Text="{DynamicResource StrCancelSendingMessage}"/>
                                <Run Text="(Ctrl+Shift+Enter)"/>
                            </TextBlock>
                        </Button.ToolTip>
                    </Button>
                </controls:ConditionalControl.ElementWhileTrue>
            </controls:ConditionalControl>
        </Grid>

        <GridSplitter Grid.Row="1" VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="3" Background="Transparent"/>

        <controls:ConditionalControl Condition="{Binding ConfigurationService.Configuration.DisableChatAnimation}">
            <controls:ConditionalControl.ElementWhileFalse>
                <controls:ConditionalControl Condition="{Binding ChatCommand.IsRunning}">
                    <controls:ConditionalControl.ElementWhileTrue>
                        <controls:LoadingCircle VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="Gray" Width="30" Margin="10"/>
                    </controls:ConditionalControl.ElementWhileTrue>
                </controls:ConditionalControl>
            </controls:ConditionalControl.ElementWhileFalse>
        </controls:ConditionalControl>
    </Grid>
</Page>
