<Window x:Class="ReAct_PME.ChatRoom"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ReAct_PME"
        mc:Ignorable="d"
        Title="ChatRoom" Height="650" Width="1100" Background="#FFF0F0F0">

    <Window.Resources>
        <local:RoleToColorConverter x:Key="RoleToColorConverter" />
        <local:RoleToColorTextConverter x:Key="RoleToColorTextConverter" />
        <local:RoleToAlignmentConverter x:Key="RoleToAlignmentConverter" />
        <local:RoleToIconConverter x:Key="RoleToIconConverter" />
        <Style x:Key="ChatBubbleStyle" TargetType="Border">
            <Setter Property="CornerRadius" Value="10" />
            <Setter Property="Padding" Value="10" />
            <Setter Property="Margin" Value="5" />
            <Setter Property="MaxWidth" Value="700" />
        </Style>
        <DropShadowEffect x:Key="ShadowEffect"
                  BlurRadius="8"
                  ShadowDepth="2"
                  Opacity="0.4"
                  Direction="270"/>
    </Window.Resources>

    <DockPanel>

            <!-- Barre supérieure avec les 3 boutons côte à côte -->
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="5">
            <!-- Bouton de suppression des conversations -->
            <Button x:Name="BtnDeleteConv"  Style="{DynamicResource ButtonStyle}" Content="🗑" Width="74" Click="BtnDeleteConv_Click" Margin="0,0,20,0" Height="35" ToolTip="Supprimer la conversation sélectionnée" />
            <!-- Bouton pour créer une nouveau conversation -->
            <Button x:Name="BtnNewConv"  Style="{DynamicResource ButtonStyle}" Content="Nouvelle conversation" Width="138" Click="BtnNewConv_Click" Margin="100,0,10,0" Height="35" ToolTip="Créer une nouvelle conversation"/>
            <!-- Liste déroulante des modèles disponibles -->
            <ComboBox x:Name="CboModels" Width="300"  Style="{DynamicResource ComboBoxStyle}"
                      ItemsSource="{Binding InstalledModels}"
                      SelectedValuePath="Name"
                      DisplayMemberPath="Name"
                      IsSynchronizedWithCurrentItem="True"
                      SelectedItem="{Binding SelectedModel}"
                      IsEnabled="{Binding CanChangeModel}" Height="31" ToolTip="Sélectionner un modèle" Visibility="Visible" />

            <ComboBox x:Name="CboDomain" Width="300"  Style="{DynamicResource ComboBoxStyle}"
                      ItemsSource="{Binding DomainListe}"
                      SelectedItem="{Binding DomainSelected}"
                      Visibility = "{Binding IsVisibleDomain}" Height="31" ToolTip="Sélectionner un domaine" />

        </StackPanel>

        <Popup x:Name="ToastPopup"
               Placement="Bottom"
               PlacementTarget="{Binding ElementName=MainGrid}" 
               StaysOpen="False"
               AllowsTransparency="True"
               PopupAnimation="Fade"
               HorizontalOffset="0"
               VerticalOffset="-30">

            <Border Background="#FF323232"
                    CornerRadius="8"
                    Padding="16"
                    Margin="10"
                    Effect="{DynamicResource ShadowEffect}">
                <TextBlock x:Name="ToastMessage"
                           Foreground="White"
                           FontWeight="Bold"
                           FontSize="14"
                           TextWrapping="Wrap"
                           MaxWidth="300"/>
            </Border>
        </Popup>
        <!-- Contenu principal en deux colonnes -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Grid Grid.Column="0" Margin="0">
                <Grid.RowDefinitions>
                    <!-- Liste des conversations -->
                    <RowDefinition Height="350"/>
                    <!-- Bouton sélectionner -->
                    <RowDefinition Height="Auto"/>
                    <!-- Liste des images (s'étire) -->
                    <RowDefinition Height="*"/>
                    <!-- Bouton supprimer -->
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <!-- Liste des conversations -->
                <ListBox Grid.Row="0" Style="{DynamicResource SimpleListBox}"
                        ItemsSource="{Binding Conversations}"
                        SelectedItem="{Binding SelectedConversation}"
                        SelectionChanged="OnConversationSelected"
                         Width="190"
                        Margin="5">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Title}"   
                                ToolTip="{Binding TitleLong}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

                <!-- Bouton Selectionner des images -->
                <Button x:Name="BtnSelectDoc" Grid.Row="1" Content="Ajout de documents"  Style="{DynamicResource ButtonStyle}" Click="BtnSelectDocumentsClick" Margin="0,10,0,0" Height="35" Width="190" ToolTip="Sélectionner des documents (un à un)"
                        IsEnabled="{Binding IsDisabledWhenRAG}"/>
                <!-- Liste des images sélectionnées -->
                <ListView Grid.Row="2" x:Name="SelectedDocumentsListView"  Style="{DynamicResource SimpleListBox}"
                      Width="190"
                      ItemsSource="{Binding SelectedDocumentsListe}"
                      Margin="0,5,0,0"
                      VerticalAlignment="Stretch" IsEnabled="{Binding IsDisabledWhenRAG}" />
                <!-- Bouton Supprimer une image -->
                <Button x:Name="BtnDeleteDoc" Grid.Row="3" Style="{DynamicResource ButtonStyle}" Content="🗑" Width="74" Click="BtnDeleteDoc_Click" HorizontalAlignment="Left" Margin="5,5,0,5" Height="35" ToolTip="Vider la liste des documents sélectionnés"
                        IsEnabled="{Binding IsDisabledWhenRAG}"/>
            </Grid>

            <!-- Zone de chat -->
            <Grid Grid.Column="1" Margin="5">
                <Grid.RowDefinitions>
                    <!-- historique de la conversation -->                    
                    <RowDefinition Height="*" />
                    <!-- saisie -->
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <!-- historique de la conversation -->
                <ScrollViewer x:Name="ChatScrollViewer"
                                VerticalScrollBarVisibility="Auto"
                                HorizontalScrollBarVisibility="Disabled"
                                ScrollChanged="ChatScrollViewer_ScrollChanged">
                    <ItemsControl ItemsSource="{Binding CurrentMessages}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <DockPanel HorizontalAlignment="{Binding Role, Converter={StaticResource RoleToAlignmentConverter}}">

                                    <!-- Icône rôle -->
                                    <TextBlock Text="{Binding Role, Converter={StaticResource RoleToIconConverter}}"
                                               FontSize="18"
                                               VerticalAlignment="Top"
                                               Margin="5" />
                                    

                                    <!-- Bulle -->
                                    <Border Style="{StaticResource ChatBubbleStyle}"
                                            Background="{Binding Role, Converter={StaticResource RoleToColorConverter}}"
                                            HorizontalAlignment="{Binding Role, Converter={StaticResource RoleToAlignmentConverter}}">
                                        <TextBlock Text="{Binding Content}"
                                               Foreground="{Binding Role, Converter={StaticResource RoleToColorTextConverter}}"
                                               TextWrapping="Wrap"
                                               TextTrimming="None"
                                               HorizontalAlignment="Stretch"
                                               VerticalAlignment="Top" 
                                               ToolTip="{Binding Model}"
                                               x:Name="TxtMessage">
                                            <TextBlock.ContextMenu>
                                                <ContextMenu>
                                                    <MenuItem Header="Copier" Click="CopierMenuItem_Click"/>
                                                </ContextMenu>
                                            </TextBlock.ContextMenu>
                                        </TextBlock>
                                    </Border>
                                </DockPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>

                <!-- Zone de saisie et bouton Envoyer -->
                <StackPanel Grid.Row="1"  Margin="0,10,0,0">
                    <!-- Zone de saisie -->
                    <TextBox Text="{Binding NewMessage, UpdateSourceTrigger=PropertyChanged}"  Style="{DynamicResource TextBoxStyle}"
                             VerticalAlignment="Center"
                             Margin="0,0,10,0"
                             DockPanel.Dock="Left"
                             MinHeight="30"
                             Height="70"
                             AcceptsReturn="True"
                             TextWrapping="Wrap"
                             HorizontalAlignment="Stretch" KeyDown="TextBox_KeyDown" ToolTip="Saisir votre requête et CTRL + ENTREE pour l'envoyer (ou le bouton 'Envoyer')"/>
                    <CheckBox x:Name="ChkApiExterne" Content="API Externe ?" IsChecked="{Binding IsApiExterne}" Style="{DynamicResource SimpleCheckBox}" Width="100" HorizontalAlignment="Left" Margin="0,5,0,0" ToolTip="Vous faites appel à une API Externe (ex : OpenAI pour ChatGPT)" />
                    <CheckBox x:Name="ChkRAG" Content="RAG ?" IsChecked="{Binding IsRAG}" Style="{DynamicResource SimpleCheckBox}" Width="100" HorizontalAlignment="Left" Margin="0,5,0,0" ToolTip="RAG pour chercher des informations dans les documents ingérés" />
                    <CheckBox x:Name="ChkGenerateImg" Content="Générer une image ?" IsChecked="{Binding IsGenerateImg}" Style="{DynamicResource SimpleCheckBox}" Width="132" HorizontalAlignment="Left" Margin="0,5,0,0" ToolTip="Générateur d'images" />
                    <CheckBox x:Name="ChkIsWithAssistant" Content="Aide d'un Assistant IA ?" 
                              IsEnabled="{Binding IsOllamaActif}"
                              IsChecked="{Binding IsWithAssistant}" Style="{DynamicResource SimpleCheckBox}" Width="150" HorizontalAlignment="Left" Margin="0,5,0,0" ToolTip="Un agent IA créera une demande de génération d'image sur la base de votre prompt (cela va augmenter le temps d'attente)" />

                    <!-- bouton Envoyer -->
                    <Button x:Name="BtnEnvoyer" Content="Envoyer" Click="Button_Click" IsDefault="True" Width="100" Margin="0,5,0,0"  Style="{DynamicResource ButtonStyle}" Height="35" ToolTip="Envoyer votre requête" />
                </StackPanel>
            </Grid>
        </Grid>
    </DockPanel>
</Window>
