<UserControl 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:m="using:SourceGit.Models"
             xmlns:vm="using:SourceGit.ViewModels"
             xmlns:v="using:SourceGit.Views"
             xmlns:c="using:SourceGit.Converters"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="SourceGit.Views.StashesPage"
             x:DataType="vm:StashesPage">
  <Grid SizeChanged="OnMainLayoutSizeChanged">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="{Binding Source={x:Static vm:Preferences.Instance}, Path=Layout.StashesLeftWidth, Mode=TwoWay}" MinWidth="300"/>
      <ColumnDefinition Width="4"/>
      <ColumnDefinition Width="*" MinWidth="300"/>
    </Grid.ColumnDefinitions>

    <!-- Left -->
    <Grid Grid.Column="0" RowDefinitions="28,36,*,28,*">
      <!-- Stash Bar -->
      <Grid Grid.Row="0" ColumnDefinitions="Auto,*">
        <Path Grid.Column="0" Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Stashes}"/>
        <TextBlock Grid.Column="1" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="4,0,0,0">
          <Run Text="{DynamicResource Text.Stashes.Stashes}"/>
          <Run Text="{Binding Stashes, Converter={x:Static c:ListConverters.ToCount}, Mode=OneWay}"/>
        </TextBlock>
      </Grid>

      <!-- Search Bar -->
      <Border Grid.Row="1" BorderThickness="0,1" BorderBrush="{DynamicResource Brush.Border0}">
        <TextBox Height="24"
                 Margin="4,0"
                 BorderThickness="1"
                 CornerRadius="12"
                 Text="{Binding SearchFilter, Mode=TwoWay}"
                 BorderBrush="{DynamicResource Brush.Border2}"
                 VerticalContentAlignment="Center">
          <TextBox.InnerLeftContent>
            <Path Width="14" Height="14"
                  Margin="6,0,0,0"
                  Fill="{DynamicResource Brush.FG2}"
                  Data="{StaticResource Icons.Search}"/>
          </TextBox.InnerLeftContent>

          <TextBox.InnerRightContent>
            <Button Classes="icon_button"
                    Width="16"
                    Margin="0,0,6,0"
                    Command="{Binding ClearSearchFilter}"
                    IsVisible="{Binding SearchFilter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
                    HorizontalAlignment="Right">
              <Path Width="14" Height="14"
                    Margin="0,1,0,0"
                    Fill="{DynamicResource Brush.FG1}"
                    Data="{StaticResource Icons.Clear}"/>
            </Button>
          </TextBox.InnerRightContent>
        </TextBox>
      </Border>

      <!-- Stash List -->
      <ListBox Grid.Row="2"
               Background="{DynamicResource Brush.Contents}"
               ItemsSource="{Binding VisibleStashes}"
               SelectedItem="{Binding SelectedStash, Mode=TwoWay}"
               SelectionMode="Single"
               KeyDown="OnStashListKeyDown"
               ScrollViewer.HorizontalScrollBarVisibility="Disabled"
               ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ListBox.Styles>
          <Style Selector="ListBoxItem">
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Height" Value="50"/>
          </Style>
        </ListBox.Styles>

        <ListBox.ItemsPanel>
          <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical"/>
          </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
          <DataTemplate DataType="m:Stash">
            <Border Background="Transparent"
                    BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="0,0,0,1"
                    Padding="4"
                    ContextRequested="OnStashContextRequested"
                    DoubleTapped="OnStashDoubleTapped"
                    ToolTip.Tip="{Binding Message}">
              <Grid RowDefinitions="Auto,*" >
                <Grid Grid.Row="0" ColumnDefinitions="*,Auto">
                  <TextBlock Grid.Column="0" Text="{Binding Name}" Foreground="DarkOrange"/>
                  <v:DateTimePresenter Grid.Column="1"
                                       Margin="8,0,0,0"
                                       Timestamp="{Binding Time}"
                                       Foreground="{DynamicResource Brush.FG2}"/>
                </Grid>

                <v:StashSubjectPresenter Grid.Row="1"
                                         Subject="{Binding Subject}"
                                         Foreground="{DynamicResource Brush.FG1}"
                                         FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize}"
                                         PrefixBackground="{DynamicResource Brush.InlineCode}"
                                         VerticalAlignment="Bottom"/>
              </Grid>
            </Border>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

      <!-- Changes Bar -->
      <Border Grid.Row="3" BorderThickness="0,1" BorderBrush="{DynamicResource Brush.Border0}">
        <Grid ColumnDefinitions="Auto,*,Auto">
          <Path Grid.Column="0" Margin="8,0,0,0" Width="14" Height="14" Fill="{DynamicResource Brush.FG2}" Data="{StaticResource Icons.Changes}"/>
          <TextBlock Grid.Column="1" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold" Margin="4,0,0,0">
            <Run Text="{DynamicResource Text.Stashes.Changes}"/>
            <Run Text="{Binding Changes, Converter={x:Static c:ListConverters.ToCount}, Mode=OneWay}"/>
          </TextBlock>

          <v:ChangeViewModeSwitcher Grid.Column="2"
                                    Width="14" Height="14"
                                    Margin="0,0,8,0"
                                    HorizontalAlignment="Right"
                                    ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=StashChangeViewMode, Mode=TwoWay}"/>
        </Grid>
      </Border>

      <!-- View Changes -->
      <Border Grid.Row="4" Background="{DynamicResource Brush.Contents}">
        <v:ChangeCollectionView ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=StashChangeViewMode, Mode=OneWay}"
                                EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
                                Changes="{Binding Changes}"
                                SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
                                ContextRequested="OnChangeContextRequested"
                                KeyDown="OnChangeCollectionViewKeyDown"/>
      </Border>
    </Grid>

    <GridSplitter Grid.Column="1"
                  MinWidth=".5"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                  Background="Transparent"
                  BorderThickness="1,0,0,0"
                  BorderBrush="{DynamicResource Brush.Border0}"
                  Focusable="False"/>

    <!-- Right -->
    <Grid Grid.Column="2" Margin="0,4,4,4">
      <Border BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
        <StackPanel Orientation="Vertical" VerticalAlignment="Center">
          <Path Width="64" Height="64" Data="{StaticResource Icons.Diff}" Fill="{DynamicResource Brush.FG2}"/>
          <TextBlock Margin="0,16,0,0"
                     Text="{DynamicResource Text.Diff.Welcome}"
                     FontSize="18" FontWeight="Bold"
                     Foreground="{DynamicResource Brush.FG2}"
                     HorizontalAlignment="Center"/>
        </StackPanel>
      </Border>

      <ContentControl Content="{Binding DiffContext}">
        <ContentControl.DataTemplates>
          <DataTemplate DataType="vm:DiffContext">
            <v:DiffView/>
          </DataTemplate>
        </ContentControl.DataTemplates>
      </ContentControl>
    </Grid>
  </Grid>
</UserControl>
