<?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"
             x:Class="WorkFlowCheck.MAUI.Pages.CheckList.CheckListWorkPage"
             xmlns:templateselectors="clr-namespace:WorkFlowCheck.MAUI.TemplateSelectors"
             xmlns:local="clr-namespace:WorkFlowCheck.MAUI.Pages.CheckList"
             BackgroundColor="#f0f0f0"
             Title="">
    <Grid>
        <StackLayout Padding="20">
            <Label x:Name="progressLabel" Text="Folyamatjelző" FontSize="Default" HorizontalOptions="Center" />
            <ProgressBar x:Name="progressBar" WidthRequest="450" HeightRequest="30" />
            <Label Text="Ellenőrzési feladatok" FontAttributes="Bold" FontSize="Medium" HorizontalOptions="Center" />
            <Frame BorderColor="Gray" Padding="1" Margin="2" HasShadow="True" CornerRadius="2">
                <CollectionView x:Name="CheckPointCheckListRows" 
                            HeightRequest="555"
                            ItemsSource="{Binding CheckListRows}"
                            ItemTemplate="{StaticResource AnswerTemplateSelector}">
                    <CollectionView.EmptyView>
                        <Label Text="Nincsenek feladatok." HorizontalOptions="Center" VerticalOptions="Center"/>
                    </CollectionView.EmptyView>
                </CollectionView>
            </Frame>
            <Grid ColumnSpacing="46" Margin="0,10,0,10">
                <!-- 10 pixel térköz az oszlopok között -->
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="Auto" />
                    <!-- Üres tér a két gomb között -->
                    <ColumnDefinition Width="200" />
                </Grid.ColumnDefinitions>

                <Button x:Name="saveButton" Text="Mentés" Command="{Binding SaveCommand}" Grid.Column="0"/>
                <Button x:Name="sendButton" Text="Beküldés" Command="{Binding SendCommand}" Grid.Column="2" Background="DarkRed" TextColor="Yellow"/>
            </Grid>
        </StackLayout>
        <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>
        <DataTemplate x:Key="I_N_Template">
            <Frame BorderColor="Gray" Padding="1" Margin="2" HasShadow="True" CornerRadius="2">
                <Grid ColumnSpacing="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding CheckListTemplateRowDTO.RowIndex,StringFormat='{}{0:D1}.'}" BackgroundColor="WhiteSmoke" Grid.Column="0" />
                    <Label Text="{Binding CheckListTemplateRowDTO.OperationDescription}" BackgroundColor="WhiteSmoke" Grid.Column="1" />
                    <Grid Grid.Column="2" ColumnDefinitions="*,*">
                        <Label Text="I" Grid.Column="0" HorizontalTextAlignment="Center"/>
                        <Label Text="N" Grid.Column="1" HorizontalTextAlignment="Center"/>
                    </Grid>
                    <Grid Grid.Column="3" RowDefinitions="Auto,Auto">
                        <local:BindableRadioButton
                            Grid.Row="0"
                            Content="Igen"
                            Value="Igen"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerYes}" 
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                        <local:BindableRadioButton
                            Grid.Row="1"
                            Content="Nem"
                            Value="Nem"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerNo}"
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                    </Grid>
                </Grid>
            </Frame>
        </DataTemplate>
        <DataTemplate x:Key="P_Template">
            <Frame BorderColor="Gray" Padding="1" Margin="2" HasShadow="True" CornerRadius="2">
                <Grid ColumnSpacing="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="2*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding CheckListTemplateRowDTO.RowIndex,StringFormat='{}{0:D1}.'}" BackgroundColor="WhiteSmoke" Grid.Column="0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
                    <Label Text="{Binding CheckListTemplateRowDTO.OperationDescription}" BackgroundColor="WhiteSmoke" Grid.Column="1" />
                    <Grid Grid.Column="2" ColumnDefinitions="Auto,Auto" VerticalOptions="Center">
                        <Image Grid.Column="0"
                               WidthRequest="95" 
                               HeightRequest="120"
                               Aspect="AspectFill"
                               Margin="0,0,10,0"
                               VerticalOptions="Center"
                               Source="{Binding Photo, Converter={StaticResource ByteArrayToImageSourceConverter}}" />
                        <Button Text="📷" Grid.Column="1" 
                                VerticalOptions="Center"
                                Command="{Binding BindingContext.CreatePhoto, Source={x:Reference CheckPointCheckListRows}}" 
                                CommandParameter="{Binding .}" />
                    </Grid>
                </Grid>
            </Frame>
        </DataTemplate>
        <DataTemplate x:Key="V_Template">
            <Frame BorderColor="Gray" Padding="1" Margin="2" HasShadow="True" CornerRadius="2">
                <Grid ColumnSpacing="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding CheckListTemplateRowDTO.RowIndex,StringFormat='{}{0:D1}.'}" BackgroundColor="WhiteSmoke" Grid.Column="0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
                    <Label Text="{Binding CheckListTemplateRowDTO.OperationDescription}" BackgroundColor="WhiteSmoke" Grid.Column="1" />
                    <Grid Grid.Column="2" ColumnDefinitions="*">
                        <Label Text="Érték megadása szükséges" Grid.Column="0" HorizontalTextAlignment="Center"/>
                    </Grid>
                    <Entry Grid.Column="3"
                        Text="{Binding Answer}"
                        HorizontalTextAlignment="Center"
                        VerticalOptions="Center" />
                </Grid>
            </Frame>
        </DataTemplate>
        <DataTemplate x:Key="SI_N_Template">
            <Frame BorderColor="Gray" Padding="1" Margin="2" HasShadow="True" CornerRadius="2">
                <Grid ColumnSpacing="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding CheckListTemplateRowDTO.RowIndex, StringFormat='{}{0:D1}.'}" BackgroundColor="WhiteSmoke" Grid.Column="0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                    <Label Text="{Binding CheckListTemplateRowDTO.OperationDescription}" BackgroundColor="WhiteSmoke" Grid.Column="1" />
                    <Grid Grid.Column="2" ColumnDefinitions="*,*">
                        <Label Text="I" Grid.Column="0" BackgroundColor="Yellow" TextColor="Black" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                        <Label Text="N" Grid.Column="1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                    </Grid>
                    <Grid Grid.Column="3" RowDefinitions="Auto,Auto">
                        <local:BindableRadioButton
                            Grid.Row="0"
                            Content="Igen"
                            Value="Igen"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerYes}" 
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                        <local:BindableRadioButton
                            Grid.Row="1"
                            Content="Nem"
                            Value="Nem"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerNo}"
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                    </Grid>
                </Grid>
            </Frame>
        </DataTemplate>
        <DataTemplate x:Key="I_SN_Template">
            <Frame BorderColor="Gray" Padding="1" Margin="2" HasShadow="True" CornerRadius="2">
                <Grid ColumnSpacing="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding CheckListTemplateRowDTO.RowIndex, StringFormat='{}{0:D1}.'}" BackgroundColor="WhiteSmoke" Grid.Column="0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                    <Label Text="{Binding CheckListTemplateRowDTO.OperationDescription}" BackgroundColor="WhiteSmoke" Grid.Column="1" />
                    <Grid Grid.Column="2" ColumnDefinitions="*,*">
                        <Label Text="I" Grid.Column="0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                        <Label Text="N" Grid.Column="1" BackgroundColor="Yellow" TextColor="Black"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                    </Grid>
                    <Grid Grid.Column="3" RowDefinitions="Auto,Auto">
                        <local:BindableRadioButton
                            Grid.Row="0"
                            Content="Igen"
                            Value="Igen"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerYes}" 
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                        <local:BindableRadioButton
                            Grid.Row="1"
                            Content="Nem"
                            Value="Nem"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerNo}"
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                    </Grid>
                </Grid>
            </Frame>
        </DataTemplate>
        <DataTemplate x:Key="PI_N_Template">
            <Frame BorderColor="Gray" Padding="1" Margin="2" HasShadow="True" CornerRadius="2">
                <Grid ColumnSpacing="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding CheckListTemplateRowDTO.RowIndex, StringFormat='{}{0:D1}.'}" BackgroundColor="WhiteSmoke" Grid.Column="0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                    <Label Text="{Binding CheckListTemplateRowDTO.OperationDescription}" BackgroundColor="WhiteSmoke" Grid.Column="1" />
                    <Grid Grid.Column="2" ColumnDefinitions="*,*">
                        <Label Text="I" Grid.Column="0" BackgroundColor="Red" TextColor="Black" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                        <Label Text="N" Grid.Column="1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                    </Grid>
                    <Grid Grid.Column="3" RowDefinitions="Auto,Auto">
                        <local:BindableRadioButton
                            Grid.Row="0"
                            Content="Igen"
                            Value="Igen"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerYes}" 
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                        <local:BindableRadioButton
                            Grid.Row="1"
                            Content="Nem"
                            Value="Nem"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerNo}"
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                    </Grid>
                </Grid>
            </Frame>
        </DataTemplate>
        <DataTemplate x:Key="I_PN_Template">
            <Frame BorderColor="Gray" Padding="1" Margin="2" HasShadow="True" CornerRadius="2">
                <Grid ColumnSpacing="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding CheckListTemplateRowDTO.RowIndex, StringFormat='{}{0:D1}.'}" BackgroundColor="WhiteSmoke" Grid.Column="0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                    <Label Text="{Binding CheckListTemplateRowDTO.OperationDescription}" BackgroundColor="WhiteSmoke" Grid.Column="1" />
                    <Grid Grid.Column="2" ColumnDefinitions="*,*">
                        <Label Text="I" Grid.Column="0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                        <Label Text="N" Grid.Column="1" BackgroundColor="Red" TextColor="Black"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                    </Grid>
                    <Grid Grid.Column="3" RowDefinitions="Auto,Auto">
                        <local:BindableRadioButton
                            Grid.Row="0"
                            Content="Igen"
                            Value="Igen"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerYes}" 
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                        <local:BindableRadioButton
                            Grid.Row="1"
                            Content="Nem"
                            Value="Nem"
                            BindableGroupName="{Binding GroupName}"
                            IsChecked="{Binding AnswerNo}"
                            HorizontalOptions="Center" VerticalOptions="Center"/>
                    </Grid>
                </Grid>
            </Frame>
        </DataTemplate>
        <templateselectors:AnswerTypeTemplateSelector x:Key="AnswerTemplateSelector"
            I_N_Template="{StaticResource I_N_Template}"
            P_Template="{StaticResource P_Template}"
            SI_N_Template="{StaticResource SI_N_Template}"
            I_SN_Template="{StaticResource I_SN_Template}"
            PI_N_Template="{StaticResource PI_N_Template}"
            I_PN_Template="{StaticResource I_PN_Template}" 
            V_Template="{StaticResource V_Template}"/>
        <local:ByteArrayToImageSourceConverter x:Key="ByteArrayToImageSourceConverter"/>
    </ContentPage.Resources>
</ContentPage>