<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:model="clr-namespace:ex_DataTemplateSelector.Model"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:local="clr-namespace:ex_DataTemplateSelector"
             x:Class="ex_DataTemplateSelector.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="Frame">
                <Setter Property="WidthRequest" Value="60"/>
                <Setter Property="HeightRequest" Value="60"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="IsClippedToBounds" Value="True"/>
                <Setter Property="BorderColor" Value="Transparent"/>
                <Setter Property="HasShadow" Value="False" />
            </Style>
            <Style TargetType="Label">
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="FontFamily" Value="SF Pro Text Regular"/>
            </Style>
            <Style TargetType="ImageButton">
                <Setter Property="MinimumWidthRequest" Value="14"/>
                <Setter Property="WidthRequest" Value="14"/>
                <Setter Property="MinimumHeightRequest" Value="14"/>
                <Setter Property="HeightRequest" Value="14"/>
                <Setter Property="VerticalOptions" Value="Center"/>
            </Style>
            <Style TargetType="Image">
                <Setter Property="HeightRequest" Value="60"/>
                <Setter Property="WidthRequest" Value="60"/>
                <Setter Property="Aspect" Value="AspectFill"/>
            </Style>
            <DataTemplate x:Key="albumDataTemplate" x:DataType="model:Album">
                <ViewCell>
                    <Grid ColumnDefinitions="Auto, *, Auto" RowDefinitions="*,*"
                          Margin="14, 8" ColumnSpacing="10">
                        <Frame Grid.RowSpan="2" CornerRadius="10">
                            <Image Source="{Binding Image}"/>
                        </Frame>
                        <Label Text="{Binding Title}" TextColor="Black"
                               Grid.Column="1" VerticalOptions="End"/>
                        <Label Text="{Binding Artist.FullName, StringFormat=Album &#x22c5; \{0\}}" TextColor="Gray"
                               Grid.Column="1" Grid.Row="1"
                               VerticalOptions="Start"/>
                        <ImageButton Source="chevron_right" 
                                     Grid.Column="2" Grid.RowSpan="2">
                            <ImageButton.Behaviors>
                                <toolkit:IconTintColorBehavior 
                                    TintColor="{StaticResource Gray400}"/>
                            </ImageButton.Behaviors>
                        </ImageButton>
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="playlistDataTemplate" x:DataType="model:Playlist">
                <ViewCell>
                    <Grid ColumnDefinitions="Auto, *, Auto" RowDefinitions="*,*"
                          Margin="14, 8" ColumnSpacing="10">
                        <Frame Grid.RowSpan="2" CornerRadius="10">
                            <Image Source="{Binding Image}"/>
                        </Frame>
                        <Label Text="{Binding Title}" TextColor="Black"
                               Grid.Column="1" VerticalOptions="End"/>
                        <Label Text="{Binding Owner, StringFormat=Playlist &#x22c5; \{0\}}" TextColor="Gray"
                               Grid.Column="1" Grid.Row="1"
                               VerticalOptions="Start"/>
                        <ImageButton Source="chevron_right" Grid.Column="2"
                                     Grid.RowSpan="2">
                            <ImageButton.Behaviors>
                                <toolkit:IconTintColorBehavior 
                                    TintColor="{StaticResource Gray400}"/>
                            </ImageButton.Behaviors>
                        </ImageButton>
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="trackDataTemplate" x:DataType="model:Track">
                <ViewCell>
                    <Grid ColumnDefinitions="Auto, *, Auto" RowDefinitions="*,*"
                          Margin="14, 8" ColumnSpacing="10">
                        <Frame Grid.RowSpan="2" CornerRadius="10">
                            <Image Source="{Binding Album.Image}"/>
                        </Frame>
                        <Label Text="{Binding Title}" TextColor="Black"
                               Grid.Column="1" VerticalOptions="End"/>
                        <Label Text="{Binding Album.Artist.FullName, StringFormat=Morceau &#x22c5; \{0\}}" TextColor="Gray"
                               Grid.Column="1" Grid.Row="1"
                               VerticalOptions="Start"/>
                        <ImageButton Source="ellipsis" Grid.Column="2"
                                    HorizontalOptions="Start" Margin="0, 0, 0, 4"
                                    Grid.RowSpan="2">
                            <ImageButton.Behaviors>
                                <toolkit:IconTintColorBehavior 
                                    TintColor="{StaticResource Black}"/>
                            </ImageButton.Behaviors>
                        </ImageButton>
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="artistDataTemplate" x:DataType="model:Artist">
                <ViewCell>
                    <Grid ColumnDefinitions="Auto, *, Auto" RowDefinitions="*,*"
                          Margin="14, 8" ColumnSpacing="10">
                        <Frame Grid.RowSpan="2" CornerRadius="30">
                            <Image Source="{Binding Picture}"/>
                        </Frame>
                        <Label Text="{Binding FullName}" TextColor="Black"
                               Grid.Column="1" VerticalOptions="End"/>
                        <Label Text="Artiste" TextColor="Gray"
                               Grid.Column="1" Grid.Row="1"
                               VerticalOptions="Start"/>
                        <ImageButton Source="chevron_right" Grid.Column="2"
                                     Grid.RowSpan="2">
                            <ImageButton.Behaviors>
                                <toolkit:IconTintColorBehavior 
                                    TintColor="{StaticResource Gray400}"/>
                            </ImageButton.Behaviors>
                        </ImageButton>
                    </Grid>
                </ViewCell>
            </DataTemplate>
            <local:ResultSelector AlbumTemplate="{StaticResource albumDataTemplate}"
                                  PlaylistTemplate="{StaticResource playlistDataTemplate}"
                                  TrackTemplate="{StaticResource trackDataTemplate}"
                                  ArtistTemplate="{StaticResource artistDataTemplate}"
                                  x:Key="resultSelector"/>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ListView ItemsSource="{Binding Results}"
              ItemTemplate="{StaticResource resultSelector}"
              RowHeight="76" BackgroundColor="White"/>

</ContentPage>
