<UserControl x:Class="NexusShell.Views.NexusBrowserView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:browser="clr-namespace:NexusShell.Models.Browser"
             Background="Transparent"
             FontFamily="Segoe UI Variable Display, Segoe UI">
    <UserControl.Resources>
        <LinearGradientBrush x:Key="NbAccentGradient" StartPoint="0,0" EndPoint="1,1">
            <GradientStop Color="#668A4FFF" Offset="0"/>
            <GradientStop Color="#4400D9FF" Offset="1"/>
        </LinearGradientBrush>

        <Style x:Key="NbIconButton" TargetType="Button">
            <Setter Property="Width" Value="36"/>
            <Setter Property="Height" Value="36"/>
            <Setter Property="Margin" Value="0,0,6,0"/>
            <Setter Property="Cursor" Value="Hand"/>
            <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}">
                            <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.32"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="NbTabCapsule" TargetType="Button">
            <Setter Property="Height" Value="34"/>
            <Setter Property="MinWidth" Value="120"/>
            <Setter Property="MaxWidth" Value="200"/>
            <Setter Property="Margin" Value="0,0,8,0"/>
            <Setter Property="Padding" Value="12,0,8,0"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Foreground" Value="#8CA3C7"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Background" Value="#12081428"/>
            <Setter Property="BorderBrush" Value="#22FFFFFF"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Root" CornerRadius="17"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{TemplateBinding Tag}"
                                           VerticalAlignment="Center" TextTrimming="CharacterEllipsis"
                                           Foreground="{TemplateBinding Foreground}"/>
                                <ContentPresenter Grid.Column="1" Margin="6,0,0,0"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Root" Property="BorderBrush" Value="#5500D9FF"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="NbTabCapsuleActive" TargetType="Button" BasedOn="{StaticResource NbTabCapsule}">
            <Setter Property="Foreground" Value="#FFFFFF"/>
            <Setter Property="Background" Value="#338A4FFF"/>
            <Setter Property="BorderBrush" Value="#7700D9FF"/>
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect Color="#8A4FFF" BlurRadius="14" ShadowDepth="0" Opacity="0.45"/>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Tab strip -->
        <Border Grid.Row="0" Padding="12,10,12,6" Background="#08081428">
            <DockPanel LastChildFill="False">
                <ScrollViewer DockPanel.Dock="Left" HorizontalScrollBarVisibility="Auto"
                              VerticalScrollBarVisibility="Disabled" MaxHeight="40">
                    <ItemsControl ItemsSource="{Binding Tabs}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="{x:Type browser:BrowserTab}">
                                <Border CornerRadius="17" Height="34" MinWidth="120" MaxWidth="200"
                                        Margin="0,0,8,0" Padding="12,0,8,0" Cursor="Hand"
                                        Background="#12081428" BorderBrush="#22FFFFFF" BorderThickness="1">
                                    <Border.Style>
                                        <Style TargetType="Border">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsActive}" Value="True">
                                                    <Setter Property="Background" Value="#338A4FFF"/>
                                                    <Setter Property="BorderBrush" Value="#7700D9FF"/>
                                                    <Setter Property="Effect">
                                                        <Setter.Value>
                                                            <DropShadowEffect Color="#8A4FFF" BlurRadius="14" ShadowDepth="0" Opacity="0.45"/>
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Border.Style>
                                    <Border.InputBindings>
                                        <MouseBinding Gesture="LeftClick"
                                                      Command="{Binding DataContext.SelectTabCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                                      CommandParameter="{Binding}"/>
                                    </Border.InputBindings>
                                    <DockPanel LastChildFill="True">
                                        <Button DockPanel.Dock="Right" Content="×" FontSize="14" Foreground="#8CA3C7"
                                                Width="22" Height="22" Padding="0"
                                                Background="Transparent" BorderThickness="0" Cursor="Hand"
                                                Command="{Binding DataContext.CloseTabCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                                CommandParameter="{Binding}"/>
                                        <TextBlock Text="{Binding Title}" VerticalAlignment="Center"
                                                   TextTrimming="CharacterEllipsis" FontSize="12" FontWeight="SemiBold">
                                            <TextBlock.Style>
                                                <Style TargetType="TextBlock">
                                                    <Setter Property="Foreground" Value="#8CA3C7"/>
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsActive}" Value="True">
                                                            <Setter Property="Foreground" Value="#FFFFFF"/>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </TextBlock.Style>
                                        </TextBlock>
                                    </DockPanel>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>
                <Button DockPanel.Dock="Right" Style="{StaticResource NbIconButton}" Margin="8,0,0,0"
                        Command="{Binding NewTabCommand}" ToolTip="Ny fane">
                    <Path Data="M12 5v14M5 12h14" Stroke="#00D9FF" StrokeThickness="1.8"
                          Stretch="Uniform" Width="14" Height="14"
                          StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                </Button>
            </DockPanel>
        </Border>

        <!-- Navigation bar -->
        <Border Grid.Row="1" Padding="12,8" Background="#0A081428">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <StackPanel Grid.Column="0" Orientation="Horizontal">
                    <Button Style="{StaticResource NbIconButton}" Command="{Binding BackCommand}" ToolTip="Tilbage">
                        <Path Data="M15 18l-6-6 6-6" Stroke="#E8F0FF" StrokeThickness="1.8"
                              Stretch="Uniform" Width="14" Height="14"
                              StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                    </Button>
                    <Button Style="{StaticResource NbIconButton}" Command="{Binding ForwardCommand}" ToolTip="Frem">
                        <Path Data="M9 18l6-6-6-6" Stroke="#E8F0FF" StrokeThickness="1.8"
                              Stretch="Uniform" Width="14" Height="14"
                              StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                    </Button>
                    <Button Style="{StaticResource NbIconButton}" Command="{Binding RefreshCommand}" ToolTip="Opdater">
                        <Path Data="M21 12a9 9 0 1 1-3-6.7M21 3v6h-6" Stroke="#E8F0FF" StrokeThickness="1.8"
                              Stretch="Uniform" Width="14" Height="14"
                              StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                    </Button>
                    <Button Style="{StaticResource NbIconButton}" Command="{Binding HomeCommand}" ToolTip="Hjem">
                        <Path Data="M3 10.5L12 3l9 7.5V20a1 1 0 0 1-1 1h-5v-6H9v6H4a1 1 0 0 1-1-1z"
                              Stroke="#E8F0FF" StrokeThickness="1.6" Fill="Transparent"
                              Stretch="Uniform" Width="14" Height="14"
                              StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                    </Button>
                </StackPanel>

                <Border Grid.Column="1" Margin="8,0" CornerRadius="16" Background="#14081428"
                        BorderBrush="#4400D9FF" BorderThickness="1" Padding="12,0">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBox Grid.Column="0" Text="{Binding AddressBarText, UpdateSourceTrigger=PropertyChanged}"
                                 Background="Transparent" BorderThickness="0" Foreground="#E8F0FF"
                                 FontSize="13" VerticalContentAlignment="Center" CaretBrush="#00D9FF">
                            <TextBox.InputBindings>
                                <KeyBinding Key="Enter" Command="{Binding GoCommand}"/>
                            </TextBox.InputBindings>
                        </TextBox>
                        <Button Grid.Column="1" Background="Transparent" BorderThickness="0" Cursor="Hand"
                                Command="{Binding GoCommand}" ToolTip="Gå">
                            <Path Data="M5 12h14M13 6l6 6-6 6" Stroke="#00D9FF" StrokeThickness="1.8"
                                  Stretch="Uniform" Width="14" Height="14"
                                  StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                        </Button>
                    </Grid>
                </Border>

                <StackPanel Grid.Column="2" Orientation="Horizontal">
                    <Border CornerRadius="12" Padding="10,6" Margin="0,0,6,0"
                            Background="#14081428" BorderBrush="#33FFFFFF" BorderThickness="1">
                        <StackPanel Orientation="Horizontal">
                            <Ellipse Width="8" Height="8" Margin="0,0,6,0" VerticalAlignment="Center">
                                <Ellipse.Style>
                                    <Style TargetType="Ellipse">
                                        <Setter Property="Fill" Value="#00D9FF"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding SecurityState}" Value="{x:Static browser:BrowserSecurityState.NotSecure}">
                                                <Setter Property="Fill" Value="#FFB347"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding SecurityState}" Value="{x:Static browser:BrowserSecurityState.Local}">
                                                <Setter Property="Fill" Value="#8A4FFF"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding SecurityState}" Value="{x:Static browser:BrowserSecurityState.Loading}">
                                                <Setter Property="Fill" Value="#8CA3C7"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Ellipse.Style>
                            </Ellipse>
                            <TextBlock Text="{Binding SecurityLabel}" Foreground="#8CA3C7" FontSize="11" FontWeight="SemiBold"/>
                        </StackPanel>
                    </Border>
                    <Button Style="{StaticResource NbIconButton}" Command="{Binding ToggleFavoriteCommand}" ToolTip="Favorit">
                        <Path Data="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"
                              Stretch="Uniform" Width="14" Height="14">
                            <Path.Style>
                                <Style TargetType="Path">
                                    <Setter Property="Stroke" Value="#8CA3C7"/>
                                    <Setter Property="Fill" Value="Transparent"/>
                                    <Setter Property="StrokeThickness" Value="1.6"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsFavorite}" Value="True">
                                            <Setter Property="Fill" Value="#FFD166"/>
                                            <Setter Property="Stroke" Value="#FFD166"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Path.Style>
                        </Path>
                    </Button>
                    <Button Style="{StaticResource NbIconButton}" Command="{Binding DownloadsCommand}" ToolTip="Downloads">
                        <Path Data="M12 3v12M7 10l5 5 5-5M5 21h14" Stroke="#E8F0FF" StrokeThickness="1.8"
                              Stretch="Uniform" Width="14" Height="14"
                              StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                    </Button>
                    <Button Style="{StaticResource NbIconButton}" Command="{Binding MenuCommand}" ToolTip="Menu">
                        <Path Data="M4 7h16M4 12h16M4 17h16" Stroke="#E8F0FF" StrokeThickness="1.8"
                              Stretch="Uniform" Width="14" Height="14"
                              StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
                    </Button>
                </StackPanel>
            </Grid>
        </Border>

        <!-- Status / blocked message -->
        <Border Grid.Row="2" Padding="12,0,12,8"
                Visibility="{Binding HasBlockedMessage, Converter={StaticResource BoolToVisibility}}">
            <Border CornerRadius="12" Padding="12,8" Background="#33FF4D6D" BorderBrush="#88FF4D6D" BorderThickness="1">
                <TextBlock Text="{Binding BlockedMessage}" Foreground="#FFD6DE" FontSize="12" FontWeight="SemiBold"/>
            </Border>
        </Border>

        <Border Grid.Row="2" Padding="12,0,12,8"
                Visibility="{Binding ShowDownloadsMessage, Converter={StaticResource BoolToVisibility}}">
            <Border CornerRadius="12" Padding="12,8" Background="#2200D9FF" BorderBrush="#6600D9FF" BorderThickness="1">
                <DockPanel>
                    <TextBlock Text="{Binding StatusMessage}" Foreground="#E8F0FF" FontSize="12"/>
                    <Button DockPanel.Dock="Right" Content="×" Command="{Binding DismissMessageCommand}"
                            Background="Transparent" BorderThickness="0" Foreground="#8CA3C7" Cursor="Hand"/>
                </DockPanel>
            </Border>
        </Border>

        <!-- WebView host -->
        <Border Grid.Row="3" Margin="12,0,12,12" CornerRadius="18"
                Background="#08081428" BorderBrush="#3300D9FF" BorderThickness="1" ClipToBounds="True">
            <Grid x:Name="WebViewHost"/>
        </Border>
    </Grid>
</UserControl>
