<?xml version="1.0" encoding="utf-8"?>
<Page x:Class="Marechai.App.Presentation.Views.CompanyDetailPage"
      x:Name="PageRoot"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:utu="using:Uno.Toolkit.UI"
      xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
      NavigationCacheMode="Required"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
    </Page.Resources>

    <Grid utu:SafeArea.Insets="VisibleBounds">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!-- Header -->
        <utu:NavigationBar Grid.Row="0"
                           Content="{Binding Path=Title}"
                           MainCommandMode="Action">
            <utu:NavigationBar.MainCommand>
                <AppBarButton Icon="Back"
                              Label="Back"
                              Command="{Binding GoBackCommand}"
                              AutomationProperties.Name="Go back" />
            </utu:NavigationBar.MainCommand>
        </utu:NavigationBar>

        <!-- Content -->
        <ScrollViewer Grid.Row="1">
            <StackPanel Padding="16"
                        Spacing="16">

                <!-- Logo Display (Top Center) -->
                <Image MaxHeight="96"
                       Stretch="Uniform"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       Margin="0,0,0,8"
                       Source="{Binding LogoImageSource}"
                       Visibility="{Binding HasLogoContent, Converter={StaticResource BoolToVisibilityConverter}}" />

                <!-- Loading State -->
                <StackPanel Visibility="{Binding IsLoading}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            Padding="32"
                            Spacing="16">
                    <ProgressRing IsActive="True"
                                  IsIndeterminate="True"
                                  Height="48"
                                  Width="48" />
                    <TextBlock Text="Loading..."
                               TextAlignment="Center"
                               FontSize="14" />
                </StackPanel>

                <!-- Error State -->
                <StackPanel Visibility="{Binding HasError}"
                            Padding="16"
                            Background="{ThemeResource SystemErrorBackgroundColor}"
                            CornerRadius="8"
                            Spacing="8">
                    <TextBlock Text="Error"
                               FontWeight="Bold"
                               Foreground="{ThemeResource SystemErrorTextForegroundColor}" />
                    <TextBlock Text="{Binding ErrorMessage}"
                               Foreground="{ThemeResource SystemErrorTextForegroundColor}"
                               TextWrapping="Wrap" />
                </StackPanel>

                <!-- Company Details -->
                <StackPanel Visibility="{Binding IsDataLoaded}"
                            Spacing="16">

                    <!-- Company Name -->
                    <TextBlock Text="{Binding Company.Name}"
                               FontSize="28"
                               FontWeight="Bold"
                               TextWrapping="Wrap" />

                    <!-- Company Status -->
                    <StackPanel Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                CornerRadius="8"
                                Padding="12"
                                Spacing="8">
                        <TextBlock Text="Status"
                                   FontSize="14"
                                   FontWeight="SemiBold"
                                   Foreground="{ThemeResource SystemBaseMediumColor}" />
                        <TextBlock Text="{Binding CompanyStatusDisplay}"
                                   FontSize="14"
                                   TextWrapping="Wrap" />
                    </StackPanel>

                    <!-- Founded Date -->
                    <StackPanel Visibility="{Binding Company.Founded, Converter={StaticResource ObjectToVisibilityConverter}}"
                                Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                CornerRadius="8"
                                Padding="12"
                                Spacing="8">
                        <TextBlock Text="Founded"
                                   FontSize="14"
                                   FontWeight="SemiBold"
                                   Foreground="{ThemeResource SystemBaseMediumColor}" />
                        <TextBlock Text="{Binding CompanyFoundedDateDisplay}"
                                   FontSize="14" />
                    </StackPanel>

                    <!-- Legal Name -->
                    <StackPanel Visibility="{Binding Company.LegalName, Converter={StaticResource StringToVisibilityConverter}}"
                                Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                CornerRadius="8"
                                Padding="12"
                                Spacing="8">
                        <TextBlock Text="Legal Name"
                                   FontSize="14"
                                   FontWeight="SemiBold"
                                   Foreground="{ThemeResource SystemBaseMediumColor}" />
                        <TextBlock Text="{Binding Company.LegalName}"
                                   FontSize="14" />
                    </StackPanel>

                    <!-- Country -->
                    <StackPanel Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                CornerRadius="8"
                                Padding="12">
                        <Grid ColumnSpacing="12">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <!-- Country Name and Label -->
                            <StackPanel Grid.Column="0"
                                        Spacing="4">
                                <TextBlock Text="Country"
                                           FontSize="14"
                                           FontWeight="SemiBold"
                                           Foreground="{ThemeResource SystemBaseMediumColor}" />
                                <TextBlock Text="{Binding Company.Country}"
                                           FontSize="14"
                                           VerticalAlignment="Center" />
                            </StackPanel>

                            <!-- Country Flag -->
                            <Image Grid.Column="1"
                                   Width="48"
                                   Height="32"
                                   Stretch="UniformToFill"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Right"
                                   Source="{Binding FlagImageSource}"
                                   Visibility="{Binding HasFlagContent, Converter={StaticResource BoolToVisibilityConverter}}" />
                        </Grid>
                    </StackPanel>

                    <!-- Address -->
                    <StackPanel Visibility="{Binding Company.Address, Converter={StaticResource StringToVisibilityConverter}}"
                                Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                CornerRadius="8"
                                Padding="12"
                                Spacing="8">
                        <TextBlock Text="Address"
                                   FontSize="14"
                                   FontWeight="SemiBold"
                                   Foreground="{ThemeResource SystemBaseMediumColor}" />
                        <TextBlock TextWrapping="Wrap">
                            <Run Text="{Binding Company.Address}" />
                            <Run Text="{Binding Company.City}" />
                            <Run Text="{Binding Company.PostalCode}" />
                            <Run Text="{Binding Company.Province}" />
                        </TextBlock>
                    </StackPanel>

                    <!-- Links Section -->
                    <StackPanel Spacing="8">
                        <TextBlock Text="Links"
                                   FontSize="14"
                                   FontWeight="SemiBold"
                                   Foreground="{ThemeResource SystemBaseMediumColor}" />

                        <!-- Website -->
                        <HyperlinkButton Visibility="{Binding Company.Website, Converter={StaticResource StringToVisibilityConverter}}"
                                         NavigateUri="{Binding Company.Website}">
                            <TextBlock Text="Website" />
                        </HyperlinkButton>

                        <!-- Twitter -->
                        <HyperlinkButton Visibility="{Binding Company.Twitter, Converter={StaticResource StringToVisibilityConverter}}"
                                         Click="OnTwitterClick">
                            <TextBlock Text="Twitter" />
                        </HyperlinkButton>

                        <!-- Facebook -->
                        <HyperlinkButton Visibility="{Binding Company.Facebook, Converter={StaticResource StringToVisibilityConverter}}"
                                         Click="OnFacebookClick">
                            <TextBlock Text="Facebook" />
                        </HyperlinkButton>
                    </StackPanel>

                    <!-- Logo Carousel Section -->
                    <StackPanel Visibility="{Binding HasMultipleLogos, Converter={StaticResource BoolToVisibilityConverter}}"
                                Spacing="8">
                        <TextBlock Text="Logo History"
                                   FontSize="14"
                                   FontWeight="SemiBold"
                                   Foreground="{ThemeResource SystemBaseMediumColor}" />

                        <!-- Logo Carousel -->
                        <controls:Carousel ItemsSource="{Binding CompanyLogos}"
                                           Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                           CornerRadius="8"
                                           ItemRotationY="45"
                                           TransitionDuration="400"
                                           Height="220">
                            <controls:Carousel.EasingFunction>
                                <CubicEase EasingMode="EaseOut" />
                            </controls:Carousel.EasingFunction>
                            <controls:Carousel.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel VerticalAlignment="Center"
                                               HorizontalAlignment="Center"
                                               Spacing="8"
                                               Padding="24">
                                        <!-- Logo Image -->
                                        <Image Source="{Binding LogoSource}"
                                               Width="120"
                                               Height="120"
                                               Stretch="Uniform"
                                               HorizontalAlignment="Center"
                                               VerticalAlignment="Center" />

                                        <!-- Year Label -->
                                        <TextBlock Text="{Binding Year, FallbackValue='Year Unknown'}"
                                                   FontSize="12"
                                                   TextAlignment="Center"
                                                   Foreground="{ThemeResource SystemBaseMediumColor}" />
                                    </StackPanel>
                                </DataTemplate>
                            </controls:Carousel.ItemTemplate>
                        </controls:Carousel>
                    </StackPanel>

                    <!-- Computers Section -->
                    <StackPanel Visibility="{Binding Computers.Count, Converter={StaticResource ZeroToVisibilityConverter}}"
                                Spacing="8">
                        <StackPanel Orientation="Horizontal"
                                    Spacing="8">
                            <TextBlock Text="Computers"
                                       FontSize="14"
                                       FontWeight="SemiBold"
                                       Foreground="{ThemeResource SystemBaseMediumColor}" />
                            <TextBlock Text="{Binding Computers.Count}"
                                       FontSize="14"
                                       FontWeight="Bold"
                                       Foreground="{ThemeResource SystemAccentColor}" />
                        </StackPanel>

                        <!-- Filter Box -->
                        <AutoSuggestBox PlaceholderText="Filter computers..."
                                        Text="{Binding ComputersFilterText, Mode=TwoWay}"
                                        BorderThickness="1"
                                        BorderBrush="{ThemeResource ControlElevationBorderBrush}" />

                        <!-- Scrollable Computers List -->
                        <ScrollViewer Height="200"
                                      BorderThickness="1"
                                      BorderBrush="{ThemeResource ControlElevationBorderBrush}"
                                      CornerRadius="8">
                            <ItemsRepeater ItemsSource="{Binding FilteredComputers}"
                                           Margin="0">
                                <ItemsRepeater.Layout>
                                    <StackLayout Spacing="4" />
                                </ItemsRepeater.Layout>
                                <ItemsRepeater.ItemTemplate>
                                    <DataTemplate>
                                        <Button Command="{Binding DataContext.NavigateToMachineCommand, ElementName=PageRoot}"
                                                CommandParameter="{Binding}"
                                                Padding="12,8"
                                                HorizontalAlignment="Stretch"
                                                HorizontalContentAlignment="Left">
                                            <TextBlock Text="{Binding Name}"
                                                       FontSize="12"
                                                       TextWrapping="Wrap" />
                                        </Button>
                                    </DataTemplate>
                                </ItemsRepeater.ItemTemplate>
                            </ItemsRepeater>
                        </ScrollViewer>
                    </StackPanel>

                    <!-- Consoles Section -->
                    <StackPanel Visibility="{Binding Consoles.Count, Converter={StaticResource ZeroToVisibilityConverter}}"
                                Spacing="8">
                        <StackPanel Orientation="Horizontal"
                                    Spacing="8">
                            <TextBlock Text="Consoles"
                                       FontSize="14"
                                       FontWeight="SemiBold"
                                       Foreground="{ThemeResource SystemBaseMediumColor}" />
                            <TextBlock Text="{Binding Consoles.Count}"
                                       FontSize="14"
                                       FontWeight="Bold"
                                       Foreground="{ThemeResource SystemAccentColor}" />
                        </StackPanel>

                        <!-- Filter Box -->
                        <AutoSuggestBox PlaceholderText="Filter consoles..."
                                        Text="{Binding ConsoelsFilterText, Mode=TwoWay}"
                                        BorderThickness="1"
                                        BorderBrush="{ThemeResource ControlElevationBorderBrush}" />

                        <!-- Scrollable Consoles List -->
                        <ScrollViewer Height="200"
                                      BorderThickness="1"
                                      BorderBrush="{ThemeResource ControlElevationBorderBrush}"
                                      CornerRadius="8">
                            <ItemsRepeater ItemsSource="{Binding FilteredConsoles}"
                                           Margin="0">
                                <ItemsRepeater.Layout>
                                    <StackLayout Spacing="4" />
                                </ItemsRepeater.Layout>
                                <ItemsRepeater.ItemTemplate>
                                    <DataTemplate>
                                        <Button Command="{Binding DataContext.NavigateToMachineCommand, ElementName=PageRoot}"
                                                CommandParameter="{Binding}"
                                                Padding="12,8"
                                                HorizontalAlignment="Stretch"
                                                HorizontalContentAlignment="Left">
                                            <TextBlock Text="{Binding Name}"
                                                       FontSize="12"
                                                       TextWrapping="Wrap" />
                                        </Button>
                                    </DataTemplate>
                                </ItemsRepeater.ItemTemplate>
                            </ItemsRepeater>
                        </ScrollViewer>
                    </StackPanel>

                </StackPanel>

            </StackPanel>
        </ScrollViewer>

    </Grid>

</Page>
