<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:tabs="clr-namespace:Artemis.UI.Screens.Workshop.Library.Tabs"
             xmlns:il="clr-namespace:AsyncImageLoader;assembly=AsyncImageLoader.Avalonia"
             xmlns:converters="clr-namespace:Artemis.UI.Converters"
             xmlns:avalonia="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="Artemis.UI.Screens.Workshop.Library.Tabs.InstalledTabItemView"
             x:DataType="tabs:InstalledTabItemViewModel">
    <UserControl.Resources>
        <converters:EntryIconUriConverter x:Key="EntryIconUriConverter" />
        <converters:DateTimeConverter x:Key="DateTimeConverter" />
    </UserControl.Resources>
    <Button MinHeight="110"
            MaxHeight="140"
            Padding="12"
            HorizontalAlignment="Stretch"
            HorizontalContentAlignment="Stretch"
            Command="{CompiledBinding ViewWorkshopPage}">
        <Grid ColumnDefinitions="Auto,*,Auto,Auto" RowDefinitions="*, Auto">
            <!-- Icon -->
            <Border Grid.Column="0"
                    Grid.Row="0"
                    Grid.RowSpan="2"
                    CornerRadius="6"
                    VerticalAlignment="Center"
                    Margin="0 0 10 0"
                    Width="80"
                    Height="80"
                    ClipToBounds="True">
                <Image Stretch="UniformToFill" il:ImageLoader.Source="{CompiledBinding Entry.Id, Converter={StaticResource EntryIconUriConverter}, Mode=OneWay}" />
            </Border>

            <!-- Body -->
            <Grid Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" VerticalAlignment="Stretch" RowDefinitions="Auto,*,Auto">
                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <TextBlock Margin="0 0 0 5" TextTrimming="CharacterEllipsis">
                        <Run Classes="h5" Text="{CompiledBinding Entry.Name, FallbackValue=Title}" />
                        <Run Classes="subtitle">by</Run>
                        <Run Classes="subtitle" Text="{CompiledBinding Entry.Author, FallbackValue=Author}" />
                    </TextBlock>
                    <avalonia:MaterialIcon
                        IsVisible="{CompiledBinding Entry.IsOfficial}"
                        Kind="ShieldStar"
                        Foreground="{DynamicResource SystemAccentColorLight1}"
                        Margin="2 -2 0 0"
                        Width="18"
                        Height="18"
                        HorizontalAlignment="Left"
                        ToolTip.Tip="Official entry by the Artemis team" />
                </StackPanel>
                <TextBlock Grid.Row="1"
                           Classes="subtitle"
                           TextWrapping="Wrap"
                           TextTrimming="CharacterEllipsis"
                           Text="{CompiledBinding Entry.Summary, FallbackValue=Summary}">
                </TextBlock>

                <ItemsControl Grid.Row="2" ItemsSource="{CompiledBinding Entry.Categories}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" Spacing="8"></StackPanel>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <avalonia:MaterialIcon Kind="{CompiledBinding Icon}" Margin="0 0 3 0"></avalonia:MaterialIcon>
                                <TextBlock Text="{CompiledBinding Name}" TextTrimming="CharacterEllipsis" />
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>

            <!-- Info -->
            <StackPanel Grid.Column="2" Grid.Row="0" Margin="0 0 4 0" HorizontalAlignment="Right">
                <TextBlock TextAlignment="Right">
                    <avalonia:MaterialIcon Kind="Harddisk" />
                    <Run Text="{CompiledBinding Entry.ReleaseVersion}" />
                </TextBlock>
            </StackPanel>

            <!-- Install state -->
            <StackPanel Grid.Column="2" Grid.Row="1" Margin="0 0 4 0" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                <TextBlock TextAlignment="Right" IsVisible="{CompiledBinding UpdateAvailable}">
                    <avalonia:MaterialIcon Kind="Update" Foreground="{DynamicResource SystemAccentColorLight1}" Width="20" Height="20" />
                    <Run>update available</Run>
                </TextBlock>
            </StackPanel>

            <!-- Management -->
            <Border Grid.Column="3" Grid.Row="0" Grid.RowSpan="2" BorderBrush="{DynamicResource ButtonBorderBrush}" BorderThickness="1 0 0 0" Margin="10 0 0 0" Padding="10 0 0 0">
                <StackPanel VerticalAlignment="Center">
                    <StackPanel Orientation="Horizontal" Spacing="5">
                        <Button Command="{CompiledBinding ViewLocal}" HorizontalAlignment="Stretch" >Open</Button>
                        <Button Command="{CompiledBinding Uninstall}" HorizontalAlignment="Stretch">Uninstall</Button>
                    </StackPanel>
                    <CheckBox MinHeight="26" Margin="0 4 0 0" IsChecked="{CompiledBinding AutoUpdate}">Auto-update</CheckBox>
                </StackPanel>
            </Border>
        </Grid>
    </Button>
</UserControl>