<UserControl x:Class="MainShell.PageCalib.AlgorithmCalib.View.FusionCalib.FusionAlignVerifyView"
             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:local="clr-namespace:MainShell.PageCalib.AlgorithmCalib.View.FusionCalib"
             xmlns:mw="http://www.maxwell-gp.com/"
             xmlns:customControl="clr-namespace:MainShell.Resources.CustomControl"
             mc:Ignorable="d"
             d:DesignHeight="800" d:DesignWidth="1200">
    <UserControl.Resources>
        <!-- 复用标定页面的样式 -->
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
        <Style x:Key="StartButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseRoundedButtonStyle}">
            <Setter Property="Width" Value="110"/>
            <Setter Property="Height" Value="32"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="0,0,6,0"/>
            <Setter Property="Background" Value="#1A202C"/>
            <Setter Property="BorderBrush" Value="#1A202C"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#2D3748"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="#0F141B"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="{StaticResource DisabledBackground}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource DisabledBackground}"/>
                    <Setter Property="Foreground" Value="{StaticResource DisabledForeground}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="VerifyButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseRoundedButtonStyle}">
            <Setter Property="Width" Value="110"/>
            <Setter Property="Height" Value="32"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="0,0,6,0"/>
            <Setter Property="Background" Value="#2B6CB0"/>
            <Setter Property="BorderBrush" Value="#2B6CB0"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#2C5282"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="#1A365D"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="{StaticResource DisabledBackground}"/>
                    <Setter Property="Foreground" Value="{StaticResource DisabledForeground}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="StopButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseRoundedButtonStyle}">
            <Setter Property="Width" Value="110"/>
            <Setter Property="Height" Value="32"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="0,0,6,0"/>
            <Setter Property="Background" Value="#E53E3E"/>
            <Setter Property="BorderBrush" Value="#E53E3E"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#C53030"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="#9B2C2C"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="ModuleCardStyle" TargetType="Border">
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="#E0E4EA"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="CornerRadius" Value="6"/>
            <Setter Property="Margin" Value="0,0,0,12"/>
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect Color="#000000" Opacity="0.06" BlurRadius="8" ShadowDepth="2" Direction="270"/>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="SectionHeaderTextStyle" TargetType="TextBlock">
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <Style x:Key="ParamLabelStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="#4A5568"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
        </Style>

        <Style x:Key="ReadOnlyNumberBoxStyle" TargetType="mw:NumberBox">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="28"/>
            <Setter Property="Margin" Value="6,2"/>
            <Setter Property="IsReadOnly" Value="True"/>
            <Setter Property="Background" Value="#F0F4F8"/>
            <Setter Property="Foreground" Value="#4A5568"/>
        </Style>

        <Style x:Key="SmallActionButtonStyle" TargetType="Button">
            <Setter Property="Height" Value="28"/>
            <Setter Property="Padding" Value="10,0"/>
            <Setter Property="Margin" Value="4,2"/>
            <Setter Property="FontSize" Value="11"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="bd" Background="#EDF2F7" BorderBrush="#CBD5E0"
                                BorderThickness="1" CornerRadius="4"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bd" Property="Background" Value="#E2E8F0"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="bd" Property="Background" Value="#CBD5E0"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="bd" Property="Background" Value="#F7FAFC"/>
                                <Setter TargetName="bd" Property="BorderBrush" Value="#E2E8F0"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="StepButtonStyle" TargetType="Button">
            <Setter Property="Width" Value="120"/>
            <Setter Property="Height" Value="32"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="0,0,8,0"/>
            <Setter Property="Background" Value="#2B6CB0"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="BorderBrush" Value="#2B6CB0"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#2C5282"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="#1A365D"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="#A0AEC0"/>
                    <Setter Property="BorderBrush" Value="#A0AEC0"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

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

        <!-- ==================== 左侧：相机预览 ==================== -->
        <Border Grid.Column="0" Margin="0,0,4,0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="32"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Border Grid.Row="0" Padding="10,0">
                    <Grid>
                        <TextBlock Text="相机预览" Foreground="#7A8FA6" FontSize="11"
                                   FontWeight="SemiBold" VerticalAlignment="Center"/>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
                            <Ellipse Width="7" Height="7" Fill="#28A745" Margin="0,0,4,0"/>
                            <TextBlock Text="LIVE" Foreground="#28A745" FontSize="10" FontWeight="Bold"/>
                        </StackPanel>
                    </Grid>
                </Border>
                <ContentControl Grid.Row="1" Content="{Binding CameraAxisViewModelSevice}"/>
            </Grid>
        </Border>

        <!-- ==================== 右侧：对位流程（3步） ==================== -->
        <Grid Grid.Column="1">
            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled"
                          CanContentScroll="False" PanningMode="VerticalOnly" Padding="8,8,8,0">
                <StackPanel>

                    <!-- ====== 步骤1：抓Pad点 ====== -->
                    <Border Style="{StaticResource ModuleCardStyle}" IsEnabled="{Binding Motion.IsFinish}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="36"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Border Grid.Row="0" Background="#F7F9FC" BorderBrush="#E0E4EA"
                                    BorderThickness="0,0,0,1" CornerRadius="6,6,0,0" Padding="10,0">
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                    <Ellipse Width="8" Height="8" Fill="#2B6CB0" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <TextBlock Text="第一步：抓Pad点（目标Pad）" Foreground="#2B6CB0" Style="{StaticResource SectionHeaderTextStyle}"/>
                                </StackPanel>
                            </Border>
                            <Border Grid.Row="1" Padding="10,8">
                                <StackPanel>
                                    <TextBlock Text="框选ROI区域，识别Pad目标点坐标" Foreground="#718096" FontSize="11" Margin="6,0,0,8"/>
                                    <Grid Margin="0,2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" Text="Pad X1(mm)：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:NumberBox Grid.Column="1" Value="{Binding PadCapturedX}" Style="{StaticResource ReadOnlyNumberBoxStyle}"/>
                                        <TextBlock Grid.Column="2" Text="Pad Y2(mm)：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:NumberBox Grid.Column="3" Value="{Binding PadCapturedY}" Style="{StaticResource ReadOnlyNumberBoxStyle}"/>
                                        <Button Grid.Column="4" Content="抓Pad点" Style="{StaticResource VerifyButtonStyle}" Click="{mw:Action btnCapturePad}"/>
                                    </Grid>
                                </StackPanel>
                            </Border>
                        </Grid>
                    </Border>

                    <!-- ====== 步骤2：Die网格飞拍 ====== -->
                    <Border Style="{StaticResource ModuleCardStyle}" IsEnabled="{Binding Motion.IsFinish}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="36"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Border Grid.Row="0" Background="#F7F9FC" BorderBrush="#E0E4EA"
                                    BorderThickness="0,0,0,1" CornerRadius="6,6,0,0" Padding="10,0">
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                    <Ellipse Width="8" Height="8" Fill="#2B6CB0" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <TextBlock Text="第二步：Die网格飞拍" Foreground="#2B6CB0" Style="{StaticResource SectionHeaderTextStyle}"/>
                                </StackPanel>
                            </Border>
                            <Border Grid.Row="1" Padding="10,8">
                                <StackPanel>
                                    <TextBlock Text="框选ROI抓Die首点，设置飞拍参数后执行网格扫描" Foreground="#718096" FontSize="11" Margin="6,0,0,8"/>
                                    <!-- 首点坐标 + 抓Die首点按钮 -->
                                    <Grid Margin="0,2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" Text="首点 X2(mm)：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:NumberBox Width="80" Grid.Column="1" Value="{Binding DieFirstX}" Style="{StaticResource ReadOnlyNumberBoxStyle}"/>
                                        <TextBlock Grid.Column="2" Text="首点 Y1(mm)：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:NumberBox Width="80" Grid.Column="3" Value="{Binding DieFirstY}" Style="{StaticResource ReadOnlyNumberBoxStyle}"/>
                                        <Button Grid.Column="4" Content="抓Die首点" Style="{StaticResource VerifyButtonStyle}" Click="{mw:Action btnCaptureDieFirst}"/>
                                    </Grid>
                                    <!-- 飞拍参数 -->
                                    <Grid Margin="0,6,0,2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="100"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Grid.Column="0" Text="X Count：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:IntNumberBox  Grid.Row="0" Grid.Column="1" Value="{Binding FlyScanXCount}" Width="80" Height="28" Margin="6,2"/>
                                        <TextBlock Grid.Row="0" Grid.Column="2" Text="Y Count：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:IntNumberBox Grid.Row="0" Grid.Column="3" Value="{Binding FlyScanYCount}" Width="80" Height="28" Margin="6,2"/>
                                        <TextBlock Grid.Row="1" Grid.Column="0" Text="Step(mm)：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:NumberBox Grid.Row="1" Grid.Column="1" Value="{Binding FlyScanStep}" Width="80" Height="28" Margin="6,2"/>
                                    </Grid>
                                    <!-- 飞拍按钮 -->
                                    <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                                        <Button Content="开始飞拍" Style="{StaticResource VerifyButtonStyle}" Click="{mw:Action btnStartFlyScan}"/>
                                        <Button Content="停止" Style="{StaticResource StopButtonStyle}" Click="{mw:Action btnStopAlign}" Width="80"/>
                                    </StackPanel>
                                    <!-- 飞拍结果 -->
                                    <TextBlock Text="{Binding FlyScanResultText}" Foreground="#2D3748" FontSize="12" Margin="6,8,0,4"/>
                                    <DataGrid ItemsSource="{Binding DisplayDieGridPoints}"
                                              AutoGenerateColumns="False"
                                              IsReadOnly="True"
                                              CanUserAddRows="False"
                                              CanUserDeleteRows="False"
                                              Height="120"
                                              Margin="0,2"
                                              FontSize="11"
                                              VerticalScrollBarVisibility="Auto">
                                        <DataGrid.Columns>
                                            <DataGridTextColumn Header="行" Binding="{Binding Row}" Width="50"/>
                                            <DataGridTextColumn Header="列" Binding="{Binding Col}" Width="50"/>
                                            <DataGridTextColumn Header="X2(mm)" Binding="{Binding X, StringFormat=F4}" Width="100"/>
                                            <DataGridTextColumn Header="Y1(mm)" Binding="{Binding Y, StringFormat=F4}" Width="100"/>
                                        </DataGrid.Columns>
                                    </DataGrid>
                                </StackPanel>
                            </Border>
                        </Grid>
                    </Border>

                    <!-- ====== 步骤3：对位验证 ====== -->
                    <Border Style="{StaticResource ModuleCardStyle}" IsEnabled="{Binding Motion.IsFinish}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="36"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Border Grid.Row="0" Background="#F7F9FC" BorderBrush="#E0E4EA"
                                    BorderThickness="0,0,0,1" CornerRadius="6,6,0,0" Padding="10,0">
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                    <Ellipse Width="8" Height="8" Fill="#38A169" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <TextBlock Text="第三步：对位验证" Foreground="#38A169" Style="{StaticResource SectionHeaderTextStyle}"/>
                                </StackPanel>
                            </Border>
                            <Border Grid.Row="1" Padding="10,8">
                                <StackPanel>
                                    <TextBlock Text="选择Die网格中的一点，与Pad点执行对位" Foreground="#718096" FontSize="11" Margin="6,0,0,8"/>
                                    <!-- 行列选择 -->
                                    <Grid Margin="0,2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" Text="选择行(Row)：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:IntNumberBox Grid.Column="1" Value="{Binding SelectedRow}" Width="80" Height="28" Margin="6,2"/>
                                        <TextBlock Grid.Column="2" Text="选择列(Col)：" Style="{StaticResource ParamLabelStyle}" Margin="6,0"/>
                                        <mw:IntNumberBox Grid.Column="3" Value="{Binding SelectedCol}" Width="80" Height="28" Margin="6,2"/>
                                        <Button Grid.Column="4" Content="执行对位" Style="{StaticResource VerifyButtonStyle}" Click="{mw:Action btnExecuteAlign}"/>
                                    </Grid>
                                    <!-- 对位结果 -->
                                    <Border Background="#F7FAFC" BorderBrush="#E2E8F0" BorderThickness="1" CornerRadius="4" Margin="0,8,0,4" Padding="8,6">
                                        <StackPanel>
                                            <TextBlock Text="对位结果" Foreground="#4A5568" FontSize="11" FontWeight="SemiBold" Margin="0,0,0,4"/>
                                            <TextBlock Text="{Binding AlignResultText}" Foreground="#2D3748" FontSize="12" TextWrapping="Wrap"/>
                                        </StackPanel>
                                    </Border>
                                </StackPanel>
                            </Border>
                        </Grid>
                    </Border>

                    <!-- ====== 进度 ====== -->
                    <Border Style="{StaticResource ModuleCardStyle}" Visibility="{Binding IsRunning, Converter={StaticResource BoolToVisibilityConverter}}">
                        <StackPanel Margin="10,8">
                            <TextBlock Text="{Binding ProgressMessage}" Foreground="#4A5568" FontSize="12"/>
                        </StackPanel>
                    </Border>

                </StackPanel>
            </ScrollViewer>
        </Grid>

        <customControl:LoadingWaitView Grid.ColumnSpan="2" Panel.ZIndex="1000"/>
    </Grid>
</UserControl>
