<Page x:Class="OpenGptChat.Views.Pages.MainPage"
      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:s="clr-namespace:System;assembly=mscorlib"
      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"
      xmlns:vm="clr-namespace:OpenGptChat.ViewModels"
      mc:Ignorable="d" 
      d:DesignHeight="600" d:DesignWidth="880"
      d:DataContext="{d:DesignInstance Type=local:MainPage}" d:Background="White"
      Title="MainPage" Style="{DynamicResource AnimatedPageStyle}"
      FocusVisualStyle="{x:Null}">
    <Page.Resources>
        <utilities:BindingProxy x:Key="PageSelf" Data="{Binding}"/>
    </Page.Resources>

    <Page.InputBindings>
        <KeyBinding Modifiers="Ctrl" Key="R" Command="{Binding ResetChatCommand}"/>
        <KeyBinding Modifiers="Ctrl" Key="T" Command="{Binding NewSessionCommand}"/>
        <KeyBinding Modifiers="Ctrl" Key="N" Command="{Binding NewSessionCommand}"/>
        <KeyBinding Modifiers="Ctrl" Key="W" Command="{Binding DeleteCurrentSessionCommand}"/>
        <KeyBinding Modifiers="Ctrl" Key="K" Command="{Binding SwitchToPreviousSessionCommand}"/>
        <KeyBinding Modifiers="Ctrl" Key="J" Command="{Binding SwitchToNextSessionCommand}"/>
        <KeyBinding Modifiers="Ctrl+Shift" Key="Tab" Command="{Binding CycleSwitchToPreviousSessionCommand}"/>
        <KeyBinding Modifiers="Ctrl" Key="Tab" Command="{Binding CycleSwitchToNextSessionCommand}"/>
    </Page.InputBindings>

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

        <Grid>
            <Border Padding="5" BorderThickness="0 0 0 1" BorderBrush="{DynamicResource GeneralBorder}">
                <Grid>
                    <TextBlock Text="{DynamicResource StrChat}"
                               FontSize="{DynamicResource ExtremLargeFontSize}" Foreground="{DynamicResource FontForeground}"/>
                    <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                        <StackPanel.Resources>
                            <Style TargetType="Border">
                                <Setter Property="CornerRadius" Value="3"/>
                            </Style>
                        </StackPanel.Resources>
                        <Button Content="♻" BorderThickness="0" Padding="3" Margin="0 0 3 0"
                                Foreground="{DynamicResource FontForeground}" Background="{DynamicResource ControlBackground}"
                                Command="{Binding ResetChatCommand}">
                            <Button.ToolTip>
                                <TextBlock>
                                    <Run Text="{DynamicResource StrResetChat}"/>
                                    <Run Text="(Ctrl+R)"/>
                                </TextBlock>
                            </Button.ToolTip>
                        </Button>
                        <Button Content="⚙︎" BorderThickness="0" Padding="3"
                                Foreground="{DynamicResource FontForeground}" Background="{DynamicResource ControlBackground}"
                                Command="{Binding GoToConfigPageCommand}" ToolTip="{DynamicResource StrGoToConfigurationPage}"/>
                    </StackPanel>
                </Grid>
            </Border>
        </Grid>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150" MinWidth="50"/>
                <ColumnDefinition Width="1"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

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

                <ScrollViewer Name="sessionsScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                    <ListBox HorizontalAlignment="Stretch" SelectedIndex="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                         ItemsSource="{Binding AppGlobalData.Sessions}" SelectedItem="{Binding AppGlobalData.SelectedSession}"
                         Margin="0" Padding="0" BorderThickness="0" Background="{DynamicResource GeneralBackground}" d:ItemsSource="{d:SampleData ItemCount=5}">
                        <behaviors:Interaction.Triggers>
                            <behaviors:EventTrigger EventName="SelectionChanged">
                                <behaviors:InvokeCommandAction Command="{Binding SwitchPageToCurrentSessionCommand}"/>
                            </behaviors:EventTrigger>
                        </behaviors:Interaction.Triggers>
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem" BasedOn="{StaticResource AnimatedListItemStyle}">
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ListBoxItem">
                                            <Border Background="{TemplateBinding Background}" Padding="5">
                                                <ContentPresenter/>
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Background" Value="{DynamicResource SessionHoverBackground}"/>
                                                </Trigger>
                                                <Trigger Property="IsSelected" Value="True">
                                                    <Setter Property="Background" Value="{DynamicResource SessionSelectedBackgroun}"/>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <DataTemplate.Resources>
                                    <ContextMenu x:Key="SessionContextMenu">
                                        <MenuItem Header="{DynamicResource StrEdit}" Command="{Binding StartEditCommand}"/>
                                        <MenuItem Header="{DynamicResource StrConfig}" Command="{Binding ConfigCommand}"/>
                                        <MenuItem Header="{DynamicResource StrDelete}"
                                                  Command="{Binding Data.DeleteSessionCommand,Source={StaticResource PageSelf}}"
                                                  CommandParameter="{Binding}"/>
                                    </ContextMenu>
                                </DataTemplate.Resources>
                                <StackPanel Background="Transparent" ContextMenu="{StaticResource SessionContextMenu}">
                                    <StackPanel.InputBindings>
                                        <MouseBinding MouseAction="MiddleClick" 
                                                      Command="{Binding Data.DeleteSessionCommand,Source={StaticResource PageSelf}}"
                                                      CommandParameter="{Binding}"/>
                                    </StackPanel.InputBindings>
                                    <TextBox BorderThickness="1" Padding="0"
                                             Foreground="{DynamicResource FontForeground}"  Background="Transparent" 
                                             IsReadOnly="{Binding IsReadOnly}" IsHitTestVisible="{Binding IsEditing}"
                                             Text="{Binding Name,TargetNullValue='New Session'}" FontSize="{DynamicResource NormalFontSize}">
                                        <behaviors:Interaction.Triggers>
                                            <behaviors:EventTrigger EventName="LostFocus">
                                                <behaviors:InvokeCommandAction Command="{Binding EndEditCommand}"/>
                                            </behaviors:EventTrigger>
                                        </behaviors:Interaction.Triggers>
                                        <TextBox.InputBindings>
                                            <KeyBinding Key="Return" Command="{Binding EndEditCommand}"/>
                                        </TextBox.InputBindings>
                                        <TextBox.Style>
                                            <Style TargetType="TextBox">
                                                <Setter Property="BorderBrush" Value="Transparent"/>
                                                <Style.Triggers>
                                                    <Trigger Property="IsReadOnly" Value="False">
                                                        <Setter Property="BorderBrush" Value="#569de5"/>
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBox.Style>

                                    </TextBox>

                                    <TextBlock HorizontalAlignment="Stretch" Background="Transparent" Foreground="{DynamicResource FontForegroundLight}"
                                               Padding="3 0" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" FontSize="{DynamicResource SmallFontSize}">
                                        <TextBlock.Resources>
                                            <s:String x:Key="SessionMessageNameFormat">{0}:</s:String>
                                        </TextBlock.Resources>
                                        <Run Text="{Binding PageModel.LastMessage.DisplayName,Mode=OneWay,StringFormat={StaticResource SessionMessageNameFormat}}"/>
                                        <Run Text="{Binding PageModel.LastMessage.SingleLineContent,Mode=OneWay,FallbackValue='Empty'}" />
                                    </TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </ScrollViewer>

                <Button Grid.Row="1"  BorderThickness="0" Padding="0 3"
                        Foreground="{DynamicResource FontForeground}" Background="{DynamicResource ControlBackground}"
                        Content="{DynamicResource StrNewSession}"
                        Command="{Binding NewSessionCommand}">
                    <Button.ToolTip>
                        <TextBlock>
                            <Run Text="{DynamicResource StrNewSession}"/>
                            <Run Text="(Ctrl+T/N)"/>
                        </TextBlock>
                    </Button.ToolTip>
                </Button>
            </Grid>

            <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{DynamicResource GeneralBorder}"/>

            <Frame Grid.Column="2" Content="{Binding ViewModel.CurrentChat}" NavigationUIVisibility="Hidden"/>
        </Grid>
    </Grid>
</Page>
