<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="SeriesTracker.Views.ActiveSeriesPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:control="clr-namespace:SeriesTracker.Controls"
    xmlns:models="clr-namespace:SeriesTracker.Models"
    xmlns:service="clr-namespace:SeriesTracker.Services.Converters"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    xmlns:vModels="clr-namespace:SeriesTracker.ViewModels"
    Title=""
    BackgroundColor="{AppThemeBinding Light={StaticResource White},
                                      Dark={StaticResource DarkBackground}}"
    Shell.FlyoutBehavior="Disabled">

    <Shell.BackButtonBehavior>
        <BackButtonBehavior IsEnabled="False" IsVisible="False" />
    </Shell.BackButtonBehavior>

    <toolkit:DockLayout>
        <StackLayout
            Margin="10"
            Padding="5"
            toolkit:DockLayout.DockPosition="Top">
            <Border BackgroundColor="Transparent" Stroke="Transparent">
                <Grid HorizontalOptions="Center">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="25" />
                        <RowDefinition Height="45" />
                        <RowDefinition Height="1" />
                        <RowDefinition Height="5" />
                        <RowDefinition Height="15" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="35" />
                        <ColumnDefinition Width="230" />
                        <ColumnDefinition Width="35" />
                        <ColumnDefinition Width="35" />
                    </Grid.ColumnDefinitions>
                    <Image
                        x:Name="searchImage"
                        Grid.Row="1"
                        Grid.Column="0"
                        Margin="0,0,10,0"
                        HeightRequest="25"
                        Source="search.png"
                        WidthRequest="25">
                        <Image.Behaviors>
                            <toolkit:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Gray600}, Dark={StaticResource Gray300}}" />
                        </Image.Behaviors>
                    </Image>
                    <Entry
                        x:Name="searchBar"
                        Grid.Row="1"
                        Grid.Column="1"
                        Completed="SearchBar_Completed"
                        Focused="SearchBar_Focused"
                        FontSize="16"
                        Keyboard="Text"
                        Placeholder="Найти сериал"
                        PlaceholderColor="{AppThemeBinding Light={StaticResource Gray600},
                                                           Dark={StaticResource Gray300}}"
                        ReturnCommand="{Binding FilterSeriesCommand}"
                        ReturnCommandParameter="{Binding Source={x:Reference searchBar}, Path=Text}"
                        TextChanged="SearchBar_TextChanged"
                        Unfocused="SearchBar_Unfocused"
                        VerticalTextAlignment="Center" />
                    <ImageButton
                        x:Name="searchbarClearBtn"
                        Grid.Row="1"
                        Grid.Column="2"
                        Clicked="SearchBarClearBtn_Clicked"
                        HeightRequest="20"
                        IsVisible="false"
                        Source="cross.png"
                        WidthRequest="20">
                        <ImageButton.Behaviors>
                            <toolkit:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Gray600}, Dark={StaticResource Gray300}}" />
                        </ImageButton.Behaviors>
                    </ImageButton>
                    <ImageButton
                        x:Name="filterExpandImage"
                        Grid.Row="1"
                        Grid.Column="3"
                        Clicked="FilterExpandImage_Clicked"
                        HeightRequest="25"
                        Source="category.png"
                        WidthRequest="25">
                        <ImageButton.Behaviors>
                            <toolkit:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Gray600}, Dark={StaticResource Gray300}}" />
                        </ImageButton.Behaviors>
                    </ImageButton>
                    <Line
                        x:Name="myLine"
                        Grid.Row="2"
                        Grid.Column="0"
                        Grid.ColumnSpan="4"
                        BackgroundColor="{StaticResource Gray600}"
                        HorizontalOptions="Fill" />

                    <ImageButton
                        Grid.Row="4"
                        Grid.Column="0"
                        Command="{Binding DecSeriesListCommand}"
                        HeightRequest="15"
                        HorizontalOptions="Center"
                        Source="backarrow.png"
                        VerticalOptions="Start"
                        WidthRequest="15">
                        <ImageButton.Behaviors>
                            <toolkit:IconTintColorBehavior TintColor="#999999" />
                        </ImageButton.Behaviors>
                    </ImageButton>
                    <Label
                        Grid.Row="4"
                        Grid.Column="1"
                        Grid.ColumnSpan="2"
                        FontFamily="NunitoItalic"
                        FontSize="12"
                        HorizontalOptions="Start"
                        TextColor="{AppThemeBinding Light={StaticResource Gray600},
                                                    Dark={StaticResource Gray300}}"
                        VerticalTextAlignment="Center">
                        <Label.Text>
                            <MultiBinding StringFormat="Показано: {0} из {1} | Всего: {2}">
                                <Binding Path="ViewedSeriesCount" />
                                <Binding Path="SeriesCount" />
                                <Binding Path="AllSeriesCount" />
                            </MultiBinding>
                        </Label.Text>
                    </Label>
                    <ImageButton
                        Grid.Row="4"
                        Grid.Column="3"
                        Command="{Binding IncSeriesListCommand}"
                        HeightRequest="15"
                        HorizontalOptions="Center"
                        Rotation="180"
                        Source="backarrow.png"
                        VerticalOptions="Start"
                        WidthRequest="15">
                        <ImageButton.Behaviors>
                            <toolkit:IconTintColorBehavior TintColor="#999999" />
                        </ImageButton.Behaviors>
                    </ImageButton>
                </Grid>

            </Border>

            <toolkit:Expander x:Name="filterExpander" IsExpanded="False">
                <Grid
                    Margin="10,10,10,-10"
                    Padding="5"
                    HorizontalOptions="Center">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="35" />
                        <ColumnDefinition Width="265" />
                        <ColumnDefinition Width="35" />
                    </Grid.ColumnDefinitions>
                    <Label
                        Grid.Row="0"
                        Grid.Column="1"
                        FontSize="15"
                        HorizontalOptions="Start"
                        Text="Только избранное"
                        TextColor="{AppThemeBinding Light={StaticResource Gray600},
                                                    Dark={StaticResource Gray300}}"
                        VerticalTextAlignment="Center" />
                    <CheckBox
                        Grid.Row="0"
                        Grid.Column="2"
                        CheckedChanged="CheckBox_CheckedChanged"
                        HorizontalOptions="Center"
                        VerticalOptions="Center" />
                </Grid>

            </toolkit:Expander>
        </StackLayout>

        <CollectionView
            x:Name="seriesCollection"
            toolkit:DockLayout.DockPosition="None"
            HorizontalScrollBarVisibility="Never"
            ItemsSource="{Binding SeriesList}"
            VerticalScrollBarVisibility="Never">
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="models:Series">
                    <Border Margin="10" BackgroundColor="{AppThemeBinding Light={StaticResource ActivePageLightBorderBackground}, Dark={StaticResource ActivePageDarkBorderBackground}}">
                        <Border.StrokeShape>
                            <RoundRectangle CornerRadius="10" />
                        </Border.StrokeShape>
                        <Grid Margin="5" Padding="5">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="25" />
                                <RowDefinition Height="1" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="130" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="20" />
                            </Grid.ColumnDefinitions>

                            <Border
                                Grid.Row="0"
                                Grid.RowSpan="5"
                                Grid.Column="0"
                                Margin="0,0,10,0"
                                BackgroundColor="Transparent"
                                HorizontalOptions="Fill"
                                Stroke="{AppThemeBinding Light={StaticResource ActivePageLightStroke},
                                                         Dark={StaticResource ActivePageDarkStroke}}"
                                VerticalOptions="Fill">
                                <Border.StrokeShape>
                                    <RoundRectangle CornerRadius="10" />
                                </Border.StrokeShape>
                                <Image Aspect="Fill" Source="{Binding imagePath, TargetNullValue='noimage.png'}" />

                            </Border>
                            <Label
                                Grid.Row="0"
                                Grid.Column="1"
                                Grid.ColumnSpan="3"
                                FontAttributes="Bold"
                                FontSize="16"
                                LineBreakMode="TailTruncation"
                                MaxLines="2"
                                Text="{Binding seriesName}"
                                TextColor="{AppThemeBinding Light={StaticResource Black},
                                                            Dark={StaticResource PrimaryDark}}"
                                VerticalOptions="Center" />
                            <ImageButton
                                x:Name="menuButton"
                                Grid.Row="0"
                                Grid.Column="4"
                                Margin="0,5,0,0"
                                Command="{Binding Source={RelativeSource AncestorType={x:Type vModels:ActiveSeriesPageViewModel}}, Path=AdditionalActionCommand}"
                                CommandParameter="{Binding .}"
                                HeightRequest="15"
                                HorizontalOptions="End"
                                Rotation="90"
                                Source="dots.png"
                                VerticalOptions="Start"
                                WidthRequest="15">
                                <ImageButton.Behaviors>
                                    <toolkit:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" />
                                </ImageButton.Behaviors>
                            </ImageButton>

                            <HorizontalStackLayout
                                Grid.Row="2"
                                Grid.Column="1"
                                Grid.ColumnSpan="3"
                                HorizontalOptions="Start"
                                VerticalOptions="Center">
                                <HorizontalStackLayout IsVisible="{Binding isOver}">
                                    <Label
                                        FontAttributes="Italic"
                                        FontSize="11"
                                        Text="{Binding lastEpisode, StringFormat='{0} эп,'}"
                                        VerticalOptions="Center" />
                                    <Label
                                        Margin="2,1,2,0"
                                        FontAttributes="Italic"
                                        FontSize="9"
                                        Text="просмотрено: "
                                        TextColor="Grey"
                                        VerticalTextAlignment="Center" />
                                    <Label
                                        FontAttributes="Italic"
                                        FontSize="11"
                                        Text="{Binding GetOverDate, Mode=OneWay}"
                                        VerticalTextAlignment="Center" />
                                </HorizontalStackLayout>

                                <Label
                                    FontFamily="NunitoItalic"
                                    FontSize="12"
                                    IsVisible="{Binding isOver, Converter={service:InverseBoolConverter}}"
                                    TextColor="{AppThemeBinding Light={StaticResource Gray950},
                                                                Dark={StaticResource White}}"
                                    VerticalOptions="Center">
                                    <Label.Text>
                                        <MultiBinding StringFormat="Просмотрено: {0} из {1} эп.">
                                            <Binding Path="currentEpisode" />
                                            <Binding Path="lastEpisode" />
                                        </MultiBinding>
                                    </Label.Text>
                                </Label>


                                <HorizontalStackLayout IsVisible="{Binding isFavourite}">
                                    <Image
                                        HeightRequest="15"
                                        Source="dot.png"
                                        VerticalOptions="Center"
                                        WidthRequest="15">
                                        <Image.Behaviors>
                                            <toolkit:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" />
                                        </Image.Behaviors>
                                    </Image>
                                    <ImageButton
                                        x:Name="favouriteBtn"
                                        HeightRequest="15"
                                        IsVisible="true"
                                        Source="bookmark.png"
                                        VerticalOptions="Center"
                                        WidthRequest="15" />
                                </HorizontalStackLayout>
                            </HorizontalStackLayout>
                            <StackLayout
                                Grid.Row="3"
                                Grid.Column="1"
                                Grid.ColumnSpan="4"
                                BackgroundColor="{AppThemeBinding Light={StaticResource Primary},
                                                                  Dark={StaticResource PrimaryDark}}"
                                HorizontalOptions="Fill" />
                            <Label
                                Grid.Row="4"
                                Grid.Column="1"
                                Grid.ColumnSpan="4"
                                Margin="0,5,0,0"
                                FontSize="14"
                                LineBreakMode="TailTruncation"
                                MaxLines="5"
                                Text="{Binding seriesDescription, TargetNullValue='Описание отсутствует.'}"
                                TextColor="{AppThemeBinding Light={StaticResource Gray500},
                                                            Dark={StaticResource Gray400}}"
                                VerticalOptions="StartAndExpand" />
                        </Grid>
                        <Border.Stroke>
                            <LinearGradientBrush EndPoint="0,1">
                                <GradientStop Offset="0.1" Color="{AppThemeBinding Light={StaticResource ActivePageLightGradient}, Dark={StaticResource ActivePageDarkGradient}}" />
                                <GradientStop Color="{AppThemeBinding Light={StaticResource ActivePageLightStroke}, Dark={StaticResource PrimaryDark}}">
                                    <GradientStop.Offset>1.0</GradientStop.Offset>
                                </GradientStop>
                            </LinearGradientBrush>
                        </Border.Stroke>
                        <Border.GestureRecognizers>
                            <TapGestureRecognizer
                                Command="{Binding Source={RelativeSource AncestorType={x:Type vModels:ActiveSeriesPageViewModel}}, Path=DetailViewCommand}"
                                CommandParameter="{Binding .}"
                                NumberOfTapsRequired="1" />
                        </Border.GestureRecognizers>
                    </Border>
                </DataTemplate>
            </CollectionView.ItemTemplate>
            <CollectionView.EmptyView>
                <ContentView>
                    <control:CustomEmptyView
                        x:Name="SeriesEmptyView"
                        IsActive="{Binding IsBusy, Mode=OneWay}"
                        MainText="Здесь самую 'малость' пустовато :( "
                        SubText="Может стоит это исправить?" />
                </ContentView>
            </CollectionView.EmptyView>

        </CollectionView>
    </toolkit:DockLayout>

</ContentPage>