<?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:converters="clr-namespace:App.Converters"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="App.Pages.ChampionsListPage"
             Title="Champions">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add" Command="{Binding NaviagteToChampionAddEditPageCommand}"/>
    </ContentPage.ToolbarItems>

    <ContentPage.Resources>
        <converters:Base64ToImage x:Key="Base64ToImageConv"/>

        <Style TargetType="Button">
            <Setter Property="WidthRequest">40</Setter>
            <Setter Property="HeightRequest">20</Setter>
        </Style>
    </ContentPage.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <ListView Grid.Row="0"
                  ItemsSource="{Binding ChampionManagerVM.ChampionVMs}"
                  SelectionMode="Single">
            <ListView.Behaviors>
                <toolkit:EventToCommandBehavior
                    EventName="ItemSelected"
                    Command="{Binding NavigateToChampionDetailCommand}"/>

            </ListView.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <SwipeView Grid.Row="0">
                            <SwipeView.RightItems>
                                <SwipeItems>
                                    <SwipeItem Text="Delete"
                                               BackgroundColor="{StaticResource DeleteColor}"/>
                                    <SwipeItem Text="Edit"
                                               BackgroundColor="{StaticResource EditColor}"/>
                                </SwipeItems>
                            </SwipeView.RightItems>

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

                                <Image Grid.Column="0" Source="{Binding Path=Icon, Converter={StaticResource Base64ToImageConv}}"/>

                                <StackLayout Grid.Column="1">
                                    <Label Text="{Binding Name}"/>
                                </StackLayout>
                            </Grid>

                        </SwipeView>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <StackLayout Grid.Row="1" Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="End">
            <Button Text="&lt;" Command="{Binding ChampionManagerVM.PreviousPageCommand}"/>
            <Label Text="{Binding ChampionManagerVM.Index}"/>
            <Label Text="/"/>
            <Label Text="{Binding ChampionManagerVM.Index}"/>
            <Button Text="&gt;"  Command="{Binding ChampionManagerVM.NextPageCommand}"/>
        </StackLayout>
    </Grid>
</ContentPage>
