<UserControl x:Class="NexusShell.Views.NexusStoreView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:conv="clr-namespace:NexusShell.Converters"
             FontFamily="Segoe UI Variable Display, Segoe UI">
    <UserControl.Resources>
        <conv:BoolToVisibilityConverter x:Key="BoolToVisibility"/>

        <Style x:Key="NsIconPath" TargetType="Path">
            <Setter Property="Stroke" Value="#00D9FF"/>
            <Setter Property="StrokeThickness" Value="1.5"/>
            <Setter Property="Fill" Value="Transparent"/>
            <Setter Property="Stretch" Value="Uniform"/>
            <Setter Property="Width" Value="16"/>
            <Setter Property="Height" Value="16"/>
            <Setter Property="StrokeStartLineCap" Value="Round"/>
            <Setter Property="StrokeEndLineCap" Value="Round"/>
            <Setter Property="StrokeLineJoin" Value="Round"/>
        </Style>

        <Style x:Key="NsNavButton" TargetType="Button">
            <Setter Property="Height" Value="38"/>
            <Setter Property="Margin" Value="0,0,0,4"/>
            <Setter Property="Padding" Value="12,0"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Foreground" Value="#B8C8E0"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Root" CornerRadius="10" Background="{TemplateBinding Background}"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Root" Property="Background" Value="#2200D9FF"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="NsGhostButton" TargetType="Button">
            <Setter Property="Height" Value="36"/>
            <Setter Property="Padding" Value="16,0"/>
            <Setter Property="Margin" Value="0,6,0,0"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Foreground" Value="#E8F0FF"/>
            <Setter Property="Background" Value="#14081428"/>
            <Setter Property="BorderBrush" Value="#33FFFFFF"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Root" CornerRadius="12"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Root" Property="Background" Value="#2800D9FF"/>
                                <Setter TargetName="Root" Property="BorderBrush" Value="#6600D9FF"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" Value="0.4"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="NsPrimaryButton" TargetType="Button" BasedOn="{StaticResource NsGhostButton}">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Foreground" Value="#FFFFFF"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Root" CornerRadius="12" BorderThickness="0" Padding="{TemplateBinding Padding}">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                    <GradientStop Color="#FF00D9FF" Offset="0"/>
                                    <GradientStop Color="#FF8A4FFF" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Root" Property="Opacity" Value="0.9"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" Value="0.35"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <DataTemplate x:Key="NsPackageCardTemplate">
            <Button Command="{Binding DataContext.SelectPackageCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                    CommandParameter="{Binding}"
                    Cursor="Hand" Background="Transparent" BorderThickness="0" Padding="0" Margin="0,0,10,10">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Card" Width="148" Height="148" CornerRadius="14"
                                Background="#18081428" BorderBrush="#3300D9FF" BorderThickness="1" Padding="12">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Border Width="40" Height="40" CornerRadius="12" HorizontalAlignment="Left">
                                    <Border.Background>
                                        <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                            <GradientStop Color="#448A4FFF" Offset="0"/>
                                            <GradientStop Color="#3300D9FF" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Border.Background>
                                    <Path Data="{Binding IconPath}" Style="{StaticResource NsIconPath}"
                                          Width="20" Height="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Border>
                                <StackPanel Grid.Row="1" Margin="0,10,0,0" VerticalAlignment="Top">
                                    <TextBlock Text="{Binding Manifest.Name}" Foreground="#FFFFFF" FontWeight="SemiBold"
                                               FontSize="13" TextTrimming="CharacterEllipsis"/>
                                    <TextBlock Text="{Binding Manifest.Version, StringFormat=v{0}}" Foreground="#8CA3C7"
                                               FontSize="10.5" Margin="0,2,0,0"/>
                                    <TextBlock Text="{Binding Manifest.Category}" Foreground="#6B84A8"
                                               FontSize="10" Margin="0,2,0,0" TextTrimming="CharacterEllipsis"/>
                                </StackPanel>
                                <TextBlock Grid.Row="2" Text="{Binding StatusLabel}" Foreground="#00D9FF"
                                           FontSize="10" TextTrimming="CharacterEllipsis"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Card" Property="BorderBrush" Value="#8800D9FF"/>
                                <Setter TargetName="Card" Property="Background" Value="#2400D9FF"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </DataTemplate>
    </UserControl.Resources>

    <Grid Margin="0,0,0,4">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="280"/>
        </Grid.ColumnDefinitions>

        <!-- Left nav -->
        <Border Grid.Column="0" Margin="0,0,12,0" Padding="8,4,8,8"
                Background="#12081428" CornerRadius="14" BorderBrush="#2200D9FF" BorderThickness="1">
            <DockPanel LastChildFill="True">
                <StackPanel DockPanel.Dock="Bottom" Margin="4,12,4,4">
                    <TextBlock Text="Shell" Foreground="#6B84A8" FontSize="10" Margin="4,0,0,4"/>
                    <TextBlock Text="{Binding ShellVersionLabel}" Foreground="#8CA3C7" FontSize="10.5" Margin="4,0,0,2"/>
                    <TextBlock Text="{Binding UpdateChannelLabel}" Foreground="#00D9FF" FontSize="10" Margin="4,0,0,0"
                               TextTrimming="CharacterEllipsis"/>
                </StackPanel>
                <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
                    <ItemsControl ItemsSource="{Binding NavItems}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Button Style="{StaticResource NsNavButton}"
                                        Command="{Binding DataContext.SelectNavCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                        CommandParameter="{Binding}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="22"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Path Data="{Binding IconPath}" Style="{StaticResource NsIconPath}"/>
                                        <TextBlock Grid.Column="1" Text="{Binding Label}" Margin="8,0,0,0"
                                                   VerticalAlignment="Center" FontSize="12.5"/>
                                        <Border Grid.Column="2" MinWidth="18" Height="18" CornerRadius="9"
                                                Background="#448A4FFF" Padding="5,0"
                                                Visibility="{Binding HasBadge, Converter={StaticResource BoolToVisibility}}">
                                            <TextBlock Text="{Binding BadgeCount}" Foreground="#FFFFFF"
                                                       FontSize="10" FontWeight="SemiBold"
                                                       HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Border>
                                    </Grid>
                                </Button>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>
            </DockPanel>
        </Border>

        <!-- Center catalog -->
        <Border Grid.Column="1" Background="#10081428" CornerRadius="14" BorderBrush="#2200D9FF" BorderThickness="1" Padding="14">
            <DockPanel LastChildFill="True">
                <Grid DockPanel.Dock="Top" Margin="0,0,0,12">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="220"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel>
                        <TextBlock Text="{Binding SectionTitle}" Foreground="#FFFFFF" FontSize="18" FontWeight="SemiBold"/>
                        <TextBlock Text="{Binding StatusMessage}" Foreground="#8CA3C7" FontSize="11" Margin="0,4,0,0"/>
                    </StackPanel>
                    <Border Grid.Column="1" CornerRadius="10" Background="#18081428" BorderBrush="#3300D9FF" BorderThickness="1" Padding="10,6">
                        <DockPanel>
                            <Path DockPanel.Dock="Left" Data="M10,4 A6,6 0 1,0 10,16 A6,6 0 1,0 10,4 M16,16 L20,20"
                                  Style="{StaticResource NsIconPath}" Margin="0,0,8,0"/>
                            <TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
                                     Background="Transparent" BorderThickness="0" Foreground="#E8F0FF"
                                     CaretBrush="#00D9FF" FontSize="12"/>
                        </DockPanel>
                    </Border>
                </Grid>

                <!-- Home sections -->
                <ScrollViewer Visibility="{Binding IsHomeSection, Converter={StaticResource BoolToVisibility}}"
                              VerticalScrollBarVisibility="Auto">
                    <ItemsControl ItemsSource="{Binding HomeSections}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,18">
                                    <TextBlock Text="{Binding Title}" Foreground="#00D9FF" FontSize="14"
                                               FontWeight="SemiBold" Margin="0,0,0,8"/>
                                    <ItemsControl ItemsSource="{Binding Packages}" ItemTemplate="{StaticResource NsPackageCardTemplate}">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <WrapPanel/>
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                    </ItemsControl>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>

                <!-- History -->
                <ScrollViewer Visibility="{Binding IsHistorySection, Converter={StaticResource BoolToVisibility}}"
                              VerticalScrollBarVisibility="Auto">
                    <ItemsControl ItemsSource="{Binding HistoryItems}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Border Margin="0,0,0,8" Padding="12" CornerRadius="10"
                                        Background="#18081428" BorderBrush="#2200D9FF" BorderThickness="1">
                                    <StackPanel>
                                        <TextBlock Text="{Binding PackageName}" Foreground="#FFFFFF" FontWeight="SemiBold"/>
                                        <TextBlock Foreground="#8CA3C7" FontSize="11" Margin="0,4,0,0">
                                            <Run Text="{Binding Action}"/>
                                            <Run Text=" · "/>
                                            <Run Text="{Binding At, StringFormat={}{0:g}}"/>
                                        </TextBlock>
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>

                <!-- Grid catalog -->
                <ScrollViewer VerticalScrollBarVisibility="Auto">
                    <ScrollViewer.Style>
                        <Style TargetType="ScrollViewer">
                            <Setter Property="Visibility" Value="Visible"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsHomeSection}" Value="True">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IsHistorySection}" Value="True">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ScrollViewer.Style>
                    <ItemsControl ItemsSource="{Binding CatalogItems}" ItemTemplate="{StaticResource NsPackageCardTemplate}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </ScrollViewer>
            </DockPanel>
        </Border>

        <!-- Right details -->
        <Border Grid.Column="2" Margin="12,0,0,0" Background="#12081428" CornerRadius="14"
                BorderBrush="#3300D9FF" BorderThickness="1" Padding="16">
            <DockPanel LastChildFill="True">
                <StackPanel DockPanel.Dock="Bottom">
                    <ProgressBar Height="4" Margin="0,0,0,8" Minimum="0" Maximum="100"
                                 Value="{Binding OperationProgress, Mode=OneWay}"
                                 IsIndeterminate="{Binding IsOperationIndeterminate, Mode=OneWay}"
                                 Visibility="{Binding IsOperationInProgress, Converter={StaticResource BoolToVisibility}}"/>
                    <Button Content="{Binding InstallActionLabel}" Style="{StaticResource NsPrimaryButton}"
                            Command="{Binding InstallCommand}"/>
                    <Button Content="Åbn" Style="{StaticResource NsGhostButton}" Command="{Binding OpenCommand}"/>
                    <Button Content="{Binding UpdateActionLabel}" Style="{StaticResource NsGhostButton}"
                            Command="{Binding UpdateCommand}"/>
                    <Button Content="Afinstaller" Style="{StaticResource NsGhostButton}"
                            Command="{Binding RemoveCommand}"/>
                    <Button Content="{Binding FavoriteLabel}" Style="{StaticResource NsGhostButton}"
                            Command="{Binding ToggleFavoriteCommand}"/>
                </StackPanel>

                <ScrollViewer VerticalScrollBarVisibility="Auto">
                    <StackPanel>
                        <Border Width="56" Height="56" CornerRadius="16" Margin="0,0,0,12" HorizontalAlignment="Left">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                    <GradientStop Color="#668A4FFF" Offset="0"/>
                                    <GradientStop Color="#4400D9FF" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <Path Data="{Binding SelectedPackage.IconPath, FallbackValue=''}"
                                  Style="{StaticResource NsIconPath}" Width="28" Height="28"
                                  HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <TextBlock Text="{Binding DetailName}" Foreground="#FFFFFF" FontSize="17" FontWeight="SemiBold"
                                   TextWrapping="Wrap"/>
                        <TextBlock Text="{Binding DetailVersion}" Foreground="#00D9FF" FontSize="12" Margin="0,4,0,0"/>
                        <TextBlock Text="{Binding DetailInstalledVersion}" Foreground="#8CA3C7" FontSize="11" Margin="0,2,0,0"/>
                        <TextBlock Text="{Binding DetailStatus}" Foreground="#8CA3C7" FontSize="11" Margin="0,2,0,12"/>
                        <TextBlock Text="Udgiver" Foreground="#6B84A8" FontSize="10"/>
                        <TextBlock Text="{Binding DetailPublisher}" Foreground="#E8F0FF" FontSize="12" Margin="0,2,0,10"/>
                        <TextBlock Text="Kategori" Foreground="#6B84A8" FontSize="10"/>
                        <TextBlock Text="{Binding DetailCategory}" Foreground="#E8F0FF" FontSize="12" Margin="0,2,0,10"/>
                        <TextBlock Text="Størrelse" Foreground="#6B84A8" FontSize="10"/>
                        <TextBlock Text="{Binding DetailSize}" Foreground="#E8F0FF" FontSize="12" Margin="0,2,0,10"/>
                        <TextBlock Text="Beskrivelse" Foreground="#6B84A8" FontSize="10"/>
                        <TextBlock Text="{Binding DetailDescription}" Foreground="#B8C8E0" FontSize="12"
                                   TextWrapping="Wrap" Margin="0,2,0,10"/>
                        <TextBlock Text="Ændringslog" Foreground="#6B84A8" FontSize="10"/>
                        <TextBlock Text="{Binding DetailChangelog}" Foreground="#8CA3C7" FontSize="11"
                                   TextWrapping="Wrap" Margin="0,2,0,0"/>
                    </StackPanel>
                </ScrollViewer>
            </DockPanel>
        </Border>
    </Grid>
</UserControl>
