<UserControl x:Class="DeviceEditModule.Views.DialogMangerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
             mc:Ignorable="d"
             prism:ViewModelLocator.AutoWireViewModel="True"
             Height="900" Width="900">

    <!-- 此窗口独立开启 ShowInTaskbar，以支持最小化后在任务栏恢复 -->
    <prism:Dialog.WindowStyle>
        <Style TargetType="Window">
            <Setter Property="WindowStyle"        Value="None"/>
            <Setter Property="ResizeMode"         Value="NoResize"/>
            <Setter Property="ShowInTaskbar"      Value="True"/>
            <Setter Property="AllowsTransparency" Value="True"/>
            <Setter Property="Background"         Value="Transparent"/>
            <Setter Property="SizeToContent"      Value="WidthAndHeight"/>
        </Style>
    </prism:Dialog.WindowStyle>

    <UserControl.Resources>
        <converters:BooleanToVisibilityConverter x:Key="BoolToVis"/>

        <!-- ── 窗口拖拽阴影外框 ── -->
        <DropShadowEffect x:Key="WindowShadow"
                          Color="#66000000"
                          BlurRadius="16"
                          ShadowDepth="4"
                          Direction="315"/>

        <!-- ── Tab 项：未选中样式 ── -->
        <Style x:Key="TabItemStyle" TargetType="Border">
            <Setter Property="Background"     Value="#F0F0F0"/>
            <Setter Property="BorderBrush"    Value="#BDBDBD"/>
            <Setter Property="BorderThickness" Value="1,1,1,0"/>
            <Setter Property="CornerRadius"   Value="6,6,0,0"/>
            <Setter Property="Margin"         Value="2,5,0,0"/>
            <Setter Property="Cursor"         Value="Hand"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                    <Setter Property="Background"      Value="White"/>
                    <Setter Property="BorderBrush"     Value="#1976D2"/>
                    <Setter Property="BorderThickness" Value="1,2,1,0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <!-- ── 标题栏按钮 ── -->
        <Style x:Key="TitleBarButtonStyle" TargetType="Button">
            <Setter Property="Background"   Value="Transparent"/>
            <Setter Property="BorderBrush"  Value="Transparent"/>
            <Setter Property="Foreground"   Value="White"/>
            <Setter Property="FontSize"     Value="14"/>
            <Setter Property="Width"        Value="36"/>
            <Setter Property="Height"       Value="28"/>
            <Setter Property="Cursor"       Value="Hand"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="bg"
                                Background="{TemplateBinding Background}"
                                CornerRadius="4">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bg" Property="Background" Value="#33FFFFFF"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="bg" Property="Background" Value="#55FFFFFF"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- ── Tab 关闭按钮（小号，无边框） ── -->
        <Style x:Key="TabCloseButtonStyle" TargetType="Button">
            <Setter Property="Background"   Value="Transparent"/>
            <Setter Property="BorderBrush"  Value="Transparent"/>
            <Setter Property="Foreground"   Value="#757575"/>
            <Setter Property="FontSize"     Value="12"/>
            <Setter Property="Width"        Value="18"/>
            <Setter Property="Height"       Value="18"/>
            <Setter Property="Padding"      Value="0"/>
            <Setter Property="Cursor"       Value="Hand"/>
            <Setter Property="VerticalContentAlignment"   Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="bg"
                                Background="{TemplateBinding Background}"
                                CornerRadius="9">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bg" Property="Background" Value="#FFD32F2F"/>
                                <Setter Property="Foreground" Value="White"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <!-- 最外层：透明底 + 阴影 + 圆角 -->
    <Border CornerRadius="10"
            Background="White"
            Effect="{StaticResource WindowShadow}"
            Margin="8">
        <Grid>
            <Grid.RowDefinitions>
                <!-- 标题栏 -->
                <RowDefinition Height="38"/>
                <!-- Tab 标签条 -->
                <RowDefinition Height="80"/>
                <!-- 内容区 -->
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Border Grid.Row="0"
                    CornerRadius="10,10,0,0"
                    Background="#1565C0"
                    MouseLeftButtonDown="MouseLeftButtonDown">
                <Grid Margin="12,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <!-- 图标 + 标题 -->
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <materialDesign:PackIcon Kind="WindowRestore"
                                                 Foreground="White"
                                                 Width="18" Height="18"
                                                 Margin="0,0,6,0"
                                                 VerticalAlignment="Center"/>
                        <TextBlock Text="弹窗管理器"
                                   Foreground="White"
                                   FontSize="13"
                                   FontWeight="Medium"
                                   VerticalAlignment="Center"/>
                        <!-- Tab 数量徽标 -->
                        <Border Background="#33FFFFFF"
                                CornerRadius="10"
                                Padding="6,1"
                                Margin="8,0,0,0"
                                VerticalAlignment="Center">
                            <TextBlock Foreground="White"
                                       FontSize="11">
                                <TextBlock.Text>
                                    <Binding Path="TagItems.Count"
                                             StringFormat="{}{0} 个窗口"/>
                                </TextBlock.Text>
                            </TextBlock>
                        </Border>
                    </StackPanel>

                    <!-- 最小化按钮 -->
                    <Button Grid.Column="1"
                            Content="—"
                            ToolTip="最小化"
                            Command="{Binding MinimizeCommand}"
                            Style="{StaticResource TitleBarButtonStyle}"
                            Margin="0,0,2,0"/>

                    <!-- 关闭按钮 -->
                    <Button Grid.Column="2"
                            Content="✕"
                            ToolTip="关闭"
                            Command="{Binding CloseCommand}"
                            Style="{StaticResource TitleBarButtonStyle}"/>
                </Grid>
            </Border>

            <Border Grid.Row="1"
                    Background="#FAFAFA"
                    BorderBrush="#E0E0E0"
                    BorderThickness="0,0,0,1">
                <ScrollViewer HorizontalScrollBarVisibility="Auto"
                              VerticalScrollBarVisibility="Hidden"
                              Padding="4,0,4,0">
                    <ItemsControl ItemsSource="{Binding TagItems}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <!-- 单个 Tab -->
                                <Border Style="{StaticResource TabItemStyle}">
                                    <StackPanel Orientation="Horizontal"
                                                Margin="8,4,4,4"
                                                VerticalAlignment="Center">
                                        <!-- Tab 标题（点击激活） -->
                                        <Button Command="{Binding SelectCommand}"
                                                Background="Transparent"
                                                BorderBrush="Transparent"
                                                Cursor="Hand"
                                                Padding="0"
                                                MaxWidth="130">
                                            <Button.Template>
                                                <ControlTemplate TargetType="Button">
                                                    <ContentPresenter/>
                                                </ControlTemplate>
                                            </Button.Template>
                                            <TextBlock Text="{Binding Title}"
                                                       FontSize="12"
                                                       TextTrimming="CharacterEllipsis"
                                                       ToolTip="{Binding Title}"
                                                       VerticalAlignment="Center">
                                                <TextBlock.Style>
                                                    <Style TargetType="TextBlock">
                                                        <Setter Property="Foreground" Value="#616161"/>
                                                        <Style.Triggers>
                                                            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                                                <Setter Property="Foreground"  Value="#1565C0"/>
                                                                <Setter Property="FontWeight"  Value="SemiBold"/>
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </TextBlock.Style>
                                            </TextBlock>
                                        </Button>
                                        <!-- 关闭按钮 -->
                                        <Button Command="{Binding CloseCommand}"
                                                Content="✕"
                                                Style="{StaticResource TabCloseButtonStyle}"
                                                Margin="4,0,0,0"
                                                ToolTip="关闭此窗口"/>
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>
            </Border>

            <Grid Grid.Row="2">

                <!-- 空状态提示 -->
                <StackPanel HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Visibility="{Binding HasNoTabs, Converter={StaticResource BoolToVis}}">
                    <materialDesign:PackIcon Kind="InboxOutline"
                                             Width="64" Height="64"
                                             Foreground="#BDBDBD"
                                             HorizontalAlignment="Center"/>
                    <TextBlock Text="暂无打开的弹窗"
                               Foreground="#9E9E9E"
                               FontSize="14"
                               Margin="0,8,0,0"
                               HorizontalAlignment="Center"/>
                    <TextBlock Text="其他窗口最小化后将在此处显示为标签页"
                               Foreground="#BDBDBD"
                               FontSize="11"
                               Margin="0,4,0,0"
                               HorizontalAlignment="Center"/>
                </StackPanel>

                <!-- 选中 Tab 的内容 -->
                <ContentControl Content="{Binding SelectedTag.Content}"
                                Visibility="{Binding HasNoTabs,
                                             Converter={StaticResource BoolToVis},
                                             ConverterParameter=Invert}"
                                HorizontalContentAlignment="Stretch"
                                VerticalContentAlignment="Stretch"/>
            </Grid>
        </Grid>
    </Border>
</UserControl>
