<Window xmlns="https://github.com/avaloniaui"
        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:vm="using:ClawdDotNet.Desktop.ViewModels"
        xmlns:ctrl="using:ClawdDotNet.Desktop.Controls"
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="560"
        x:Class="ClawdDotNet.Desktop.Views.InstancePickerWindow"
        x:DataType="vm:InstancePickerViewModel"
        Icon="/Assets/app.ico"
        Title="ClawdDotNet – Instanz"
        Width="640" Height="560"
        MinWidth="520" MinHeight="420"
        WindowStartupLocation="CenterScreen">

  <!-- Rahmen mit Eckmarken wie im Entwurf; 12 Punkte Abstand, weil die Marken nach
       aussen ragen. -->
  <ctrl:BlueprintFrame Margin="12" Padding="16">
    <DockPanel>

      <StackPanel DockPanel.Dock="Top" Spacing="6" Margin="0,0,0,14">
        <TextBlock Classes="h1" Text="{Binding Title}" />
        <TextBlock Classes="muted" FontSize="13" Text="{Binding Hint}" TextWrapping="Wrap" />
        <TextBlock Classes="caption" TextWrapping="Wrap"
                   Text="{Binding InstancesDirectory, StringFormat='Verzeichnis: {0}'}" />
      </StackPanel>

      <!-- Fehlerzeile: nimmt keinen Platz weg, solange nichts anliegt -->
      <TextBlock DockPanel.Dock="Bottom" Margin="0,10,0,0" TextWrapping="Wrap"
                 Foreground="{DynamicResource DangerBrush}"
                 Text="{Binding ErrorText}"
                 IsVisible="{Binding ErrorText, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" />

      <!-- Neue Instanz anlegen -->
      <StackPanel DockPanel.Dock="Bottom" Spacing="10" Margin="0,14,0,0">
        <Border Classes="hr" />
        <Grid ColumnDefinitions="*,Auto">
          <TextBox Grid.Column="0" Text="{Binding NewInstanceName}"
                   PlaceholderText="Name der neuen Instanz" />
          <Button Grid.Column="1" Classes="primary" Margin="8,0,0,0"
                  Command="{Binding CreateCommand}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconPlus}" Width="14" Height="14" />
              <TextBlock Text="Anlegen" />
            </StackPanel>
          </Button>
        </Grid>
      </StackPanel>

      <!-- Abschluss -->
      <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Spacing="8"
                  HorizontalAlignment="Right" Margin="0,14,0,0">
        <Button Content="Abbrechen" Command="{Binding CancelCommand}" IsCancel="True" />
        <Button Classes="primary" Content="{Binding ConfirmLabel}" IsDefault="True"
                Command="{Binding StartCommand}" />
      </StackPanel>

      <!-- Liste -->
      <ScrollViewer VerticalScrollBarVisibility="Auto">
        <ItemsControl x:Name="InstanceList" ItemsSource="{Binding Instances}">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <StackPanel Spacing="6" />
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>

          <ItemsControl.ItemTemplate>
            <DataTemplate DataType="vm:InstanceChoiceViewModel">
              <Border Classes="card" Padding="8,8">
                <DockPanel>

                  <!-- Monogramm mit Statuspunkt -->
                  <Panel DockPanel.Dock="Left" Width="30" Height="30" Margin="0,0,10,0"
                         VerticalAlignment="Center">
                    <Border Width="26" Height="26" Background="{DynamicResource Accent600Brush}"
                            HorizontalAlignment="Left" VerticalAlignment="Top">
                      <TextBlock Classes="h2" FontSize="10" Text="{Binding Monogram}"
                                 Foreground="{DynamicResource Neutral100Brush}"
                                 HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>

                    <Ellipse Width="10" Height="10" HorizontalAlignment="Right"
                             VerticalAlignment="Bottom"
                             Stroke="{DynamicResource SurfaceBrush}" StrokeThickness="2"
                             Fill="{DynamicResource SuccessBrush}"
                             IsVisible="{Binding IsReady}"
                             ToolTip.Tip="API-Key hinterlegt" />
                    <Ellipse Width="10" Height="10" HorizontalAlignment="Right"
                             VerticalAlignment="Bottom"
                             Stroke="{DynamicResource SurfaceBrush}" StrokeThickness="2"
                             Fill="{DynamicResource Neutral500Brush}"
                             IsVisible="{Binding !IsReady}"
                             ToolTip.Tip="Kein API-Key — Agenten bleiben deaktiviert" />
                  </Panel>

                  <Button DockPanel.Dock="Right" Classes="toolbar" VerticalAlignment="Center"
                          Content="{Binding $parent[ItemsControl].((vm:InstancePickerViewModel)DataContext).ConfirmLabel}"
                          Command="{Binding $parent[ItemsControl].((vm:InstancePickerViewModel)DataContext).ActivateCommand}"
                          CommandParameter="{Binding}" />

                  <Border DockPanel.Dock="Right" Classes="tag ok" Margin="0,0,8,0"
                          IsVisible="{Binding IsCurrent}">
                    <TextBlock Text="Aktiv" />
                  </Border>

                  <StackPanel VerticalAlignment="Center">
                    <TextBlock Classes="h2" FontSize="13.5" Text="{Binding InstanceName}"
                               TextTrimming="CharacterEllipsis" />
                    <TextBlock Classes="caption" Text="{Binding Summary}"
                               TextTrimming="CharacterEllipsis" />
                  </StackPanel>

                </DockPanel>
              </Border>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
      </ScrollViewer>

    </DockPanel>
  </ctrl:BlueprintFrame>

</Window>
