<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="OwlCore.Wpf.AbstractUI.Themes.AbstractDataListStyle"
    xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:abstractUI="clr-namespace:OwlCore.AbstractUI.ViewModels;assembly=OwlCore"
    xmlns:convertvis="clr-namespace:OwlCore.Wpf.Converters.Bools.Visible"
    xmlns:themes="clr-namespace:OwlCore.Wpf.AbstractUI.Themes"
    xmlns:controls="clr-namespace:OwlCore.Wpf.AbstractUI.Controls"
    xmlns:converters="clr-namespace:OwlCore.Wpf.Converters">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/OwlCore.Wpf;component/AbstractUI/Themes/AbstractUIResources.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <convertvis:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
    <convertvis:InverseBoolToVisibilityConverter x:Key="InverseBoolToVisibilityConverter"/>
    <convertvis:NotNullOrEmptyToVisibilityConverter x:Key="NotNullOrEmptyToVisibilityConverter"/>
    <converters:SelectionChangedEventArgsToClickedItemConverter x:Key="SelectionChangedEventArgsToClickedItemConverter" />
    
    <DataTemplate x:Key="DefaultAbstractDataListListTemplate" DataType="abstractUI:AbstractDataListViewModel">
        <ListView x:Name="PrimaryListView" ItemsSource="{Binding Items}" SelectionMode="Single" Margin="10">
            <behaviors:Interaction.Triggers>
                <behaviors:EventTrigger EventName="SelectionChanged">
                    <behaviors:InvokeCommandAction Command="{Binding ItemTappedCommand}" EventArgsConverter="{StaticResource SelectionChangedEventArgsToClickedItemConverter}" />
                </behaviors:EventTrigger>
            </behaviors:Interaction.Triggers>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">
                                <ContentPresenter />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate DataType="abstractUI:AbstractDataListItemViewModel">
                    <Grid ToolTipService.ToolTip="{Binding TooltipText, Mode=OneWay}" Height="60">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <Grid Visibility="{Binding Parent.IsUserEditingEnabled, Converter={StaticResource BoolToVisibilityConverter}}">
                            <Button Content="&#xE8BB;" FontFamily="Segoe MDL2 Assets" FontSize="16" 
                                    Height="40" Width="40" Margin="0,0,5,0" Background="Transparent"
                                    Visibility="{Binding IsAddItem, Converter={StaticResource InverseBoolToVisibilityConverter}}">
                                <behaviors:Interaction.Triggers>
                                    <behaviors:EventTrigger EventName="Click">
                                        <behaviors:InvokeCommandAction Command="{Binding RequestRemoveCommand}" CommandParameter="{Binding}" />
                                    </behaviors:EventTrigger>
                                </behaviors:Interaction.Triggers>
                            </Button>

                            <Button Grid.RowSpan="2" Content="&#xE710;" FontFamily="Segoe MDL2 Assets" FontSize="18" 
                                    Height="40" Width="40" Margin="0,0,5,0" Background="Transparent"
                                    Visibility="{Binding IsAddItem, Converter={StaticResource BoolToVisibilityConverter}}">
                                <behaviors:Interaction.Triggers>
                                    <behaviors:EventTrigger EventName="Click">
                                        <behaviors:InvokeCommandAction Command="{Binding RequestAddCommand}"  CommandParameter="{Binding}" />
                                    </behaviors:EventTrigger>
                                </behaviors:Interaction.Triggers>
                            </Button>
                        </Grid>

                        <Border Grid.Column="1" Grid.RowSpan="2" Margin="0,0,5,0" Visibility="{Binding Path=IconCode, TargetNullValue=Collapsed}"
                                    Height="65" Width="60">
                            <TextBlock Text="{Binding IconCode, Mode=OneWay}" FontFamily="Segoe MDL2 Assets" FontSize="42" />
                        </Border>

                        <Image Grid.Column="1" Grid.RowSpan="2" Margin="0,0,5,0" Visibility="{Binding ImageSourceIsValid, TargetNullValue=Collapsed}"
                                   x:Name="Thumbnail"
                                   MaxHeight="60" MaxWidth="60"
                                   Source="{Binding ImageSource, Mode=OneWay}" />

                        <TextBlock Grid.Column="2" Text="{Binding Title, Mode=OneWay}" FontSize="20" VerticalAlignment="Center"
                                   Visibility="{Binding Path=Title, TargetNullValue=Collapsed, Mode=OneWay}"/>

                        <TextBlock x:Name="SubtitleTextBlock" Grid.Column="2" Grid.Row="1"
                                       VerticalAlignment="Center"
                                       Visibility="{Binding Path=Subtitle, TargetNullValue=Collapsed, Mode=OneWay}"
                                       Text="{Binding Subtitle, Mode=OneWay}"
                                       FontSize="16" FontWeight="Light" Opacity="0.75" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </DataTemplate>

    <DataTemplate x:Key="DefaultAbstractDataListGridTemplate" DataType="abstractUI:AbstractDataListViewModel">
        <ListView SelectionMode="Single" ItemsSource="{Binding Items}">
            <behaviors:Interaction.Triggers>
                <behaviors:EventTrigger EventName="SelectionChanged">
                    <behaviors:InvokeCommandAction Command="{Binding ItemTappedCommand}" EventArgsConverter="{StaticResource SelectionChangedEventArgsToClickedItemConverter}" />
                </behaviors:EventTrigger>
            </behaviors:Interaction.Triggers>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">
                                <Grid x:Name="ContentBorder">
                                    <ContentPresenter x:Name="ContentPresenter" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate DataType="abstractUI:AbstractDataListItemViewModel">
                    <Grid ToolTipService.ToolTip="{Binding TooltipText, Mode=OneWay}" Width="150" Margin="15">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <Border BorderBrush="White" BorderThickness="1">
                            <Grid Height="150" Width="150" Grid.ColumnSpan="2">
                                <TextBlock Text="{Binding IconCode}" FontFamily="Segoe MDL2 Assets" FontSize="34"
                                           VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                <TextBlock Visibility="{Binding IsAddItem, Converter={StaticResource BoolToVisibilityConverter}}" Text="&#xE710;" FontFamily="Segoe MDL2 Assets" FontSize="34" />
                                <Image Source="{Binding ImageSource, Mode=OneWay}"/>
                            </Grid>
                        </Border>

                        <TextBlock Text="{Binding Title, Mode=OneWay}" FontSize="16" Grid.Row="1" TextTrimming="CharacterEllipsis" />

                        <Border Visibility="{Binding Parent.IsUserEditingEnabled, Converter={StaticResource BoolToVisibilityConverter}}">
                            <Button Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" HorizontalAlignment="Right" VerticalAlignment="Top"
                                    Content="&#xE8BB;" FontFamily="Segoe MDL2 Assets" FontSize="14" 
                                    Margin="5,-1,0,0" Background="Transparent"
                                    Visibility="{Binding IsAddItem, Converter={StaticResource InverseBoolToVisibilityConverter}}">
                                <behaviors:Interaction.Triggers>
                                    <behaviors:EventTrigger EventName="Click">
                                        <behaviors:InvokeCommandAction Command="{Binding RequestRemoveCommand}" CommandParameter="{Binding}" />
                                    </behaviors:EventTrigger>
                                </behaviors:Interaction.Triggers>
                            </Button>
                        </Border>

                        <TextBlock x:Name="SubtitleTextBlock" Grid.Row="2"  Grid.ColumnSpan="2"
                                       Text="{Binding Subtitle}"
                                       FontSize="12" FontWeight="Light" Opacity="0.5" TextTrimming="CharacterEllipsis" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </DataTemplate>

    <DataTemplate x:Key="DefaultAbstractDataListTemplate" DataType="abstractUI:AbstractDataListViewModel">
        <StackPanel>
            <TextBlock Text="{Binding Title, Mode=OneWay}" FontSize="{StaticResource DefaultAbstractUITitleFontSize}" ToolTipService.ToolTip="{Binding TooltipText, Mode=OneWay}" />

            <TextBlock Text="{Binding Subtitle, Mode=OneWay}" FontSize="{StaticResource DefaultAbstractUISubtitleFontSize}"
                       FontWeight="{StaticResource DefaultAbstractUISubtitleFontWeight}"
                       Opacity="{StaticResource DefaultAbstractUISubtitleOpacity}"
                       Visibility="{Binding Path=Subtitle, TargetNullValue=Collapsed, Mode=OneWay, Converter={StaticResource NotNullOrEmptyToVisibilityConverter}}"/>

            <ContentControl Content="{Binding}">
                <ContentControl.ContentTemplateSelector>
                    <themes:AbstractDataListTypeTemplateSelector ListTemplate="{StaticResource DefaultAbstractDataListListTemplate}"
                                                                 GridTemplate="{StaticResource DefaultAbstractDataListGridTemplate}"/>
                </ContentControl.ContentTemplateSelector>
            </ContentControl>
        </StackPanel>
    </DataTemplate>

    <Style TargetType="controls:AbstractDataListPresenter">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="controls:AbstractDataListPresenter">
                        <ContentControl ContentTemplate="{StaticResource DefaultAbstractDataListTemplate}"
                                        Content="{Binding}"
                                        HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>

</ResourceDictionary>
