<UserControl
    x:Class="SHH.CameraDashboard.CameraImgProc"
    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"
    d:DataContext="{d:DesignInstance local:CameraImgProcViewModel}"
    d:DesignHeight="650"
    d:DesignWidth="500"
    mc:Ignorable="d">

    <UserControl.Resources>
        <local:BoolToMaxScaleConverter x:Key="BoolToMaxScaleConverter" />
    </UserControl.Resources>

    <Grid Margin="20" Background="{DynamicResource Brush.Bg.Window}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <DockPanel Margin="0,0,0,20" LastChildFill="False">
            <TextBlock
                VerticalAlignment="Center"
                FontSize="18"
                FontWeight="Bold"
                Foreground="{DynamicResource Brush.Text.Primary}"
                Text="{Binding Title}" />
            <Button
                Width="30"
                Height="30"
                Command="{Binding CancelCommand}"
                Content="✕"
                DockPanel.Dock="Right"
                Style="{DynamicResource Btn.Ghost}" />
        </DockPanel>

        <Border
            Grid.Row="1"
            Margin="0,0,0,15"
            Padding="15"
            Background="{DynamicResource Brush.Bg.Panel}"
            CornerRadius="6">
            <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
                <TextBlock Foreground="{DynamicResource Brush.Text.Secondary}" Text="源画面分辨率：" />
                <TextBlock
                    Margin="10,0,0,0"
                    FontWeight="Bold"
                    Foreground="{DynamicResource Brush.Text.Primary}"
                    Text="{Binding SourceResolutionText}" />
            </StackPanel>
        </Border>

        <GroupBox
            Grid.Row="2"
            Margin="0,0,0,15"
            Padding="15"
            BorderBrush="{DynamicResource Brush.Border}"
            Foreground="{DynamicResource Brush.Text.Secondary}"
            Header="分辨率调整">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <DockPanel LastChildFill="False">
                        <StackPanel>
                            <TextBlock Foreground="{DynamicResource Brush.Text.Primary}" Text="允许缩小 (Allow Shrink)" />
                            <TextBlock
                                FontSize="11"
                                Foreground="{DynamicResource Brush.Text.Secondary}"
                                Text="可将画面缩小至任意尺寸" />
                        </StackPanel>
                        <CheckBox
                            DockPanel.Dock="Right"
                            IsChecked="{Binding AllowShrink}"
                            Style="{DynamicResource Style.CheckBox.Switch}" />
                    </DockPanel>

                    <DockPanel
                        Grid.Column="1"
                        Margin="20,0,0,0"
                        LastChildFill="False">
                        <StackPanel>
                            <TextBlock Foreground="{DynamicResource Brush.Text.Primary}" Text="允许放大 (Allow Enlarge)" />
                            <TextBlock
                                FontSize="11"
                                Foreground="{DynamicResource Brush.Text.Secondary}"
                                Text="最大支持 1080P" />
                        </StackPanel>
                        <CheckBox
                            Margin="5,0,0,0"
                            DockPanel.Dock="Right"
                            IsChecked="{Binding AllowEnlarge}"
                            IsEnabled="{Binding CanCheckExpand}"
                            Style="{DynamicResource Style.CheckBox.Switch}" />
                    </DockPanel>


                </Grid>

                <StackPanel Grid.Row="1" Margin="0,20,0,0">
                    <TextBlock
                        Margin="0,0,0,10"
                        HorizontalAlignment="Center"
                        Foreground="{DynamicResource Brush.State.Warning}"
                        Text="请先开启“允许缩小”或“允许放大”以调整分辨率">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsSliderEnabled}" Value="False">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>

                    <Grid Margin="0,0,0,15" IsEnabled="{Binding IsSliderEnabled}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="50" />
                        </Grid.ColumnDefinitions>
                        <TextBlock
                            Margin="0,0,10,0"
                            VerticalAlignment="Center"
                            Text="缩放比" />

                        <Slider
                            Grid.Column="1"
                            VerticalAlignment="Center"
                            IsSnapToTickEnabled="True"
                            Maximum="{Binding AllowEnlarge, Converter={StaticResource BoolToMaxScaleConverter}}"
                            Minimum="10"
                            TickFrequency="5"
                            Value="{Binding ScalePercent}" />

                        <TextBlock
                            Grid.Column="2"
                            HorizontalAlignment="Right"
                            VerticalAlignment="Center"
                            Foreground="{DynamicResource Brush.Brand}"
                            Text="{Binding ScalePercent, StringFormat={}{0:F0}%}" />
                    </Grid>

                    <Grid IsEnabled="{Binding IsSliderEnabled}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <StackPanel>
                            <TextBlock
                                Margin="0,0,0,5"
                                FontSize="12"
                                Text="宽度" />
                            <TextBox
                                Height="32"
                                Style="{DynamicResource Style.TextBox.Search}"
                                Text="{Binding TargetWidth, UpdateSourceTrigger=PropertyChanged}"
                                TextAlignment="Center" />
                        </StackPanel>
                        <ToggleButton
                            Grid.Column="1"
                            Margin="15,18,15,0"
                            IsChecked="{Binding IsRatioLocked}"
                            ToolTip="锁定长宽比">
                            <Path
                                Width="16"
                                Height="16"
                                Data="M12,17C10.89,17 10,16.1 10,15C10,13.89 10.89,13 12,13A2,2 0 0,1 14,15A2,2 0 0,1 12,17M18,20V10H6V20H18M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6C4.89,22 4,21.1 4,20V10C4,8.89 4.89,8 6,8H7V6A5,5 0 0,1 17,6V8H18M12,4A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,4Z"
                                Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType=ToggleButton}}"
                                Stretch="Uniform" />
                        </ToggleButton>
                        <StackPanel Grid.Column="2">
                            <TextBlock
                                Margin="0,0,0,5"
                                FontSize="12"
                                Text="高度" />
                            <TextBox
                                Height="32"
                                Style="{DynamicResource Style.TextBox.Search}"
                                Text="{Binding TargetHeight, UpdateSourceTrigger=PropertyChanged}"
                                TextAlignment="Center" />
                        </StackPanel>
                    </Grid>
                </StackPanel>

                <StackPanel Grid.Row="2" Margin="0,0,0,20">
                    <TextBlock
                        Margin="0,20,0,10"
                        FontSize="16"
                        FontWeight="Bold"
                        Foreground="{DynamicResource Brush.Text.Secondary}"
                        Text="常用尺寸预设" />
                    <WrapPanel>
                        <WrapPanel.Resources>
                            <Style BasedOn="{StaticResource Btn.Ghost.Secondary}" TargetType="Button">
                                <Setter Property="Margin" Value="0,0,8,8" />
                                <Setter Property="Width" Value="Auto" />
                                <Setter Property="Padding" Value="6,0" />
                                <Setter Property="Height" Value="16" />
                                <Setter Property="Command" Value="{Binding ApplyPresetCommand}" />
                            </Style>
                        </WrapPanel.Resources>
                        <Button CommandParameter="1920x1080" Content="1080P (1920x1080)" />
                        <Button CommandParameter="1280x720" Content="720P (1280x720)" />
                        <Button CommandParameter="704x576" Content="D1 (704x576)" />
                        <Button CommandParameter="640x480" Content="VGA (640x480)" />
                        <Button CommandParameter="640x640" Content="Square (640x640)" />
                        <Button CommandParameter="352x288" Content="CIF (352x288)" />
                    </WrapPanel>
                </StackPanel>
            </Grid>
        </GroupBox>

        <StackPanel Grid.Row="3">
            <TextBlock
                Margin="0,0,0,10"
                FontWeight="Bold"
                Foreground="{DynamicResource Brush.Text.Secondary}"
                Text="高级处理" />
            <Border
                Padding="10"
                Background="{DynamicResource Brush.Bg.Input}"
                CornerRadius="4">
                <DockPanel LastChildFill="False">
                    <StackPanel>
                        <TextBlock Foreground="{DynamicResource Brush.Text.Primary}" Text="图像画质增强 (Image Enhancement)" />
                        <TextBlock
                            FontSize="11"
                            Foreground="{DynamicResource Brush.Text.Secondary}"
                            Text="自动调节对比度与锐度" />
                    </StackPanel>
                    <CheckBox
                        DockPanel.Dock="Right"
                        IsChecked="{Binding EnhanceImage}"
                        Style="{DynamicResource Style.CheckBox.Switch}" />
                </DockPanel>
            </Border>
        </StackPanel>

        <StackPanel
            Grid.Row="4"
            Margin="0,20,0,0"
            HorizontalAlignment="Right"
            Orientation="Horizontal">
            <Button
                Width="100"
                Margin="0,0,10,0"
                Command="{Binding CancelCommand}"
                Content="取消"
                Style="{DynamicResource Btn.Ghost}" />
            <Button
                Width="120"
                Command="{Binding SaveCommand}"
                Content="保存应用" />
        </StackPanel>
    </Grid>
</UserControl>