<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:ClaudeDo.Ui.ViewModels"
             xmlns:vmi="using:ClaudeDo.Ui.ViewModels.Islands"
             xmlns:vmm="using:ClaudeDo.Ui.ViewModels.MissionControl"
             xmlns:mc="using:ClaudeDo.Ui.Views.MissionControl"
             xmlns:loc="using:ClaudeDo.Ui.Localization"
             x:DataType="vm:MissionControlViewModel"
             x:Class="ClaudeDo.Ui.Views.MissionControl.MissionControlView">
  <UserControl.DataTemplates>
    <!-- Polymorphic pane templates: both the grid ItemsControl and the focus-mode TabControl
         resolve per-item content through these (no explicit ItemTemplate on either). -->
    <DataTemplate DataType="vmi:TaskMonitorViewModel">
      <mc:MonitorPaneView Margin="6" />
    </DataTemplate>
    <DataTemplate DataType="vmm:ConPtyPaneViewModel">
      <mc:ConPtyPaneView Margin="6" />
    </DataTemplate>
  </UserControl.DataTemplates>
  <DockPanel LastChildFill="True" Background="{DynamicResource VoidBrush}"
             DragDrop.AllowDrop="True">

    <!-- Header -->
    <Border DockPanel.Dock="Top"
            Background="{DynamicResource DeepBrush}"
            BorderBrush="{DynamicResource LineBrush}"
            BorderThickness="0,0,0,1" Padding="14,8">
      <Grid ColumnDefinitions="*,Auto">
        <TextBlock Grid.Column="0" Classes="eyebrow"
                   Text="{loc:Tr missionControl.windowTitle}"
                   Foreground="{DynamicResource TextBrush}"
                   LetterSpacing="1.4" VerticalAlignment="Center" />
        <StackPanel Grid.Column="1" Orientation="Horizontal" Spacing="8"
                    VerticalAlignment="Center">
          <Button Classes="icon-btn"
                  Click="OnNewSessionClicked"
                  ToolTip.Tip="{loc:Tr missionControl.newSession}">
            <PathIcon Data="{StaticResource Icon.Plus}" Width="15" Height="15"
                      Foreground="{DynamicResource TextMuteBrush}"/>
          </Button>
          <Button Classes="icon-btn"
                  Command="{Binding ToggleLayoutCommand}"
                  ToolTip.Tip="{Binding LayoutToggleLabel}">
            <PathIcon Data="{StaticResource Icon.Grid}" Width="15" Height="15"
                      Foreground="{DynamicResource TextMuteBrush}"/>
          </Button>
          <Button Classes="icon-btn"
                  Command="{Binding OpenSettingsCommand}"
                  ToolTip.Tip="{loc:Tr missionControl.settings}">
            <PathIcon Data="{StaticResource Icon.Settings}" Width="15" Height="15"
                      Foreground="{DynamicResource TextMuteBrush}"/>
          </Button>
          <Button Classes="btn"
                  Content="{loc:Tr missionControl.clearFinished}"
                  Command="{Binding ClearFinishedCommand}" />
        </StackPanel>
      </Grid>
    </Border>

    <!-- Read-only queue strip — collapses when nothing is queued -->
    <Border DockPanel.Dock="Right"
            IsVisible="{Binding HasQueued}"
            Width="210"
            Background="{DynamicResource DeepBrush}"
            BorderBrush="{DynamicResource LineBrush}"
            BorderThickness="1,0,0,0">
      <DockPanel LastChildFill="True" Margin="10,10">
        <TextBlock DockPanel.Dock="Top" Classes="eyebrow"
                   Text="{loc:Tr missionControl.queue}"
                   Foreground="{DynamicResource TextMuteBrush}"
                   LetterSpacing="1.4" Margin="0,0,0,8" />
        <ScrollViewer>
          <ItemsControl ItemsSource="{Binding Queued}">
            <ItemsControl.ItemTemplate>
              <DataTemplate x:DataType="vm:QueuedTaskViewModel">
                <Border Margin="0,0,0,4" Padding="8,6"
                        Background="{DynamicResource SurfaceBrush}"
                        BorderBrush="{DynamicResource LineBrush}"
                        BorderThickness="1" CornerRadius="6">
                  <StackPanel Spacing="2">
                    <TextBlock Text="{Binding Title}"
                               TextTrimming="CharacterEllipsis"
                               ToolTip.Tip="{Binding Title}"
                               Foreground="{DynamicResource TextDimBrush}" />
                    <TextBlock Classes="meta"
                               Text="{loc:Tr missionControl.blocked}"
                               IsVisible="{Binding IsBlocked}"
                               Foreground="{DynamicResource AmberBrush}" />
                  </StackPanel>
                </Border>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </ScrollViewer>
      </DockPanel>
    </Border>

    <!-- Grid / tabs / empty state -->
    <Panel Margin="6">
      <!-- Overview mode: the existing UniformGrid, one tile per pane -->
      <ItemsControl ItemsSource="{Binding Panes}" IsVisible="{Binding !IsFocusMode}">
        <ItemsControl.ItemsPanel>
          <ItemsPanelTemplate>
            <UniformGrid x:CompileBindings="False" Columns="{Binding ColumnCount}" />
          </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
      </ItemsControl>

      <!-- Focus mode: one pane large, switched via tabs -->
      <TabControl ItemsSource="{Binding Panes}"
                  SelectedItem="{Binding FocusedPane}"
                  IsVisible="{Binding IsFocusMode}">
        <TabControl.ItemTemplate>
          <DataTemplate x:DataType="vmm:IMissionControlPane">
            <TextBlock Text="{Binding DisplayTitle}" TextTrimming="CharacterEllipsis" MaxWidth="160" />
          </DataTemplate>
        </TabControl.ItemTemplate>
      </TabControl>

      <TextBlock IsVisible="{Binding !HasPanes}"
                 Text="{loc:Tr missionControl.empty}"
                 Foreground="{DynamicResource TextMuteBrush}"
                 HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Panel>

  </DockPanel>
</UserControl>
