<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="500" d:DesignHeight="450"
             x:Class="SourceGit.Views.ExecuteCustomAction"
             x:DataType="vm:ExecuteCustomAction">
  <StackPanel Orientation="Vertical" Margin="8,0">
    <StackPanel Orientation="Horizontal">
      <Path Width="16" Height="16"
            Data="{StaticResource Icons.Action}"/>

      <TextBlock FontSize="18"
                 Margin="8,0,0,0"
                 Classes="bold"
                 Text="{Binding CustomAction.Name}"/>
    </StackPanel>

    <Grid Margin="0,16,0,0" Height="32" ColumnDefinitions="150,*">
      <TextBlock Grid.Column="0"
                 Text="{DynamicResource Text.ExecuteCustomAction.Target}"
                 HorizontalAlignment="Right" VerticalAlignment="Center"
                 Margin="0,0,8,0"/>

      <ContentControl Grid.Column="1" Content="{Binding Target}">
        <ContentControl.DataTemplates>
          <DataTemplate DataType="m:Null">
            <StackPanel Orientation="Horizontal">
              <Path Width="14" Height="14" Data="{StaticResource Icons.Repositories}"/>
              <TextBlock VerticalAlignment="Center" Text="{DynamicResource Text.ExecuteCustomAction.Repository}" Margin="8,0,0,0" IsTabStop="False"/>
            </StackPanel>
          </DataTemplate>

          <DataTemplate DataType="m:Branch">
            <StackPanel Orientation="Horizontal">
              <Path Width="14" Height="14" Data="{StaticResource Icons.Branch}"/>
              <TextBlock VerticalAlignment="Center" Text="{Binding FriendlyName}" Margin="8,0,0,0" IsTabStop="False"/>
            </StackPanel>
          </DataTemplate>

          <DataTemplate DataType="m:Commit">
            <Grid ColumnDefinitions="Auto,Auto,*">
              <Path Grid.Column="0" Width="14" Height="14" Data="{StaticResource Icons.Commit}"/>
              <TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0"/>
              <TextBlock Grid.Column="2" VerticalAlignment="Center" Text="{Binding Subject}" Margin="4,0,0,0" TextTrimming="CharacterEllipsis"/>
            </Grid>
          </DataTemplate>

          <DataTemplate DataType="m:Tag">
            <StackPanel Orientation="Horizontal">
              <Path Width="14" Height="14" Data="{StaticResource Icons.Tag}"/>
              <TextBlock VerticalAlignment="Center" Text="{Binding Name}" Margin="8,0,0,0"/>
            </StackPanel>
          </DataTemplate>

          <DataTemplate x:DataType="m:Remote">
            <StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
              <Path Width="14" Height="14" Data="{StaticResource Icons.Remote}"/>
              <TextBlock VerticalAlignment="Center" Text="{Binding Name}" Margin="8,0,0,0"/>
            </StackPanel>
          </DataTemplate>
        </ContentControl.DataTemplates>
      </ContentControl>
    </Grid>

    <ItemsControl IsVisible="{Binding ControlParameters, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"
                  ItemsSource="{Binding ControlParameters, Mode=OneWay}">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>

      <ItemsControl.DataTemplates>
        <DataTemplate DataType="vm:CustomActionControlTextBox">
          <Grid Height="32" ColumnDefinitions="150,*">
            <TextBlock Grid.Column="0"
                       Text="{Binding Label}"
                       HorizontalAlignment="Right" VerticalAlignment="Center"
                       Margin="0,0,8,0"/>
            <TextBox Grid.Column="1"
                     Height="28"
                     VerticalAlignment="Center"
                     CornerRadius="3"
                     Watermark="{Binding Placeholder, Mode=OneWay}"
                     Text="{Binding Text, Mode=TwoWay}"/>
          </Grid>
        </DataTemplate>

        <DataTemplate DataType="vm:CustomActionControlCheckBox">
          <Grid Height="32" ColumnDefinitions="150,*">
            <CheckBox Grid.Column="1"
                      Content="{Binding Label}"
                      ToolTip.Tip="{Binding ToolTip, Mode=OneWay}"
                      IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
          </Grid>
        </DataTemplate>

        <DataTemplate DataType="vm:CustomActionControlPathSelector">
          <Grid Height="32" ColumnDefinitions="150,*">
            <TextBlock Grid.Column="0"
                       Text="{Binding Label}"
                       HorizontalAlignment="Right" VerticalAlignment="Center"
                       Margin="0,0,8,0"/>
            <TextBox Grid.Column="1"
                     Height="28"
                     CornerRadius="3"
                     Watermark="{Binding Placeholder, Mode=OneWay}"
                     Text="{Binding Path, Mode=TwoWay}">
              <TextBox.InnerRightContent>
                <Button Classes="icon_button" Width="30" Height="30" Click="SelectPath">
                  <Path Data="{StaticResource Icons.Folder.Open}" Fill="{DynamicResource Brush.FG1}"/>
                </Button>
              </TextBox.InnerRightContent>
            </TextBox>
          </Grid>
        </DataTemplate>

        <DataTemplate DataType="vm:CustomActionControlComboBox">
          <Grid RowDefinitions="32,Auto" ColumnDefinitions="150,*">
            <TextBlock Grid.Row="0" Grid.Column="0"
                       Text="{Binding Label}"
                       HorizontalAlignment="Right" VerticalAlignment="Center"
                       Margin="0,0,8,0"/>
            <ComboBox Grid.Row="0" Grid.Column="1"
                      Height="28" Padding="8,0"
                      VerticalAlignment="Center" HorizontalAlignment="Stretch"
                      ItemsSource="{Binding Options}"
                      SelectedItem="{Binding Value, Mode=TwoWay}"/>

            <TextBlock Grid.Row="1" Grid.Column="1"
                       Classes="small"
                       Margin="0,2"
                       TextWrapping="Wrap"
                       Text="{Binding Description}"
                       Foreground="{DynamicResource Brush.FG2}"
                       IsVisible="{Binding Description, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
          </Grid>
        </DataTemplate>
      
        <DataTemplate DataType="vm:CustomActionControlBranchSelector">
          <Grid RowDefinitions="32,Auto" ColumnDefinitions="150,*">
            <TextBlock Grid.Row="0" Grid.Column="0"
                       Text="{Binding Label}"
                       HorizontalAlignment="Right" VerticalAlignment="Center"
                       Margin="0,0,8,0"/>
            <v:BranchSelector Grid.Row="0" Grid.Column="1"
                              Height="28" Padding="8,0"
                              VerticalAlignment="Center" HorizontalAlignment="Stretch"
                              Branches="{Binding Branches, Mode=OneWay}"
                              SelectedBranch="{Binding SelectedBranch, Mode=TwoWay}"/>

            <TextBlock Grid.Row="1" Grid.Column="1"
                       Classes="small"
                       Margin="0,2"
                       TextWrapping="Wrap"
                       Text="{Binding Description}"
                       Foreground="{DynamicResource Brush.FG2}"
                       IsVisible="{Binding Description, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
          </Grid>
        </DataTemplate>
      </ItemsControl.DataTemplates>
    </ItemsControl>
  </StackPanel>
</UserControl>
