<UserControl
  x:Class="NuGet.PackageManagement.UI.PackageItemControl"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  x:Name="_self"
  mc:Ignorable="d"
  xmlns:nuget="clr-namespace:NuGet.PackageManagement.UI"
  MinHeight="72"
  d:DesignHeight="90"
  d:DesignWidth="400"
  Style="{DynamicResource controlStyle}">
  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <nuget:SharedResources />
      </ResourceDictionary.MergedDictionaries>
      <nuget:PackageStatusToInstallImageVisibilityConverter
        x:Key="PackageStatusToInstallImageVisibilityConverter" />
      <nuget:PackageStatusToUpdateImageVisibilityConverter
        x:Key="PackageStatusToUpdateImageVisibilityConverter" />
      <nuget:BooleanToVisibilityConverter
        x:Key="BooleanToVisibilityConverter" />
      <nuget:SummaryToFontStyleConverter
        x:Key="SummaryToFontStyleConverter" />
      <nuget:SummaryToStringConverter
        x:Key="SummaryToStringConverter" />
      <nuget:StringFormatConverter
        x:Key="StringFormatConverter" />
      <nuget:NullToBooleanConverter
        x:Key="NullToBooleanConverter" />

      <!-- style for install, uninstall and update buttons -->
      <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
              <Border
                x:Name="border"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Background="{TemplateBinding Background}"
                SnapsToDevicePixels="True">
                <ContentPresenter
                  x:Name="contentPresenter"
                  ContentTemplate="{TemplateBinding ContentTemplate}"
                  Content="{TemplateBinding Content}"
                  ContentStringFormat="{TemplateBinding ContentStringFormat}"
                  Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                  Margin="{TemplateBinding Padding}"
                  RecognizesAccessKey="True"
                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
              </Border>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Style.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{DynamicResource {x:Static nuget:Brushes.ToolWindowButtonHoverActiveKey}}" />
            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static nuget:Brushes.ToolWindowButtonHoverActiveBorderKey}}" />
          </Trigger>
          <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" Value="{DynamicResource {x:Static nuget:Brushes.ToolWindowButtonDownKey}}" />
            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static nuget:Brushes.ToolWindowButtonDownBorderKey}}" />
          </Trigger>
        </Style.Triggers>
      </Style>

      <!-- style of this control itself -->
      <Style x:Key="controlStyle" TargetType="{x:Type UserControl}">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type UserControl}">
              <Border
                BorderThickness="0,0,0,0"
                BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}"
                Background="Transparent"
                Padding="0,8,0,0">
                <Grid
                  Margin="16,0,7,0">
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                  </Grid.ColumnDefinitions>

                  <!-- check box -->
                  <CheckBox
                    Grid.Column="0"
                    Margin="0,12,8,0"
                    VerticalAlignment="top"
                    Visibility="{Binding CheckBoxesEnabled, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}, Converter={StaticResource BooleanToVisibilityConverter}}"
                    IsChecked="{Binding Selected}" />

                  <!-- Icon -->
                  <Grid
                    Grid.Column="1"
                    VerticalAlignment="Top">
                    <Grid.RowDefinitions>
                      <RowDefinition Height="Auto" />
                      <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Image
                      Grid.Row="0"
                      Style="{StaticResource PackageIconImageStyle}"
                      VerticalAlignment="Top"
                      Margin="0,0,16,0" />
                    <nuget:InstalledIndicator
                      x:Name="_checkMark"
                      Grid.Row="0"
                      Margin="18,20,0,0"
                      Width="16"
                      Height="16"
                      VerticalAlignment="Top"
                      Visibility="{Binding Status, Converter={StaticResource PackageStatusToInstallImageVisibilityConverter}}"
                      ToolTip="{x:Static nuget:Resources.ToolTip_PackageInstalled}" />
                    <nuget:UpdateAvailableIndicator
                      x:Name="_updateMark"
                      Grid.Row="0"
                      Margin="18,20,0,0"
                      Width="16"
                      Height="16"
                      VerticalAlignment="Top"
                      Visibility="{Binding Status, Converter={StaticResource PackageStatusToUpdateImageVisibilityConverter}}"
                      ToolTip="{x:Static nuget:Resources.ToolTip_UpdateAvailable}" />

                    <TextBlock
                      Grid.Row="1"
                      Text="{x:Static nuget:Resources.Label_PackagePrerelease}"
                      FontSize="9"
                      Margin="0,3,0,0"
                      HorizontalAlignment="Left"
                      Visibility="{Binding Version.IsPrerelease, Converter={StaticResource BooleanToVisibilityConverter}}" />
                  </Grid>

                  <!-- title & summary-->
                  <Grid
                    Grid.Column="2"
                    VerticalAlignment="Top"
                    MinHeight="48">
                    <Grid.RowDefinitions>
                      <RowDefinition Height="Auto" />
                      <RowDefinition Height="36" MaxHeight="36" />
                    </Grid.RowDefinitions>
                    <StackPanel
                      Grid.Row="0"
                      Orientation="Horizontal">
                      <TextBlock
                        FontWeight="Bold"
                        FontSize="{Binding ElementName=_self,Path=FontSize,Converter={StaticResource Font122PercentSizeConverter}}"
                        TextTrimming="CharacterEllipsis"
                        TextWrapping="NoWrap"
                        AutomationProperties.AutomationId="id"
                        VerticalAlignment="Top"
                        Text="{Binding Id}" />
                      <!-- author and download count -->
                      <nuget:AuthorAndDownloadCount
                        Margin="5,4,0,0"
                        Author="{Binding Author}"
                        DownloadCount="{Binding DownloadCount}" />
                    </StackPanel>

                    <Grid
                      Grid.Row="2">
                      <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="4" />
                        <RowDefinition Height="*" />
                      </Grid.RowDefinitions>
                      <!-- providers -->
                      <nuget:PackageManagerProvidersLabel
                        Grid.Row="0"
                        FormatString="{x:Static nuget:Resources.Label_UseInstead}"
                        DataContext="{Binding Providers}" />

                      <!-- Summary -->
                      <TextBlock
                        Grid.Row="2"
                        Text="{Binding Summary, Converter={StaticResource SummaryToStringConverter}}"
                        FontStyle="{Binding Summary, Converter={StaticResource SummaryToFontStyleConverter}}"
                        TextTrimming="CharacterEllipsis"
                        TextWrapping="Wrap" />
                    </Grid>

                    <Grid.ToolTip>
                      <TextBlock
                        Style="{StaticResource TooltipStyle}">
                        <Run
                          Text="{Binding Id}"
                          FontWeight="Bold" />
                        <LineBreak />
                        <Run
                          Text="{Binding Summary}" />
                      </TextBlock>
                    </Grid.ToolTip>
                  </Grid>

                  <Grid
                    Grid.Column="3"
                    VerticalAlignment="Top"
                    Margin="10,0,0,0">
                    <Grid.RowDefinitions>
                      <RowDefinition />
                      <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="auto" />
                      <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>

                    <!-- row 0: -->
                    <Grid
                      Grid.Row="0"
                      Grid.Column="0"
                      HorizontalAlignment="Right">
                      <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="auto" />
                      </Grid.ColumnDefinitions>

                      <!-- installed indicator -->
                      <nuget:InstalledIndicator
                        x:Name="_installedIndicator"
                        Grid.Column="0"
                        Width="12"
                        Height="12"
                        IsGrayed="True"
                        Visibility="Visible"
                        VerticalAlignment="Center" />

                      <!-- installed version -->
                      <TextBlock
                        x:Name="_installedVersion"
                        Grid.Column="1"
                        Margin="4,0,4,0"
                        VerticalAlignment="Center"
                        Visibility="Hidden"
                        Text="{Binding InstalledVersion, Converter={StaticResource VersionToStringConverter},StringFormat={}v{0}}">
                        <TextBlock.ToolTip>
                          <MultiBinding Converter="{StaticResource StringFormatConverter}">
                            <Binding Source="{x:Static nuget:Resources.ToolTip_InstalledVersion}" />
                            <Binding Path="InstalledVersion" Converter="{StaticResource VersionToStringConverter}" />
                          </MultiBinding>
                        </TextBlock.ToolTip>
                      </TextBlock>
                    </Grid>

                    <!-- version to install. It occupies the same position as the installed version -->
                    <TextBlock
                      x:Name="_versionToInstall"
                      Grid.Row="0"
                      Grid.Column="0"
                      Margin="2,0,4,0"
                      VerticalAlignment="Center"
                      TextAlignment="Right"
                      Visibility="Hidden"
                      Text="{Binding LatestVersion, Converter={StaticResource VersionToStringConverter},StringFormat={}v{0}}">
                      <TextBlock.ToolTip>
                        <MultiBinding Converter="{StaticResource StringFormatConverter}">
                          <Binding Source="{x:Static nuget:Resources.ToolTip_LatestVersion}" />
                          <Binding Path="LatestVersion" Converter="{StaticResource VersionToStringConverter}" />
                        </MultiBinding>
                      </TextBlock.ToolTip>
                    </TextBlock>

                    <!-- uninstall button -->
                    <Button
                      x:Name="_uninstallButton"
                      Style="{StaticResource buttonStyle}"
                      Grid.Row="0"
                      Grid.Column="1"
                      Command="{x:Static nuget:Commands.UninstallPackageCommand}"
                      CommandParameter="{Binding}"
                      Visibility="Hidden"
                      ToolTip="{x:Static nuget:Resources.ToolTip_UninstallButton}">
                      <nuget:UninstallButton
                        Width="16"
                        Height="16" />
                    </Button>

                    <!-- install button. It occupies the same position of the install button -->
                    <Button
                      x:Name="_installButton"
                      Grid.Row="0"
                      Grid.Column="1"
                      Style="{StaticResource buttonStyle}"
                      Command="{x:Static nuget:Commands.InstallPackageCommand}"
                      CommandParameter="{Binding}"
                      Visibility="Hidden">
                      <Button.ToolTip>
                        <MultiBinding Converter="{StaticResource StringFormatConverter}">
                          <Binding Source="{x:Static nuget:Resources.ToolTip_InstallButton}" />
                          <Binding Path="Id" />
                          <Binding Path="LatestVersion" Converter="{StaticResource VersionToStringConverter}" />
                        </MultiBinding>
                      </Button.ToolTip>
                      <nuget:InstallButton
                        Width="16"
                        Height="16" />
                    </Button>

                    <!-- row 1: latest version and the update button-->
                    <!-- version to update to -->
                    <TextBlock
                      x:Name="_versionToUpdateTo"
                      Grid.Row="1"
                      Grid.Column="0"
                      Margin="2,5,4,0"
                      VerticalAlignment="Center"
                      TextAlignment="Right"
                      Visibility="Hidden"
                      Text="{Binding LatestVersion, Converter={StaticResource VersionToStringConverter},StringFormat={}v{0}}">
                      <TextBlock.ToolTip>
                        <MultiBinding Converter="{StaticResource StringFormatConverter}">
                          <Binding Source="{x:Static nuget:Resources.ToolTip_LatestVersion}" />
                          <Binding Path="LatestVersion" Converter="{StaticResource VersionToStringConverter}" />
                        </MultiBinding>
                      </TextBlock.ToolTip>
                    </TextBlock>

                    <!-- update button -->
                    <Button
                      x:Name="_updateButton"
                      Grid.Row="1"
                      Grid.Column="2"
                      Style="{StaticResource buttonStyle}"
                      Margin="0,5,0,0"
                      Command="{x:Static nuget:Commands.InstallPackageCommand}"
                      CommandParameter="{Binding}"
                      Visibility="Hidden">
                      <Button.ToolTip>
                        <MultiBinding Converter="{StaticResource StringFormatConverter}">
                          <Binding Source="{x:Static nuget:Resources.ToolTip_UpdateButton}" />
                          <Binding Path="Id" />
                          <Binding Path="LatestVersion" Converter="{StaticResource VersionToStringConverter}" />
                        </MultiBinding>
                      </Button.ToolTip>
                      <nuget:UpdateButton
                        Width="16"
                        Height="16" />
                    </Button>
                  </Grid>
                </Grid>
              </Border>

              <!-- triggers -->
              <ControlTemplate.Triggers>
                <!-- The install button is visible when
                     * IsSolution is false;
                     * Status is NotInstalled;
                     * mouse hovers over the control;
                -->
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition
                      Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}"
                      Value="true" />
                    <Condition
                      Binding="{Binding Status}"
                      Value="NotInstalled" />
                    <Condition
                      Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                      Value="False" />
                  </MultiDataTrigger.Conditions>
                  <Setter TargetName="_installButton" Property="Visibility" Value="Visible" />
                </MultiDataTrigger>

                <!-- The install button is collapsed when IsSolution is true -->
                <DataTrigger
                  Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                  Value="True">
                  <Setter TargetName="_installButton" Property="Visibility" Value="Collapsed" />
                </DataTrigger>

                <!-- installed version is visible when
                     * IsSolution is false;
                     * InstalledVersion is not null;
                -->
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition
                      Binding="{Binding InstalledVersion,Converter={StaticResource NullToBooleanConverter}}"
                      Value="False" />
                    <Condition
                      Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                      Value="False" />
                  </MultiDataTrigger.Conditions>
                  <Setter TargetName="_installedVersion" Property="Visibility" Value="Visible" />
                </MultiDataTrigger>

                <!-- version to install is visible when
                     * Status is NotInstalled
                     * Or IsSolution is true
                -->
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition
                      Binding="{Binding Status}"
                      Value="NotInstalled" />
                    <Condition
                      Binding="{Binding LatestVersion,Converter={StaticResource NullToBooleanConverter}}"
                      Value="False" />
                  </MultiDataTrigger.Conditions>
                  <Setter
                    TargetName="_versionToInstall"
                    Property="Visibility" Value="Visible" />
                </MultiDataTrigger>

                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition
                      Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                      Value="True">
                    </Condition>
                    <Condition
                      Binding="{Binding LatestVersion,Converter={StaticResource NullToBooleanConverter}}"
                      Value="False" />
                  </MultiDataTrigger.Conditions>
                  <Setter
                    TargetName="_versionToInstall"
                    Property="Visibility" Value="Visible" />
                </MultiDataTrigger>

                <!-- version to update to is visible when
                     * Status is UpdateAvailable;
                     * IsSolution is false
                -->
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition
                      Binding="{Binding Status}"
                      Value="UpdateAvailable" />
                    <Condition
                      Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                      Value="False" />
                  </MultiDataTrigger.Conditions>
                  <Setter
                    TargetName="_versionToUpdateTo"
                    Property="Visibility" Value="Visible" />
                </MultiDataTrigger>

                <!-- The uninstall button is visible when
                     * IsSolution is false;
                     * Status is Installed or UpdateAvailable;
                     * mouse hovers over the control;
                     Since there is no OR condition in triggers, we need to use two triggers instead.
                -->
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition
                      Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}"
                      Value="true" />
                    <Condition
                      Binding="{Binding Status}"
                      Value="Installed" />
                    <Condition
                      Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                      Value="False" />
                  </MultiDataTrigger.Conditions>
                  <Setter TargetName="_uninstallButton" Property="Visibility" Value="Visible" />
                </MultiDataTrigger>
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition
                      Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}"
                      Value="true" />
                    <Condition
                      Binding="{Binding Status}"
                      Value="UpdateAvailable" />
                    <Condition
                      Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                      Value="False" />
                  </MultiDataTrigger.Conditions>
                  <Setter TargetName="_uninstallButton" Property="Visibility" Value="Visible" />
                </MultiDataTrigger>

                <!-- The uninstall button is collapsed when IsSolution is true -->
                <DataTrigger
                  Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                  Value="True">
                  <Setter TargetName="_uninstallButton" Property="Visibility" Value="Collapsed" />
                </DataTrigger>

                <!-- The update button is visible when
                     * IsSolution is false;
                     * Status is UpdateAvailable;
                     * mouse hovers over the control;
                -->
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition
                      Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}"
                      Value="true" />
                    <Condition
                      Binding="{Binding Status}"
                      Value="UpdateAvailable" />
                    <Condition
                      Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                      Value="False" />
                  </MultiDataTrigger.Conditions>
                  <Setter TargetName="_updateButton" Property="Visibility" Value="Visible" />
                </MultiDataTrigger>

                <!-- The update button is collapsed when IsSolution is true -->
                <DataTrigger
                  Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}"
                  Value="True">
                  <Setter TargetName="_updateButton" Property="Visibility" Value="Collapsed" />
                </DataTrigger>

                <!-- The installed version indicator becomes invisible when
                   * IsSolution is true; or
                   * InstalledVersion is null;
                -->
                <DataTrigger
                  Binding="{Binding IsSolution, RelativeSource={RelativeSource AncestorType={x:Type nuget:InfiniteScrollList}}}" Value="true">
                  <Setter TargetName="_installedIndicator" Property="Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger
                  Binding="{Binding InstalledVersion}" Value="{x:Null}">
                  <Setter TargetName="_installedIndicator" Property="Visibility" Value="Collapsed" />
                </DataTrigger>
              </ControlTemplate.Triggers>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </ResourceDictionary>
  </UserControl.Resources>
</UserControl>