<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="SeriesTracker.Views.NewSeriesPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:Models="clr-namespace:SeriesTracker.Models"
    xmlns:controls="clr-namespace:SeriesTracker.Controls"
    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.TabBarIsVisible="False">
    <Shell.BackButtonBehavior>
        <BackButtonBehavior IsEnabled="False" IsVisible="False" />
    </Shell.BackButtonBehavior>

    <ContentPage.Resources>
        <Style x:Key="InvalidEntryStyle" TargetType="Entry">
            <Setter Property="TextColor" Value="{StaticResource Red}" />
        </Style>
        <Style x:Key="NameUnderLineEditor" TargetType="Line">
            <Setter Property="StrokeThickness" Value="1.5" />
            <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource LineSeparatorColor}, Dark={StaticResource ActivePageDarkGradient}}" />
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding Source={x:Reference nameEditor}, Path=IsFocused}"
                    TargetType="Line"
                    Value="True">
                    <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" />
                </DataTrigger>

            </Style.Triggers>
        </Style>
        <Style x:Key="DescriptionUnderLineEditor" TargetType="Line">
            <Setter Property="StrokeThickness" Value="1.5" />
            <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource LineSeparatorColor}, Dark={StaticResource ActivePageDarkGradient}}" />
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding Source={x:Reference descriptionEditor}, Path=IsFocused}"
                    TargetType="Line"
                    Value="True">
                    <Setter Property="BackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" />
                </DataTrigger>

            </Style.Triggers>
        </Style>
    </ContentPage.Resources>

    <ScrollView Margin="15,0,15,40" VerticalScrollBarVisibility="Never">
        <StackLayout>
            <StackLayout VerticalOptions="FillAndExpand">
                <Grid x:Name="gridContainer">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40" />
                        <RowDefinition Height="230" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="60" />
                        <RowDefinition Height="60" />
                        <RowDefinition Height="60" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <ImageButton
                        Grid.Row="1"
                        Grid.Column="0"
                        Grid.ColumnSpan="3"
                        Margin="15"
                        Command="{Binding BackCommand}"
                        HeightRequest="25"
                        HorizontalOptions="Start"
                        Source="backarrow.png"
                        VerticalOptions="Start"
                        WidthRequest="25">
                        <ImageButton.Behaviors>
                            <toolkit:IconTintColorBehavior TintColor="{StaticResource Gray400}" />
                        </ImageButton.Behaviors>
                    </ImageButton>
                    <StackLayout
                        Grid.Row="1"
                        Grid.Column="1"
                        Margin="15">
                        <Border
                            x:Name="posterBorder"
                            BackgroundColor="Transparent"
                            HeightRequest="130"
                            HorizontalOptions="Center"
                            Stroke="Transparent"
                            WidthRequest="180">
                            <Border.StrokeShape>
                                <RoundRectangle CornerRadius="10" />
                            </Border.StrokeShape>
                            <Image
                                x:Name="posterImage"
                                Aspect="AspectFit"
                                BackgroundColor="Transparent"
                                Source="addposter.png">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="SelectPosterImage_Tapped" />
                                </Image.GestureRecognizers>
                            </Image>
                        </Border>
                        <Label
                            x:Name="tipPosterLabel"
                            Margin="15"
                            FontAttributes="Italic"
                            FontSize="12"
                            HorizontalOptions="Center"
                            HorizontalTextAlignment="Center"
                            IsVisible="true"
                            Text="Нажмите, чтобы добавить изображение"
                            TextColor="{StaticResource Gray400}"
                            WidthRequest="170" />
                    </StackLayout>
                    <StackLayout Grid.Row="1" Grid.Column="2">
                        <Border
                            Margin="10,20,0,0"
                            Padding="5"
                            BackgroundColor="Transparent"
                            HorizontalOptions="Center"
                            Stroke="{AppThemeBinding Light={StaticResource Primary},
                                                     Dark={StaticResource PrimaryDark}}"
                            StrokeShape="RoundRectangle 50"
                            StrokeThickness="1"
                            VerticalOptions="Start">
                            <ImageButton
                                Clicked="ImageButton_Clicked"
                                HeightRequest="30"
                                Source="myanimelist.png"
                                WidthRequest="30">
                                <ImageButton.Behaviors>
                                    <toolkit:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" />
                                </ImageButton.Behaviors>
                            </ImageButton>
                        </Border>

                        <Border
                            Margin="10,20,0,0"
                            Padding="5"
                            BackgroundColor="Transparent"
                            HorizontalOptions="Center"
                            Stroke="{AppThemeBinding Light={StaticResource Primary},
                                                     Dark={StaticResource PrimaryDark}}"
                            StrokeShape="RoundRectangle 50"
                            StrokeThickness="1"
                            VerticalOptions="Start">
                            <ImageButton
                                Clicked="ImageButton_Clicked_1"
                                HeightRequest="30"
                                Source="shikimori.png"
                                WidthRequest="30">
                                <ImageButton.Behaviors>
                                    <toolkit:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" />
                                </ImageButton.Behaviors>
                            </ImageButton>
                        </Border>
                    </StackLayout>
                    <StackLayout
                        Grid.Row="2"
                        Grid.Column="0"
                        Grid.ColumnSpan="3"
                        HorizontalOptions="Fill">
                        <Editor
                            x:Name="nameEditor"
                            Margin="0,5,0,-10"
                            AutoSize="TextChanges"
                            FontSize="Title"
                            HorizontalOptions="FillAndExpand"
                            HorizontalTextAlignment="Center"
                            Placeholder="Название сериала"
                            Text="{Binding Series.seriesName, Mode=TwoWay}"
                            VerticalTextAlignment="Center">
                            <Editor.Keyboard>
                                <Keyboard x:FactoryMethod="Create">
                                    <x:Arguments>
                                        <KeyboardFlags>Suggestions,CapitalizeSentence</KeyboardFlags>
                                    </x:Arguments>
                                </Keyboard>
                            </Editor.Keyboard>
                            <Editor.Behaviors>
                                <toolkit:TextValidationBehavior
                                    x:Name="nameValidator"
                                    Flags="ValidateOnValueChanged,ValidateOnAttaching"
                                    InvalidStyle="{StaticResource InvalidEntryStyle}"
                                    MinimumLength="1" />
                            </Editor.Behaviors>
                        </Editor>
                        <Line x:Name="nameUnderline" Style="{StaticResource NameUnderLineEditor}" />
                    </StackLayout>
                    <toolkit:Expander
                        x:Name="descriptionExpander"
                        Grid.Row="3"
                        Grid.ColumnSpan="3"
                        Padding="10,0,10,10"
                        ExpandedChanged="DescriptionExpander_ExpandedChanged"
                        VerticalOptions="CenterAndExpand">
                        <toolkit:Expander.Header>
                            <HorizontalStackLayout HorizontalOptions="CenterAndExpand">
                                <Label
                                    x:Name="descriptionCaption"
                                    FontFamily="NunitoItalic"
                                    FontSize="14"
                                    Text="Открыть описание"
                                    TextColor="{StaticResource Gray400}" />
                                <Image
                                    x:Name="descriptionImage"
                                    HeightRequest="20"
                                    Source="downarrow.png"
                                    WidthRequest="20">
                                    <Image.Behaviors>
                                        <toolkit:IconTintColorBehavior TintColor="{AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource Gray200}}" />
                                    </Image.Behaviors>
                                </Image>
                            </HorizontalStackLayout>
                        </toolkit:Expander.Header>
                        <StackLayout>
                            <Editor
                                x:Name="descriptionEditor"
                                Margin="0,0,0,-10"
                                AutoSize="TextChanges"
                                FontSize="13"
                                HorizontalOptions="FillAndExpand"
                                HorizontalTextAlignment="Center"
                                MinimumHeightRequest="200"
                                Placeholder="Здесь можно добавить описание сериала"
                                Text="{Binding Series.seriesDescription, Mode=TwoWay}"
                                VerticalTextAlignment="Start">
                                <Editor.Keyboard>
                                    <Keyboard x:FactoryMethod="Create">
                                        <x:Arguments>
                                            <KeyboardFlags>Suggestions,CapitalizeSentence</KeyboardFlags>
                                        </x:Arguments>
                                    </Keyboard>
                                </Editor.Keyboard>
                                <Editor.Behaviors>
                                    <toolkit:TextValidationBehavior
                                        Flags="ValidateOnValueChanged,ValidateOnAttaching"
                                        InvalidStyle="{StaticResource InvalidEntryStyle}"
                                        MinimumLength="1" />
                                </Editor.Behaviors>
                            </Editor>
                            <Line x:Name="descriptionUnderline" Style="{StaticResource DescriptionUnderLineEditor}" />
                        </StackLayout>
                    </toolkit:Expander>


                    <controls:MaterialEntry
                        x:Name="currentEntry"
                        Grid.Row="4"
                        Grid.Column="0"
                        Grid.ColumnSpan="2"
                        Margin="0,0,20,0"
                        HorizontalOptions="Center"
                        Label="Уже просмотрено"
                        Text="{Binding Series.currentEpisode, Mode=TwoWay}" />


                    <controls:MaterialEntry
                        x:Name="lastEntry"
                        Grid.Row="4"
                        Grid.Column="1"
                        Grid.ColumnSpan="2"
                        Margin="20,0,0,0"
                        HorizontalOptions="Center"
                        Label="Финальная серия"
                        Text="{Binding Series.lastEpisode, Mode=TwoWay}" />

                    <StackLayout
                        Grid.Row="5"
                        Grid.ColumnSpan="3"
                        Margin="0,15,0,0"
                        Padding="5"
                        HorizontalOptions="Center"
                        Orientation="Horizontal">
                        <StackLayout Orientation="Horizontal">
                            <Label Text="Ср. продожительность эпизода:" />
                            <CheckBox
                                x:Name="durationCheckBox"
                                Margin="0,-12,0,0"
                                CheckedChanged="CheckBox_CheckedChanged" />
                        </StackLayout>
                        <StackLayout Orientation="Horizontal">
                            <Label
                                BindingContext="{x:Reference durationEntry}"
                                FontAttributes="Italic"
                                Text="~"
                                TextColor="{Binding TextColor}" />
                            <Entry
                                x:Name="durationEntry"
                                Margin="0,-12,0,0"
                                HorizontalTextAlignment="Center"
                                IsEnabled="False"
                                Keyboard="Numeric"
                                MaxLength="3"
                                Text="{Binding Series.seriesDuration, Mode=TwoWay}"
                                TextChanged="DurationEntry_TextChanged">
                                <Entry.Behaviors>
                                    <toolkit:CharactersValidationBehavior
                                        CharacterType="Digit"
                                        Flags="ValidateOnValueChanged"
                                        InvalidStyle="{StaticResource InvalidEntryStyle}"
                                        RegexPattern="^0{1}$|^[1-9][0-9]*$" />
                                </Entry.Behaviors>
                            </Entry>
                            <Label
                                x:Name="durationLabel"
                                BindingContext="{x:Reference durationEntry}"
                                FontAttributes="Italic"
                                Text="мин."
                                TextColor="{Binding TextColor}" />
                        </StackLayout>
                    </StackLayout>

                    <StackLayout
                        Grid.Row="6"
                        Grid.ColumnSpan="3"
                        Margin="10"
                        Padding="5"
                        HorizontalOptions="Center"
                        Orientation="Horizontal">
                        <Label Text="Дата Выхода:" />
                        <DatePicker
                            x:Name="releaseDatePicker"
                            Margin="0,-12,0,0"
                            Date="{Binding Series.releaseDate, Mode=TwoWay}" />
                    </StackLayout>
                </Grid>
            </StackLayout>

            <StackLayout Orientation="Horizontal" VerticalOptions="End">
                <Border
                    BackgroundColor="{AppThemeBinding Light={StaticResource Primary},
                                                      Dark={StaticResource PrimaryDark}}"
                    HeightRequest="75"
                    HorizontalOptions="FillAndExpand"
                    Stroke="{AppThemeBinding Light={StaticResource Primary},
                                             Dark={StaticResource PrimaryDark}}"
                    VerticalOptions="End">
                    <Button
                        x:Name="saveBtn"
                        BackgroundColor="{AppThemeBinding Light={StaticResource Primary},
                                                          Dark={StaticResource PrimaryDark}}"
                        Command="{Binding SaveSeriesCommand}"
                        FontSize="24"
                        Text="Добавить"
                        TextColor="{AppThemeBinding Light=White,
                                                    Dark=Black}" />
                    <Border.StrokeShape>
                        <RoundRectangle CornerRadius="7" />
                    </Border.StrokeShape>
                    <Border.Shadow
                        Brush="{AppThemeBinding Light={StaticResource Primary},
                                                Dark={StaticResource PrimaryDark}}"
                        Opacity="0.7"
                        Radius="20"
                        Offset="20,20" />
                </Border>
            </StackLayout>
        </StackLayout>
    </ScrollView>
</ContentPage>