<?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:local="clr-namespace:WorkFlowCheck.MAUI.Pages.CheckList"
             x:Class="WorkFlowCheck.MAUI.Pages.CheckList.CheckListPage"
             Title="Ellenőrzések"
             BackgroundColor="#f0f0f0">
    <Shell.TitleView>
        <Grid Padding="2" VerticalOptions="Center">
            <Label Text="Ellenőrzések"
               FontSize="22"
               FontAttributes="Bold"
               VerticalOptions="Center"
               HorizontalOptions="Start" />
            <HorizontalStackLayout 
            VerticalOptions="Center" 
            HorizontalOptions="End" 
            Spacing="5">
                <Frame CornerRadius="1"
               Padding="5"
               HasShadow="True"
               VerticalOptions="Center"
               HorizontalOptions="End">
                    <Label x:Name="DeviceId" 
                       FontSize="10"
                       TextColor="Black"/>
                </Frame>
                <Frame CornerRadius="1"
               Padding="5"
               HasShadow="True"
               VerticalOptions="Center"
               HorizontalOptions="End">
                    <Label x:Name="UserInfo" 
                       FontSize="10"
                       FontAttributes="Bold"
                       TextColor="Black"/>
                </Frame>
            </HorizontalStackLayout>
        </Grid>
    </Shell.TitleView>

    <Grid RowDefinitions="*,4*,2*" Padding="30,30">
        <VerticalStackLayout Grid.Row="0" Margin="0,0,0,0">
            <Label x:Name="progressLabel" Text="Folyamatjelző" FontSize="Medium" HorizontalOptions="Center" />
            <HorizontalStackLayout HorizontalOptions="Center" Spacing="10">
                <ActivityIndicator x:Name="LoadingIndicator" 
                                    IsRunning="False" 
                                    IsVisible="False"
                                    VerticalOptions="Center" 
                                    HorizontalOptions="Center" />
                <ProgressBar x:Name="progressBar" 
                             WidthRequest="310" 
                             HeightRequest="30" 
                             VerticalOptions="Center" />
                <Button x:Name="SyncBtn"
                        ImageSource="cloud_download"
                        Text="" 
                        Clicked="OnSyncClicked"
                        HeightRequest="50"
                        MaximumHeightRequest="50"
                        WidthRequest="50" 
                        MaximumWidthRequest="50"
                        CornerRadius="25"
                        VerticalOptions="Center" />
            </HorizontalStackLayout>

        </VerticalStackLayout>
        <Frame Grid.Row="1"
               BorderColor="Gray" 
               Padding="5" 
               Margin="1" 
               CornerRadius="10"
               HasShadow="True">
            <RefreshView x:Name="CheckListRefreshView"
                         IsRefreshing="{Binding IsRefreshing}"
                         Command="{Binding RefreshCommand}">
                <ScrollView >
                    <VerticalStackLayout>
                        <Label Text="Megkezdett ellenőrzések" FontAttributes="Bold" FontSize="Medium" HorizontalOptions="Center" />
                        <CollectionView x:Name="CheckList">
                            <CollectionView.ItemTemplate>
                                <DataTemplate>
                                    <Frame BorderColor="Gray" Padding="15" Margin="5" HasShadow="True" BackgroundColor="WhiteSmoke">
                                        <Grid ColumnDefinitions="*,Auto">
                                            <Grid ColumnSpacing="10">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="*" />
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto"/>
                                                </Grid.RowDefinitions>

                                                <!-- Bal oldali gomb -->
                                                <Button Grid.Column="0"
                                                    VerticalOptions="Center"
                                                    Text="i"
                                                    FontSize="17"   
                                                    FontAttributes="Bold"
                                                    BackgroundColor="Purple"
                                                    HeightRequest="40"
                                                    WidthRequest="40" 
                                                    CornerRadius="20"
                                                    BorderColor="Gray"
                                                    BorderWidth="1"
                                                    Command="{Binding BindingContext.InfoCommand, Source={x:Reference CheckList}}" 
                                                    CommandParameter="{Binding .}" />

                                                <!-- Jobb oldali szövegek -->
                                                <StackLayout Grid.Column="1" Spacing="2">
                                                    <Label Text="{Binding DocumentNumber}" FontSize="14" FontAttributes="Bold" />
                                                    <Label Text="{Binding DateExecution}" FontSize="12" />
                                                    <Label Text="{Binding CheckListTemplateHeaderDTO.ShortName}" FontSize="10" />
                                                    <Label Text="{Binding StatusName}" FontSize="10" />
                                                </StackLayout>
                                            </Grid>

                                            <!-- Jobb oldali oszlop: Két gomb egymás mellett -->
                                            <HorizontalStackLayout Grid.Column="1" HorizontalOptions="End" Spacing="3">
                                                <Button Text="L"
                                                    Margin="0,0,0,0"
                                                    BackgroundColor="DarkOrange"
                                                    HeightRequest="50"
                                                    MaximumHeightRequest="50"
                                                    WidthRequest="50" 
                                                    MaximumWidthRequest="50"
                                                    CornerRadius="25"    
                                                    IsVisible="{Binding CheckStatus, Converter={StaticResource CloseConverter}}"
                                                    Command="{Binding BindingContext.CloseCommand, Source={x:Reference CheckList}}" 
                                                    CommandParameter="{Binding .}">
                                                </Button>
                                                <Button Text=""
                                                    Margin="0,0,0,0"
                                                    ImageSource="arrow_down.svg"
                                                    BackgroundColor="Red"
                                                    HeightRequest="50"
                                                    MaximumHeightRequest="50"
                                                    WidthRequest="50" 
                                                    MaximumWidthRequest="50"
                                                    CornerRadius="25"    
                                                    IsVisible="{Binding CheckStatus, Converter={StaticResource BlockedConverter}}"
                                                    Command="{Binding BindingContext.BlockCommand, Source={x:Reference CheckList}}" 
                                                    CommandParameter="{Binding .}">
                                                </Button>
                                                <Button Text=""
                                                    ImageSource="arrow_up.svg"
                                                    Margin="0,0,0,0"
                                                    BackgroundColor="Green"
                                                    HeightRequest="50"
                                                    MaximumHeightRequest="50"
                                                    WidthRequest="50" 
                                                    MaximumWidthRequest="50"
                                                     CornerRadius="25"
                                                    IsVisible="{Binding CheckStatus, Converter={StaticResource UnBlockedConverter}}"
                                                    Command="{Binding BindingContext.UnblockCommand, Source={x:Reference CheckList}}" 
                                                    CommandParameter="{Binding .}">
                                                </Button>
                                                <Button Text=""
                                                    ImageSource="check_circle.svg"    
                                                    Margin="0,0,0,0"
                                                    BackgroundColor="DarkGreen"
                                                    HeightRequest="50"
                                                    MaximumHeightRequest="50"
                                                    WidthRequest="50"
                                                    MaximumWidthRequest="50"
                                                    CornerRadius="25"
                                                    IsVisible="{Binding CheckStatus, Converter={StaticResource AcceptConverter}}"
                                                    Command="{Binding BindingContext.AcceptCommand, Source={x:Reference CheckList}}" 
                                                    CommandParameter="{Binding .}">
                                                </Button>
                                                <Button Text=""
                                                    ImageSource="arrow_right.svg"
                                                    Margin="0,0,0,0"
                                                    BackgroundColor="Blue"
                                                    CornerRadius="25"
                                                    HeightRequest="50"
                                                    MaximumHeightRequest="50"
                                                    WidthRequest="50" 
                                                    MaximumWidthRequest="50"
                                                    IsVisible="{Binding CheckStatus, Converter={StaticResource ContinueConverter}, ConverterParameter = {Binding IsNFCSupported}}"
                                                    Command="{Binding BindingContext.ContinueCommand, Source={x:Reference CheckList}}" 
                                                    CommandParameter="{Binding .}">
                                                </Button>
                                            </HorizontalStackLayout>
                                        </Grid>
                                    </Frame>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                    </VerticalStackLayout>
                </ScrollView>
            </RefreshView>
        </Frame>

        <Frame Grid.Row="2"
                   BorderColor="Gray" 
                   Padding="5" 
                   Margin="1" 
                   CornerRadius="10"
                   HasShadow="True">
            <RefreshView x:Name="CheckListTemplateRefreshView"
             IsRefreshing="{Binding IsRefreshingT}"
             Command="{Binding RefreshCommandT}">
                <ScrollView>
                    <VerticalStackLayout>
                        <Label Text="Új ellenőrzés" FontAttributes="Bold" FontSize="Medium" HorizontalOptions="Center" />
                        <CollectionView x:Name="CheckListTemplate">
                            <CollectionView.ItemTemplate>
                                <DataTemplate>
                                    <Frame BorderColor="Gray" Padding="10" Margin="5" HasShadow="True" BackgroundColor="WhiteSmoke">
                                        <Grid ColumnDefinitions="*,Auto">
                                            <StackLayout>
                                                <Label Text="{Binding ShortName}" FontSize="16" FontAttributes="Bold" />
                                                <Label Text="{Binding Description}" FontSize="14" />
                                            </StackLayout>
                                            <HorizontalStackLayout HorizontalOptions="End">
                                                <Button Text="Új ellenőrzés" 
                                                BackgroundColor="Green"
                                                Margin="5,0,0,0"
                                                HeightRequest="50"
                                                MaximumHeightRequest="50"
                                                WidthRequest="120" 
                                                MaximumWidthRequest="120"
                                                Command="{Binding BindingContext.NewCommand, Source={x:Reference CheckListTemplate}}" 
                                                CommandParameter="{Binding .}" />
                                            </HorizontalStackLayout>
                                        </Grid>
                                    </Frame>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                    </VerticalStackLayout>
                </ScrollView>
            </RefreshView>
        </Frame>
        <Grid BackgroundColor="#80000000"
              IsVisible="{Binding IsLoading}"
              Grid.Row="0"
              Grid.RowSpan="2"
              VerticalOptions="Fill"
              HorizontalOptions="Fill"
              ZIndex="1000">
            <ActivityIndicator IsRunning="{Binding IsLoading}"
                       HorizontalOptions="Center"
                       VerticalOptions="Center"
                       Color="White"
                       WidthRequest="50"
                       HeightRequest="50"/>
        </Grid>
    </Grid>
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:BlockedConverter x:Key="BlockedConverter" />
            <local:UnBlockedConverter x:Key="UnBlockedConverter" />
            <local:ContinueConverter x:Key="ContinueConverter" />
            <local:AcceptConverter x:Key="AcceptConverter" />
            <local:CloseConverter x:Key="CloseConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
</ContentPage>