<Window x:Class="HaCreator.GUI.EditorPanels.AIMapEditWindow"
        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:ai="clr-namespace:HaCreator.MapEditor.AI"
        mc:Ignorable="d"
        Title="AI Map Editor"
        Width="1200" Height="700"
        MinWidth="900" MinHeight="400"
        WindowStartupLocation="CenterOwner"
        Closing="Window_Closing">

    <Window.Resources>
        <!-- Bool to Visibility Converters -->
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

        <!-- Button Styles -->
        <Style x:Key="ToolbarButtonStyle" TargetType="Button">
            <Setter Property="Height" Value="28"/>
            <Setter Property="Padding" Value="10,4"/>
            <Setter Property="Margin" Value="0,0,5,0"/>
        </Style>

        <Style x:Key="PrimaryButtonStyle" TargetType="Button">
            <Setter Property="Height" Value="32"/>
            <Setter Property="Background" Value="#4285F4"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}"
                                CornerRadius="3"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#5A95F5"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Background" Value="#CCCCCC"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="SuccessButtonStyle" TargetType="Button">
            <Setter Property="Height" Value="32"/>
            <Setter Property="Background" Value="#34A853"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}"
                                CornerRadius="3"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#45B864"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Background" Value="#CCCCCC"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="SectionLabelStyle" TargetType="TextBlock">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="0,0,0,5"/>
        </Style>

        <Style x:Key="SubLabelStyle" TargetType="TextBlock">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="Foreground" Value="#646464"/>
            <Setter Property="Margin" Value="0,0,0,3"/>
        </Style>

        <!-- User Message Bubble Style -->
        <Style x:Key="UserMessageBubbleStyle" TargetType="Border">
            <Setter Property="Background" Value="#DCF8C6"/>
            <Setter Property="CornerRadius" Value="12,12,0,12"/>
            <Setter Property="Padding" Value="12,8"/>
            <Setter Property="Margin" Value="60,4,8,4"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="MaxWidth" Value="450"/>
        </Style>

        <!-- Assistant Message Bubble Style -->
        <Style x:Key="AssistantMessageBubbleStyle" TargetType="Border">
            <Setter Property="Background" Value="#F1F0F0"/>
            <Setter Property="CornerRadius" Value="12,12,12,0"/>
            <Setter Property="Padding" Value="12,8"/>
            <Setter Property="Margin" Value="8,4,60,4"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="MaxWidth" Value="450"/>
        </Style>

        <!-- Commands Box Style -->
        <Style x:Key="CommandsBoxStyle" TargetType="Border">
            <Setter Property="Background" Value="#2D2D2D"/>
            <Setter Property="CornerRadius" Value="6"/>
            <Setter Property="Padding" Value="8"/>
            <Setter Property="Margin" Value="0,8,0,0"/>
        </Style>

        <!-- Chat Message Data Template -->
        <DataTemplate x:Key="ChatMessageTemplate" DataType="{x:Type ai:ChatMessage}">
            <Grid Margin="0,2">
                <!-- User Message -->
                <Border Style="{StaticResource UserMessageBubbleStyle}">
                    <Border.Visibility>
                        <Binding Path="IsUser" Converter="{StaticResource BoolToVisibilityConverter}"/>
                    </Border.Visibility>
                    <StackPanel>
                        <RichTextBox ai:MarkdownHelper.Markdown="{Binding Content}"
                                     IsReadOnly="True"
                                     IsDocumentEnabled="True"
                                     Background="Transparent"
                                     BorderThickness="0"
                                     Foreground="#000000"
                                     FontSize="13"
                                     Padding="0"
                                     Cursor="IBeam"/>
                        <TextBlock Text="{Binding TimestampDisplay}"
                                   FontSize="9"
                                   Foreground="#666666"
                                   HorizontalAlignment="Right"
                                   Margin="0,4,0,0"/>
                    </StackPanel>
                </Border>

                <!-- Assistant Message -->
                <Border Style="{StaticResource AssistantMessageBubbleStyle}">
                    <Border.Visibility>
                        <Binding Path="IsAssistant" Converter="{StaticResource BoolToVisibilityConverter}"/>
                    </Border.Visibility>
                    <StackPanel>
                        <!-- Processing indicator with animation -->
                        <StackPanel Orientation="Horizontal">
                            <StackPanel.Visibility>
                                <Binding Path="IsProcessing" Converter="{StaticResource BoolToVisibilityConverter}"/>
                            </StackPanel.Visibility>
                            <StackPanel.Style>
                                <Style TargetType="StackPanel">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsProcessing}" Value="True">
                                            <DataTrigger.EnterActions>
                                                <BeginStoryboard Name="ThinkingAnimation">
                                                    <Storyboard>
                                                        <DoubleAnimation
                                                            Storyboard.TargetProperty="Opacity"
                                                            From="1.0" To="0.4"
                                                            Duration="0:0:0.6"
                                                            AutoReverse="True"
                                                            RepeatBehavior="Forever"/>
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </DataTrigger.EnterActions>
                                            <DataTrigger.ExitActions>
                                                <StopStoryboard BeginStoryboardName="ThinkingAnimation"/>
                                            </DataTrigger.ExitActions>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </StackPanel.Style>
                            <TextBlock Text="Thinking" FontStyle="Italic" Foreground="#666666" FontSize="13"/>
                            <TextBlock Text=" . . ." FontStyle="Italic" Foreground="#666666" FontSize="13"/>
                        </StackPanel>

                        <!-- Message content (hidden while processing) -->
                        <StackPanel>
                            <StackPanel.Style>
                                <Style TargetType="StackPanel">
                                    <Setter Property="Visibility" Value="Visible"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsProcessing}" Value="True">
                                            <Setter Property="Visibility" Value="Collapsed"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </StackPanel.Style>

                            <!-- Error display -->
                            <TextBlock TextWrapping="Wrap"
                                       Foreground="#D32F2F"
                                       FontSize="13">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding HasError}" Value="True">
                                                <Setter Property="Visibility" Value="Visible"/>
                                                <Setter Property="Text" Value="{Binding ErrorMessage}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

                            <!-- Normal content -->
                            <RichTextBox ai:MarkdownHelper.Markdown="{Binding Content}"
                                         IsReadOnly="True"
                                         IsDocumentEnabled="True"
                                         Background="Transparent"
                                         BorderThickness="0"
                                         Foreground="#000000"
                                         FontSize="13"
                                         Padding="0"
                                         Cursor="IBeam">
                                <RichTextBox.Style>
                                    <Style TargetType="RichTextBox">
                                        <Setter Property="Visibility" Value="Visible"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding HasError}" Value="True">
                                                <Setter Property="Visibility" Value="Collapsed"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </RichTextBox.Style>
                            </RichTextBox>

                            <!-- Commands section (collapsible) -->
                            <Expander Header="Generated Commands"
                                      IsExpanded="True"
                                      Margin="0,8,0,0"
                                      FontSize="11">
                                <Expander.Style>
                                    <Style TargetType="Expander">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding HasCommands}" Value="True">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Expander.Style>
                                <Border Style="{StaticResource CommandsBoxStyle}">
                                    <TextBox Text="{Binding CommandsContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                             IsReadOnly="False"
                                             AcceptsReturn="True"
                                             TextWrapping="Wrap"
                                             FontFamily="Consolas"
                                             FontSize="11"
                                             Foreground="#FFFFFF"
                                             Background="Transparent"
                                             BorderThickness="0"
                                             MaxHeight="200"
                                             VerticalScrollBarVisibility="Auto"/>
                                </Border>
                            </Expander>

                            <TextBlock Text="{Binding TimestampDisplay}"
                                       FontSize="9"
                                       Foreground="#666666"
                                       HorizontalAlignment="Left"
                                       Margin="0,4,0,0"/>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </Grid>
        </DataTemplate>
    </Window.Resources>

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

        <!-- Menu Bar -->
        <Menu Grid.Row="0" Background="White">
            <MenuItem Header="_Tools">
                <MenuItem x:Name="menuSettings" Header="_Settings" Click="BtnSettings_Click"
                          ToolTip="Configure OpenRouter API key and model"/>
                <Separator/>
                <MenuItem x:Name="menuRunTests" Header="_Run Tests" Click="BtnRunTests_Click"
                          ToolTip="Run automated tests to verify AI agent capabilities"/>
            </MenuItem>
        </Menu>

        <!-- Main Content: Left (Map Context) | Right (Chat) -->
        <Grid Grid.Row="1" Margin="5,0,5,5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" MinWidth="150"/>
                <ColumnDefinition Width="5"/>
                <ColumnDefinition Width="1.5*" MinWidth="400"/>
            </Grid.ColumnDefinitions>

            <!-- Left Panel: Map Context -->
            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="Map Context (auto-loaded):"
                               Style="{StaticResource SubLabelStyle}"/>
                    <Button x:Name="btnRefresh" Grid.Column="1" Content="Refresh"
                            Style="{StaticResource ToolbarButtonStyle}"
                            Height="22" Padding="8,2" Margin="0"
                            Click="BtnRefresh_Click"
                            ToolTip="Refresh map context after making changes"/>
                </Grid>
                <TextBox x:Name="txtMapContext" Grid.Row="1"
                         IsReadOnly="True"
                         AcceptsReturn="True"
                         TextWrapping="Wrap"
                         HorizontalScrollBarVisibility="Auto"
                         VerticalScrollBarVisibility="Auto"
                         FontFamily="Consolas"
                         FontSize="10"
                         Background="#FAFAFA"/>
            </Grid>

            <GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Center"
                          VerticalAlignment="Stretch" Background="Transparent" Cursor="SizeWE"/>

            <!-- Right Panel: Chat Interface -->
            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <!-- Toolbar -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,5">
                    <Button x:Name="btnClearChat" Content="New Chat"
                            Style="{StaticResource ToolbarButtonStyle}"
                            Click="BtnClearChat_Click"
                            ToolTip="Start a new conversation"/>
                </StackPanel>

                <!-- Chat Messages Area -->
                <Border Grid.Row="1" BorderBrush="#E0E0E0" BorderThickness="1" CornerRadius="4" Background="White">
                    <ScrollViewer x:Name="chatScrollViewer"
                                  VerticalScrollBarVisibility="Auto"
                                  HorizontalScrollBarVisibility="Disabled">
                        <ItemsControl x:Name="chatItemsControl"
                                      ItemTemplate="{StaticResource ChatMessageTemplate}"
                                      VirtualizingPanel.IsVirtualizing="False"
                                      Padding="4">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </ScrollViewer>
                </Border>

                <!-- Message Input Area -->
                <Grid Grid.Row="2" Margin="0,8,0,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <Border Grid.Column="0" BorderBrush="#CCCCCC" BorderThickness="1" CornerRadius="4" Background="White">
                        <TextBox x:Name="txtMessageInput"
                                 AcceptsReturn="True"
                                 TextWrapping="Wrap"
                                 VerticalScrollBarVisibility="Auto"
                                 MaxHeight="100"
                                 MinHeight="40"
                                 FontSize="13"
                                 Padding="8"
                                 BorderThickness="0"
                                 PreviewKeyDown="TxtMessageInput_PreviewKeyDown"
                                 TextChanged="TxtMessageInput_TextChanged"/>
                    </Border>

                    <Button x:Name="btnSend" Grid.Column="1"
                            Content="Send"
                            Style="{StaticResource PrimaryButtonStyle}"
                            Width="70"
                            Margin="8,0,0,0"
                            Click="BtnSend_Click"
                            ToolTip="Send message (Enter)"/>
                </Grid>

                <!-- Execute Commands Button -->
                <Button x:Name="btnExecute" Grid.Row="3"
                        Content="Execute Latest Commands"
                        Style="{StaticResource SuccessButtonStyle}"
                        Margin="0,8,0,0"
                        Click="BtnExecute_Click"
                        ToolTip="Apply the commands from the last AI response"/>
            </Grid>
        </Grid>
    </Grid>
</Window>
