<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:ClaudeDo.Ui.ViewModels.Modals"
        xmlns:converters="using:ClaudeDo.Ui.Converters"
        x:Class="ClaudeDo.Ui.Views.Modals.WorktreeModalView"
        x:DataType="vm:WorktreeModalViewModel"
        Title="Worktree"
        Width="1100" Height="720"
        MinWidth="640" MinHeight="400"
        WindowStartupLocation="CenterOwner"
        WindowDecorations="BorderOnly"
        ExtendClientAreaToDecorationsHint="True"
        ExtendClientAreaTitleBarHeightHint="-1"
        Background="Transparent"
        CanResize="True"
        TransparencyLevelHint="AcrylicBlur">

  <Window.Resources>
    <converters:DiffLineKindToBrushConverter x:Key="DiffLineKindToBrush"/>
  </Window.Resources>

  <Window.KeyBindings>
    <KeyBinding Gesture="Escape" Command="{Binding CloseCommand}"/>
  </Window.KeyBindings>

  <Border Classes="island" Margin="12">
    <DockPanel>

      <!-- Title strip -->
      <Border DockPanel.Dock="Top" Height="36"
              PointerPressed="OnTitleBarPressed">
        <Grid ColumnDefinitions="*,Auto" Margin="14,0">
          <TextBlock Grid.Column="0" Text="Worktree" VerticalAlignment="Center"
                     FontFamily="{DynamicResource MonoFont}" FontSize="12"
                     Foreground="{DynamicResource TextMuteBrush}"/>
          <Button Grid.Column="1" Classes="icon-btn" Content="✕"
                  Command="{Binding CloseCommand}" VerticalAlignment="Center"/>
        </Grid>
      </Border>

      <!-- Path strip -->
      <Border DockPanel.Dock="Top" Padding="14,0,14,8">
        <TextBlock Text="{Binding WorktreePath}"
                   FontFamily="{DynamicResource MonoFont}" FontSize="11"
                   Foreground="{DynamicResource TextFaintBrush}"
                   TextTrimming="CharacterEllipsis"/>
      </Border>

      <!-- Split: file tree | splitter | diff pane -->
      <Grid ColumnDefinitions="260,4,*">

        <!-- Left: file tree -->
        <TreeView x:Name="FileTree"
                  Grid.Column="0"
                  ItemsSource="{Binding Root}"
                  SelectedItem="{Binding SelectedNode, Mode=TwoWay}"
                  Background="Transparent"
                  Margin="8,0,4,8">
          <TreeView.Styles>
            <Style Selector="TreeViewItem" x:DataType="vm:WorktreeNodeViewModel">
              <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
            </Style>
          </TreeView.Styles>
          <TreeView.ItemTemplate>
            <TreeDataTemplate DataType="vm:WorktreeNodeViewModel"
                              ItemsSource="{Binding Children}">
              <Border Background="Transparent" Tapped="OnNodeTapped" Cursor="Hand">
              <StackPanel Orientation="Horizontal" Spacing="8">
                <TextBlock Text="{Binding Name}"
                           FontFamily="{DynamicResource MonoFont}" FontSize="12"
                           Foreground="{DynamicResource TextBrush}"/>
                <Border Tag="{Binding Status}" CornerRadius="3" Padding="4,0"
                        VerticalAlignment="Center"
                        IsVisible="{Binding Status, Converter={x:Static ObjectConverters.IsNotNull}}">
                  <TextBlock Text="{Binding Status}"
                             FontFamily="{DynamicResource MonoFont}" FontSize="10"
                             Foreground="{DynamicResource TextBrush}"/>
                </Border>
              </StackPanel>
              </Border>
            </TreeDataTemplate>
          </TreeView.ItemTemplate>
        </TreeView>

        <!-- Splitter -->
        <GridSplitter Grid.Column="1" ResizeDirection="Columns" Background="{DynamicResource LineBrush}"/>

        <!-- Right: diff content -->
        <ScrollViewer Grid.Column="2" Padding="8"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto"
                      Margin="4,0,8,8">
          <ItemsControl ItemsSource="{Binding SelectedFileDiffLines}">
            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="vm:WorktreeDiffLineViewModel">
                <SelectableTextBlock Text="{Binding Text}"
                                     FontFamily="{DynamicResource MonoFont}"
                                     FontSize="11"
                                     Foreground="{Binding Kind, Converter={StaticResource DiffLineKindToBrush}}"
                                     TextWrapping="NoWrap"/>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </ScrollViewer>

      </Grid>

    </DockPanel>
  </Border>
</Window>
