<UserControl x:Class="NexusShell.Views.LauncherOverlay"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:conv="clr-namespace:NexusShell.Converters"
             x:Name="Root"
             Background="{DynamicResource NexusShellOverlayScrimBrush}"
             Opacity="0"
             MouseDown="Overlay_OnMouseDown">
    <UserControl.Resources>
        <conv:BoolToVisibilityConverter x:Key="BoolToVisibility"/>
        <Style x:Key="NexusLauncherPinnedTileStyle" TargetType="Button">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="122"/>
            <Setter Property="Margin" Value="2,0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <StackPanel HorizontalAlignment="Center">
                            <Grid Width="58" Height="58" Margin="0,0,0,6">
                                <Border x:Name="TileGlow" VerticalAlignment="Bottom" HorizontalAlignment="Center"
                                        Width="54" Height="18" CornerRadius="10" Opacity="0" IsHitTestVisible="False">
                                    <Border.Background>
                                        <RadialGradientBrush GradientOrigin="0.5,1" Center="0.5,1" RadiusX="0.9" RadiusY="1">
                                            <GradientStop Color="#9000D9FF" Offset="0"/>
                                            <GradientStop Color="#0000D9FF" Offset="1"/>
                                        </RadialGradientBrush>
                                    </Border.Background>
                                    <Border.Effect>
                                        <BlurEffect Radius="18"/>
                                    </Border.Effect>
                                </Border>
                                <Border x:Name="Tile" Width="58" Height="58" CornerRadius="15"
                                        Background="{DynamicResource NexusShellLauncherTileBgBrush}"
                                        BorderBrush="{DynamicResource NexusShellLauncherTileBorderBrush}" BorderThickness="1">
                                    <Border.Effect>
                                        <DropShadowEffect Color="#000000" BlurRadius="8" ShadowDepth="2" Opacity="0.4"/>
                                    </Border.Effect>
                                    <Border Width="52" Height="52" CornerRadius="14"
                                            HorizontalAlignment="Center" VerticalAlignment="Center">
                                        <Border.Background>
                                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                                <GradientStop Color="#668A4FFF" Offset="0"/>
                                                <GradientStop Color="#4400D9FF" Offset="1"/>
                                            </LinearGradientBrush>
                                        </Border.Background>
                                        <TextBlock Text="{Binding IconPath, TargetNullValue=⬡, FallbackValue=⬡}"
                                                   FontSize="22" FontWeight="SemiBold"
                                                   Foreground="White" TextAlignment="Center"
                                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    </Border>
                                </Border>
                            </Grid>
                            <TextBlock Text="{Binding Name}" FontSize="11" FontWeight="Medium"
                                       Foreground="{DynamicResource NexusShellLauncherTileTextBrush}" TextTrimming="CharacterEllipsis"
                                       TextAlignment="Center" MaxWidth="96" LineHeight="14"
                                       TextWrapping="NoWrap"/>
                            <TextBlock Text="{Binding Category}" FontSize="9"
                                       Foreground="{DynamicResource NexusShellLabelBrush}" TextTrimming="CharacterEllipsis"
                                       TextAlignment="Center" MaxWidth="96" Opacity="0.72"/>
                            <TextBlock Text="{Binding StatusLabel}" FontSize="9" FontWeight="SemiBold"
                                       Foreground="#00D9FF" TextTrimming="CharacterEllipsis"
                                       TextAlignment="Center" MaxWidth="96" Opacity="0.88"/>
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="TileGlow" Property="Opacity" Value="1"/>
                                <Setter TargetName="Tile" Property="Background" Value="#1800D9FF"/>
                                <Setter TargetName="Tile" Property="BorderBrush" Value="#6600D9FF"/>
                                <Setter TargetName="Tile" Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect Color="#00D9FF" BlurRadius="22" ShadowDepth="4" Direction="270" Opacity="0.85"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <DataTrigger Binding="{Binding CanOpen}" Value="False">
                                <Setter Property="Opacity" Value="0.58"/>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="NexusLauncherRecentRowStyle" TargetType="Button">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="MinHeight" Value="54"/>
            <Setter Property="Padding" Value="4,4"/>
            <Setter Property="Margin" Value="0,1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Row" CornerRadius="14" Background="Transparent" Padding="{TemplateBinding Padding}">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Row" Property="Background" Value="#1800D9FF"/>
                                <Setter TargetName="Row" Property="BorderBrush" Value="#3300D9FF"/>
                                <Setter TargetName="Row" Property="BorderThickness" Value="1"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LauncherPanel"
          Width="664"
          Height="700"
          HorizontalAlignment="Center"
          VerticalAlignment="Bottom"
          Margin="0,48,0,68"
          MouseDown="Panel_OnMouseDown"
          RenderTransformOrigin="0.5,1">
        <Grid.RenderTransform>
            <TransformGroup>
                <ScaleTransform ScaleX="0.96" ScaleY="0.88"/>
                <TranslateTransform Y="36"/>
            </TransformGroup>
        </Grid.RenderTransform>
        <Grid.Clip>
            <StaticResource ResourceKey="NexusLauncherShellShape"/>
        </Grid.Clip>
        <Grid.Effect>
            <DropShadowEffect Color="#00D9FF" BlurRadius="48" ShadowDepth="0" Opacity="0.32"/>
        </Grid.Effect>

        <!-- Dock-matching glass stack -->
        <Grid Width="664" Height="700" IsHitTestVisible="False">
            <Grid.Clip>
                <StaticResource ResourceKey="NexusLauncherShellShape"/>
            </Grid.Clip>
            <Path Fill="{DynamicResource NexusShellDockShadowBrush}" Data="{StaticResource NexusLauncherShellShape}" Opacity="0.42">
                <Path.RenderTransform>
                    <TranslateTransform Y="3"/>
                </Path.RenderTransform>
                <Path.Effect>
                    <BlurEffect Radius="8"/>
                </Path.Effect>
            </Path>
            <Path Fill="{DynamicResource NexusDockElevatedGlassFillGradient}" Data="{StaticResource NexusLauncherShellShape}"/>
            <Path IsHitTestVisible="False" Opacity="0.55" Data="{StaticResource NexusLauncherShellShape}">
                <Path.Fill>
                    <DynamicResource ResourceKey="NexusDockInnerShadowGradient"/>
                </Path.Fill>
            </Path>
            <Path IsHitTestVisible="False" Opacity="0.45" Data="{StaticResource NexusLauncherShellShape}">
                <Path.Fill>
                    <LinearGradientBrush StartPoint="0.5,1" EndPoint="0.5,0">
                        <GradientStop Color="#40000000" Offset="0"/>
                        <GradientStop Color="#00000000" Offset="0.35"/>
                    </LinearGradientBrush>
                </Path.Fill>
            </Path>
            <Path IsHitTestVisible="False" Opacity="0.75" Data="{StaticResource NexusLauncherShellShape}">
                <Path.Fill>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,0.38">
                        <GradientStop Color="#28FFFFFF" Offset="0"/>
                        <GradientStop Color="#00FFFFFF" Offset="1"/>
                    </LinearGradientBrush>
                </Path.Fill>
            </Path>
            <Path IsHitTestVisible="False" Opacity="0.32" Data="{StaticResource NexusLauncherShellShape}">
                <Path.Fill>
                    <DynamicResource ResourceKey="NexusDockSheenGradient"/>
                </Path.Fill>
            </Path>
            <Path Stroke="{DynamicResource NexusDockRimGradient}" StrokeThickness="1.3"
                  StrokeLineJoin="Round" Fill="Transparent"
                  Data="{StaticResource NexusLauncherShellRimShape}" Opacity="0.9"/>
        </Grid>

        <!-- Content inset within plateau width (59–605) -->
        <Grid Margin="68,18,68,14">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <!-- Search -->
            <Border Grid.Row="0" Height="52" CornerRadius="18" Margin="0,0,0,18"
                    Background="{DynamicResource NexusShellLauncherSearchBgBrush}" BorderThickness="1">
                <Border.BorderBrush>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                        <GradientStop Color="#5500D9FF" Offset="0"/>
                        <GradientStop Color="#338A4FFF" Offset="0.5"/>
                        <GradientStop Color="#5500D9FF" Offset="1"/>
                    </LinearGradientBrush>
                </Border.BorderBrush>
                <Border.Effect>
                    <DropShadowEffect Color="#00D9FF" BlurRadius="16" ShadowDepth="0" Opacity="0.22"/>
                </Border.Effect>
                <Grid Margin="12,0,14,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Image Source="{DynamicResource NexusLauncherLogo}"
                           Width="32" Height="32" Margin="0,0,10,0"
                           RenderOptions.BitmapScalingMode="HighQuality"/>
                    <Grid Grid.Column="1">
                        <TextBlock Text="Søg apps, filer, kommandoer…"
                                   VerticalAlignment="Center"
                                   Foreground="{DynamicResource NexusShellLabelBrush}" FontSize="14" Opacity="0.75"
                                   IsHitTestVisible="False">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding SearchText, ElementName=Root}" Value="">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                        <TextBox x:Name="SearchBox"
                                 Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
                                 Background="Transparent" BorderThickness="0"
                                 Foreground="{DynamicResource NexusTextPrimaryBrush}" FontSize="14"
                                 VerticalContentAlignment="Center"
                                 CaretBrush="#00D9FF"/>
                    </Grid>
                </Grid>
            </Border>

            <TextBlock Grid.Row="1" Text="{Binding PinnedSectionTitle}"
                       FontSize="11" FontWeight="SemiBold"
                       Foreground="{DynamicResource NexusShellLabelBrush}"
                       Margin="2,0,0,10" Opacity="0.92"/>

            <ScrollViewer Grid.Row="2" MaxHeight="272" VerticalScrollBarVisibility="Auto" Margin="0,0,0,12">
                <ItemsControl ItemsSource="{Binding PinnedApps}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="5"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Button Style="{StaticResource NexusLauncherPinnedTileStyle}"
                                    Command="{Binding DataContext.LaunchCommand, ElementName=Root}"
                                    CommandParameter="{Binding}"
                                    ToolTip="{Binding Description}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>

            <TextBlock Grid.Row="3" Text="Seneste"
                       FontSize="11" FontWeight="SemiBold"
                       Foreground="{DynamicResource NexusShellLabelBrush}"
                       Margin="2,0,0,8" Opacity="0.92"/>

            <StackPanel Grid.Row="4" Margin="0,0,0,4">
                <ItemsControl ItemsSource="{Binding RecentDocuments}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Button Style="{StaticResource NexusLauncherRecentRowStyle}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Border Width="44" Height="44" CornerRadius="13" Margin="0,0,12,0"
                                            Background="{DynamicResource NexusShellLauncherTileBgBrush}"
                                            BorderBrush="{DynamicResource NexusShellLauncherTileBorderBrush}" BorderThickness="1">
                                        <Border Width="38" Height="38" CornerRadius="11"
                                                HorizontalAlignment="Center" VerticalAlignment="Center">
                                            <Border.Background>
                                                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                                    <GradientStop Color="#558A4FFF" Offset="0"/>
                                                    <GradientStop Color="#3300D9FF" Offset="1"/>
                                                </LinearGradientBrush>
                                            </Border.Background>
                                            <TextBlock Text="{Binding Glyph}" FontSize="18"
                                                       HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Border>
                                    </Border>
                                    <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="0,0,8,0">
                                        <TextBlock Text="{Binding Name}" FontSize="13" FontWeight="SemiBold"
                                                   Foreground="{DynamicResource NexusTextPrimaryBrush}" TextTrimming="CharacterEllipsis"/>
                                        <TextBlock Text="{Binding Category}" FontSize="11"
                                                   Foreground="{DynamicResource NexusShellLabelBrush}" Opacity="0.82" Margin="0,2,0,0"/>
                                    </StackPanel>
                                    <TextBlock Grid.Column="2" Text="{Binding Time}" FontSize="11"
                                               Foreground="{DynamicResource NexusShellLabelBrush}" Opacity="0.7" VerticalAlignment="Center"
                                               MinWidth="44" TextAlignment="Right"/>
                                </Grid>
                            </Button>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
                <Button Command="{Binding OpenFilesCommand}"
                        Visibility="{Binding CanOpenFiles, Converter={StaticResource BoolToVisibility}}"
                        HorizontalAlignment="Stretch" Cursor="Hand" Margin="2,6,2,0"
                        Background="Transparent" BorderThickness="0" Padding="8,10">
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Border x:Name="Root" CornerRadius="12" Padding="{TemplateBinding Padding}"
                                    Background="#0AFFFFFF" BorderBrush="#18FFFFFF" BorderThickness="1">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                    <Path Data="M4,6 L10,6 L12,8 L20,8 L20,18 L4,18 Z" Fill="#00D9FF"
                                          Stretch="Uniform" Width="14" Height="14" Margin="0,0,8,0" Opacity="0.9"/>
                                    <TextBlock Text="Se flere i Nexus Files" FontSize="12" FontWeight="SemiBold"
                                               Foreground="{DynamicResource NexusShellLabelBrush}"/>
                                </StackPanel>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="Root" Property="Background" Value="#1800D9FF"/>
                                    <Setter TargetName="Root" Property="BorderBrush" Value="#4400D9FF"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </StackPanel>

            <!-- Bruger + strøm -->
            <Border Grid.Row="5" Padding="0,10,0,0"
                    BorderBrush="{DynamicResource NexusDockZoneDividerBrush}" BorderThickness="0,1,0,0">
                <Border Background="{DynamicResource NexusShellLauncherFooterBgBrush}" CornerRadius="14" Padding="12,10" Margin="0,0,0,2">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Border Width="40" Height="40" CornerRadius="20"
                                BorderBrush="#408A4FFF" BorderThickness="1">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                    <GradientStop Color="#64748B" Offset="0"/>
                                    <GradientStop Color="#334155" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <TextBlock Text="{Binding UserInitials}" FontWeight="SemiBold" FontSize="12"
                                       Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="10,0,0,0">
                            <TextBlock Text="{Binding UserDisplayName}" FontSize="13" FontWeight="SemiBold"
                                       Foreground="{DynamicResource NexusTextPrimaryBrush}"/>
                            <TextBlock Text="{Binding UserRole}" FontSize="10"
                                       Foreground="{DynamicResource NexusShellLabelBrush}"/>
                        </StackPanel>
                        <StackPanel Grid.Column="2" Orientation="Horizontal" VerticalAlignment="Center">
                            <Button Style="{StaticResource NexusIconButtonStyle}" Padding="7"
                                    ToolTip="Lås" Command="{Binding LockCommand}">
                                <Path Data="M8,11 L8,8 C8,5.8 9.8,4 12,4 C14.2,4 16,5.8 16,8 L16,11 M6,11 L18,11 L18,20 L6,20 Z"
                                      Stroke="#8CA3C7" StrokeThickness="1.4" Stretch="Uniform" Width="15" Height="15"/>
                            </Button>
                            <Button Style="{StaticResource NexusIconButtonStyle}" Padding="7"
                                    ToolTip="Genstart" Command="{Binding RestartCommand}">
                                <Path Data="M12,4 A8,8 0 1,1 11.2,19.2 M12,8 L12,4 M12,8 L15,11"
                                      Stroke="#8CA3C7" StrokeThickness="1.4" Stretch="Uniform" Width="15" Height="15"
                                      StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                            </Button>
                            <Button Style="{StaticResource NexusIconButtonStyle}" Padding="7"
                                    ToolTip="Sluk" Command="{Binding ShutdownCommand}">
                                <Path Data="M12,3 L12,12 M8,8 C8,5.8 9.8,4 12,4 C14.2,4 16,5.8 16,8 C16,10.2 14.2,12 12,12"
                                      Stroke="#8CA3C7" StrokeThickness="1.4" Stretch="Uniform" Width="15" Height="15"
                                      StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                            </Button>
                            <Button Style="{StaticResource NexusIconButtonStyle}" Padding="7"
                                    ToolTip="Log ud" Command="{Binding LogoutCommand}">
                                <Path Data="M14,4 L14,8 L18,8 L18,16 L14,16 L14,20 L6,12 Z M10,12 L18,12"
                                      Fill="#8CA3C7" Stretch="Uniform" Width="15" Height="15"/>
                            </Button>
                        </StackPanel>
                    </Grid>
                </Border>
            </Border>
        </Grid>
    </Grid>
</UserControl>
