<?xml version="1.0"
      encoding="utf-8"?>

<Page x:Class="Marechai.App.Presentation.Views.NewsPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:utu="using:Uno.Toolkit.UI"
      NavigationCacheMode="Required"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid utu:SafeArea.Insets="VisibleBounds">
        <RefreshContainer x:Name="RefreshContainer"
                          RefreshRequested="RefreshContainer_RefreshRequested">
            <ScrollViewer>
                <Grid Padding="16">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <!-- News Title Section -->
                    <StackPanel Grid.Row="0"
                                Margin="0,0,0,16">
                        <TextBlock Text="Latest News"
                                   FontSize="32"
                                   FontWeight="Bold"
                                   Foreground="{ThemeResource SystemAccentColor}"
                                   Margin="0,0,0,8" />
                        <TextBlock Text="Stay updated with the latest additions to the database"
                                   FontSize="14"
                                   Foreground="{ThemeResource SystemBaseMediumColor}"
                                   TextWrapping="Wrap" />
                    </StackPanel>

                    <!-- Loading State -->
                    <StackPanel Grid.Row="2"
                                Visibility="{Binding IsLoading}"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"
                                Padding="32"
                                Spacing="16">
                        <ProgressRing IsActive="True"
                                      IsIndeterminate="True"
                                      Height="48"
                                      Width="48"
                                      Foreground="{ThemeResource SystemAccentColor}" />
                        <TextBlock Text="Loading latest news..."
                                   FontSize="16"
                                   TextAlignment="Center"
                                   Foreground="{ThemeResource SystemBaseMediumColor}" />
                    </StackPanel>

                    <!-- Error State -->
                    <StackPanel Grid.Row="2"
                                Visibility="{Binding HasError}"
                                VerticalAlignment="Center"
                                Spacing="16"
                                Padding="32">
                        <InfoBar IsOpen="True"
                                 Severity="Error"
                                 Title="Unable to Load News"
                                 Message="{Binding ErrorMessage}"
                                 IsClosable="False" />
                        <Button Content="Retry"
                                Command="{Binding LoadNews}"
                                HorizontalAlignment="Center"
                                Style="{ThemeResource AccentButtonStyle}" />
                    </StackPanel>

                    <!-- News Feed -->
                    <ItemsControl Grid.Row="2"
                                  ItemsSource="{Binding NewsList}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Button Margin="0,0,0,12"
                                        Padding="0"
                                        Background="Transparent"
                                        BorderThickness="0"
                                        HorizontalAlignment="Stretch"
                                        HorizontalContentAlignment="Stretch"
                                        Command="{Binding NavigateToItemCommand}"
                                        CommandParameter="{Binding News}">
                                    <Border CornerRadius="8"
                                            Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                            BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
                                            BorderThickness="1"
                                            Padding="16">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                            </Grid.RowDefinitions>

                                            <!-- Date -->
                                            <TextBlock Grid.Row="0"
                                                       Text="{Binding News.Timestamp}"
                                                       FontSize="12"
                                                       Foreground="{ThemeResource SystemBaseMediumColor}"
                                                       Margin="0,0,0,12" />

                                            <!-- News Title/Text (Localized) -->
                                            <TextBlock Grid.Row="1"
                                                       Text="{Binding DisplayText}"
                                                       FontSize="16"
                                                       FontWeight="SemiBold"
                                                       Foreground="{ThemeResource SystemBaseHighColor}"
                                                       TextWrapping="Wrap"
                                                       Margin="0,0,0,12" />

                                            <!-- Item Name -->
                                            <TextBlock Grid.Row="2"
                                                       Text="{Binding News.ItemName}"
                                                       FontSize="14"
                                                       Foreground="{ThemeResource SystemAccentColor}" />
                                        </Grid>
                                    </Border>
                                </Button>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Vertical"
                                            Spacing="0" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </Grid>
            </ScrollViewer>
        </RefreshContainer>
    </Grid>
</Page>