<Window
    x:Class="SHH.CameraDashboard.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:SHH.CameraDashboard"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Width="1900"
    Height="1030"
    Background="{DynamicResource Brush.Bg.Window}"
    FontFamily="{StaticResource Font.Normal}"
    Foreground="{DynamicResource Brush.Text.Primary}"
    Topmost="{Binding IsTopMost}"
    WindowStartupLocation="CenterScreen"
    mc:Ignorable="d">

    <WindowChrome.WindowChrome>
        <WindowChrome
            CaptionHeight="0"
            CornerRadius="0"
            GlassFrameThickness="0"
            ResizeBorderThickness="5" />
    </WindowChrome.WindowChrome>

    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>

    <Border
        Background="{DynamicResource Brush.Bg.Window}"
        BorderBrush="{DynamicResource Brush.Border}"
        BorderThickness="1">

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

            <Border
                x:Name="TitleBar"
                Grid.Row="0"
                Height="32"
                Background="{DynamicResource Brush.Bg.Panel}"
                BorderBrush="{DynamicResource Brush.Border}"
                BorderThickness="0,0,0,1"
                MouseLeftButtonDown="TitleBar_MouseDown">
                <Grid>
                    <StackPanel
                        Margin="12,0"
                        VerticalAlignment="Center"
                        Orientation="Horizontal">
                        <TextBlock
                            Margin="0,0,8,0"
                            VerticalAlignment="Center"
                            Text="📷" />
                        <TextBlock
                            VerticalAlignment="Center"
                            FontSize="12"
                            FontWeight="Bold"
                            Text="SHH 视频网关 - 中控台" />
                    </StackPanel>

                    <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                        <Button
                            Command="{Binding OpenWizardCommand}"
                            Style="{StaticResource Btn.TitleBar}"
                            ToolTip="向导">
                            <Path
                                Width="18"
                                Height="18"
                                Data="{StaticResource Icon.Wizard}"
                                Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                                Stretch="Uniform" />
                        </Button>

                        <Button
                            Width="40"
                            Height="30"
                            Click="BtnTheme_Click"
                            Style="{StaticResource Btn.TitleBar}"
                            ToolTip="切换皮肤">
                            <StackPanel VerticalAlignment="Center" Orientation="Horizontal">
                                <Path
                                    Width="14"
                                    Height="14"
                                    Data="{StaticResource Icon.Theme}"
                                    Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                                    Stretch="Uniform" />
                                <Path
                                    Width="8"
                                    Height="8"
                                    Margin="4,2,0,0"
                                    VerticalAlignment="Center"
                                    Data="M7,10L12,15L17,10H7Z"
                                    Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                                    Stretch="Uniform" />
                            </StackPanel>
                            <Button.ContextMenu>
                                <ContextMenu Placement="Bottom">
                                    <MenuItem Command="{Binding SwitchThemeCommand}" CommandParameter="Dark">
                                        <MenuItem.Header>
                                            <TextBlock
                                                Margin="0,1,0,0"
                                                VerticalAlignment="Center"
                                                FontSize="13"
                                                Text="🌚 深色模式 (Dark)" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Command="{Binding SwitchThemeCommand}" CommandParameter="Light">
                                        <MenuItem.Header>
                                            <TextBlock
                                                Margin="0,1,0,0"
                                                VerticalAlignment="Center"
                                                FontSize="13"
                                                Text="☀️ 浅色模式 (Light)" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                </ContextMenu>
                            </Button.ContextMenu>
                        </Button>

                        <Button
                            Command="{Binding ToggleTopMostCommand}"
                            Style="{StaticResource Btn.TitleBar}"
                            ToolTip="{Binding TopMostTooltip}">

                            <Path
                                Width="16"
                                Height="16"
                                Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                                Stretch="Uniform">
                                <Path.Style>
                                    <Style TargetType="Path">
                                        <Setter Property="Data" Value="{StaticResource Icon.Pin.Outline}" />

                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsTopMost}" Value="True">
                                                <Setter Property="Data" Value="{StaticResource Icon.Pin.Fixed}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Path.Style>
                            </Path>
                        </Button>
                        <Rectangle Width="10" Fill="Transparent" />

                        <Button
                            Command="{Binding MinimizeCommand}"
                            CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                            Content="—"
                            Style="{StaticResource Btn.TitleBar}" />
                        <Button
                            Command="{Binding ToggleMaximizeCommand}"
                            CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                            Content="{Binding MaxButtonContent}"
                            Style="{StaticResource Btn.TitleBar}" />
                        <Button
                            Command="{Binding CloseCommand}"
                            CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                            Content="✕"
                            Style="{StaticResource Btn.TitleBar.Close}" />
                    </StackPanel>
                </Grid>
            </Border>

            <DockPanel Grid.Row="1">
                <!--  左侧摄像头列表  -->
                <Border
                    Width="320"
                    BorderBrush="{DynamicResource Brush.Border}"
                    BorderThickness="0,0,1,0"
                    DockPanel.Dock="Left">
                    <local:CameraList x:Name="CameraList" />
                </Border>

                <!--  右侧面板  -->
                <Border
                    Width="500"
                    Background="{DynamicResource Brush.Bg.Window}"
                    BorderBrush="{DynamicResource Brush.Border}"
                    BorderThickness="1,0,0,0"
                    DockPanel.Dock="Right"
                    Visibility="{Binding IsRightPanelVisible, Converter={StaticResource BooleanToVisibilityConverter}}">

                    <Border.Resources>
                        <DataTemplate DataType="{x:Type local:CameraEditViewModel}">
                            <local:CameraEdit />
                        </DataTemplate>

                        <DataTemplate DataType="{x:Type local:CameraPtzViewModel}">
                            <local:CameraPtz />
                        </DataTemplate>

                        <DataTemplate DataType="{x:Type local:CameraImgProcViewModel}">
                            <local:CameraImgProc />
                        </DataTemplate>

                        <DataTemplate DataType="{x:Type local:CameraImageSubscriptionViewModels}">
                            <local:CameraImageSubscription />
                        </DataTemplate>
                    </Border.Resources>
                    <ContentControl Content="{Binding CurrentRightPanelViewModel}" />
                </Border>

                <DockPanel>
                    <local:CameraItemTop DataSource="{Binding ElementName=CameraList, Path=DataContext.SelectedCamera}" DockPanel.Dock="Top" />

                    <Grid>
                        <Grid.Resources>
                            <DataTemplate DataType="{x:Type local:VideoWallViewModel}">
                                <local:VideoWall />
                            </DataTemplate>
                        </Grid.Resources>

                        <ContentControl Content="{Binding MainContent}" />
                    </Grid>
                </DockPanel>
            </DockPanel>

            <GridSplitter
                Grid.Row="2"
                Height="5"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Center"
                Background="Transparent"
                ResizeBehavior="PreviousAndNext"
                ResizeDirection="Rows"
                ShowsPreview="True" />

            <Border
                x:Name="BottomLogPanel"
                Grid.Row="3"
                Background="{DynamicResource Brush.Bg.Panel}"
                BorderBrush="{DynamicResource Brush.Border}"
                BorderThickness="0,1,0,0">

                <Border.Style>
                    <Style TargetType="Border">
                        <Setter Property="Height" Value="250" />
                        <Setter Property="MinHeight" Value="100" />

                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsLogPanelExpanded}" Value="False">
                                <Setter Property="Height" Value="35" />
                                <Setter Property="MinHeight" Value="35" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>

                <Grid>
                    <TabControl
                        Padding="0"
                        HorizontalContentAlignment="Stretch"
                        VerticalContentAlignment="Stretch"
                        Background="Transparent"
                        BorderThickness="0"
                        ItemContainerStyle="{DynamicResource Style.TabItem.Modern}">

                        <TabItem Header="WebAPI 诊断">
                            <local:ServiceNodesDiagnostic DataContext="{Binding DiagnosticVM}" />
                        </TabItem>
                    </TabControl>

                    <StackPanel
                        Margin="0,2,5,0"
                        HorizontalAlignment="Right"
                        VerticalAlignment="Top"
                        Panel.ZIndex="1"
                        Orientation="Horizontal">

                        <Button
                            Width="30"
                            Height="30"
                            Command="{Binding DiagnosticVM.ClearCommand}"
                            Style="{StaticResource Btn.TitleBar}"
                            ToolTip="清空日志">
                            <Path
                                Width="14"
                                Height="14"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Data="{StaticResource Icon.Trash}"
                                Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                                Stretch="Uniform" />
                        </Button>

                        <Button
                            Width="30"
                            Height="30"
                            Command="{Binding ToggleLogPanelCommand}"
                            Style="{StaticResource Btn.TitleBar}"
                            ToolTip="展开/收起面板">
                            <Path
                                Width="12"
                                Height="12"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                                Stretch="Uniform">
                                <Path.Style>
                                    <Style TargetType="Path">
                                        <Setter Property="Data" Value="{StaticResource Icon.ChevronDown}" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsLogPanelExpanded}" Value="False">
                                                <Setter Property="Data" Value="{StaticResource Icon.ChevronUp}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Path.Style>
                            </Path>
                        </Button>
                    </StackPanel>
                </Grid>
            </Border>

            <Grid
                x:Name="GlobalMask"
                Grid.Row="1"
                Grid.RowSpan="3"
                Background="#CC000000"
                Visibility="{Binding IsOverlayVisible, Converter={StaticResource BooleanToVisibilityConverter}}">
                <ContentControl
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Content="{Binding OverlayContent}">
                    <ContentControl.Style>
                        <Style TargetType="ContentControl">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsOverlayVisible}" Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation
                                                    Storyboard.TargetProperty="Opacity"
                                                    From="0"
                                                    To="1"
                                                    Duration="0:0:0.2" />
                                                <DoubleAnimation
                                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)"
                                                    From="0.9"
                                                    To="1"
                                                    Duration="0:0:0.2" />
                                                <DoubleAnimation
                                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)"
                                                    From="0.9"
                                                    To="1"
                                                    Duration="0:0:0.2" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.EnterActions>
                                </DataTrigger>
                            </Style.Triggers>
                            <Setter Property="RenderTransform">
                                <Setter.Value>
                                    <ScaleTransform CenterX="0.5" CenterY="0.5" />
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ContentControl.Style>
                </ContentControl>
            </Grid>

        </Grid>
    </Border>
</Window>