<UserControl x:Class="MainShell.MainView"
             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:mw="http://www.maxwell-gp.com/"
             xmlns:custom="clr-namespace:MainShell.Resources.CustomControl"
             mc:Ignorable="d" 
             d:DesignHeight="650" d:DesignWidth="800">
    <UserControl.Resources>
        <ResourceDictionary>
            <Style x:Key="DashboardPanelBorderStyle" TargetType="Border">
                <Setter Property="Background" Value="{StaticResource ProcessCardBackgroundBrush}"/>
                <Setter Property="BorderBrush" Value="{StaticResource ProcessCardBorderBrush}"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="CornerRadius" Value="8"/>
            </Style>

            <Style x:Key="DashboardSectionGroupBoxStyle" TargetType="GroupBox" BasedOn="{StaticResource ProcessPageContentGroupBoxStyle}">
                <Setter Property="Margin" Value="0,0,0,12"/>
                <Setter Property="Padding" Value="14,16,14,14"/>
            </Style>

            <Style x:Key="DashboardCameraBorderStyle" TargetType="Border">
                <Setter Property="Background" Value="#101418"/>
                <Setter Property="BorderBrush" Value="{StaticResource ProcessCardBorderBrush}"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="CornerRadius" Value="6"/>
            </Style>

            <Style x:Key="DashboardMapBorderStyle" TargetType="Border">
                <Setter Property="Background" Value="#F8FAFC"/>
                <Setter Property="BorderBrush" Value="{StaticResource ProcessDividerBrush}"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="CornerRadius" Value="6"/>
                <Setter Property="Padding" Value="10"/>
            </Style>

            <Style x:Key="DashboardMutedTextStyle" TargetType="TextBlock">
                <Setter Property="Foreground" Value="#6B7280"/>
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>

            <Style x:Key="DashboardReadOnlyTextBoxStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
                <Setter Property="IsReadOnly" Value="True"/>
                <Setter Property="IsReadOnlyCaretVisible" Value="True"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Cursor" Value="IBeam"/>
                <Setter Property="TextWrapping" Value="Wrap"/>
                <Setter Property="AcceptsReturn" Value="False"/>
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
                <Setter Property="VerticalScrollBarVisibility" Value="Disabled"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
            </Style>

            <Style x:Key="DashboardInfoCardStyle" TargetType="Border">
                <Setter Property="BorderBrush" Value="{StaticResource ProcessDividerBrush}"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="CornerRadius" Value="6"/>
                <Setter Property="Padding" Value="12,10"/>
                <Setter Property="Background" Value="#F8FAFC"/>
            </Style>

            <Style x:Key="DashboardCountCardStyle" TargetType="Border" BasedOn="{StaticResource DashboardInfoCardStyle}">
                <Setter Property="Padding" Value="10,8"/>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid Background="{StaticResource ProcessPageBackgroundBrush}" Margin="8">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="280"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="320"/>
        </Grid.ColumnDefinitions>

        <Border Grid.Column="0" Margin="0,0,12,0" Style="{StaticResource DashboardPanelBorderStyle}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Border Style="{StaticResource ProcessCardHeaderBarStyle}" CornerRadius="8,8,0,0">
                    <TextBlock Text="CAMERA / 相机图像"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Style="{StaticResource ProcessCardHeaderTextStyle}"/>
                </Border>

                <Border Grid.Row="1" Margin="10,12,10,0" Style="{StaticResource DashboardCameraBorderStyle}">
                    <Grid>
                        <ContentControl mw:View.Model="{Binding CameraSearchViewModel}"/>
                        <Border HorizontalAlignment="Right"
                                VerticalAlignment="Bottom"
                                Margin="0,0,8,8"
                                Background="#660F172A"
                                CornerRadius="3"
                                Padding="6,2">
                            <TextBlock Text="上相机" Opacity="0.9" Foreground="#CBD5E1" FontSize="12"/>
                        </Border>
                    </Grid>
                </Border>

                <Border Grid.Row="2" Margin="10,10,10,0" Style="{StaticResource DashboardCameraBorderStyle}">
                    <Grid>
                        <ContentControl mw:View.Model="{Binding CameraLocateViewModel}"/>
                        <Border HorizontalAlignment="Right"
                                VerticalAlignment="Bottom"
                                Margin="0,0,8,8"
                                Background="#660F172A"
                                CornerRadius="3"
                                Padding="6,2">
                            <TextBlock Text="下相机" Opacity="0.9" Foreground="#CBD5E1" FontSize="12"/>
                        </Border>
                    </Grid>
                </Border>

                <Border Grid.Row="3" Margin="10" Style="{StaticResource DashboardCameraBorderStyle}">
                    <Grid>
                        <ContentControl mw:View.Model="{Binding CameraInspectViewModel}"/>
                        <Border HorizontalAlignment="Right"
                                VerticalAlignment="Bottom"
                                Margin="0,0,8,8"
                                Background="#660F172A"
                                CornerRadius="3"
                                Padding="6,2">
                            <TextBlock Text="复检相机" Opacity="0.9" Foreground="#CBD5E1" FontSize="12"/>
                        </Border>
                    </Grid>
                </Border>
            </Grid>
        </Border>

        <Grid Grid.Column="1" Margin="0,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <GroupBox Margin="0,0,0,12" Style="{StaticResource DashboardSectionGroupBoxStyle}" Header="MAIN FLOW / 主流程">
                <custom:FlowStageDisplayControl FlowNodes="{Binding FlowNodes}"/>
            </GroupBox>

            <GroupBox Grid.Row="1" Margin="0,0,0,12" Style="{StaticResource DashboardSectionGroupBoxStyle}" Header="WAFER HANDLING / 晶圆上下料">
                <custom:FlowStageDisplayControl FlowNodes="{Binding WaferFlowNodes}"/>
            </GroupBox>

            <GroupBox Grid.Row="2" Margin="0" Style="{StaticResource DashboardSectionGroupBoxStyle}" Header="REAL-TIME MAPPING / 双图展示区">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Border Style="{StaticResource DashboardInfoCardStyle}" Background="#F6F9FC">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="220"/>
                            </Grid.ColumnDefinitions>

                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <Rectangle Width="4" Height="18" Fill="{StaticResource ProcessHeaderAccentBrush}" Margin="0,0,10,0"/>
                                <TextBlock Text="Mapping Progress" Foreground="{StaticResource ProcessTitleForegroundBrush}" FontWeight="SemiBold" VerticalAlignment="Center"/>
                            </StackPanel>

                            <TextBlock Grid.Column="1" Text="进度:" VerticalAlignment="Center" Margin="0,0,8,0" Style="{StaticResource DashboardMutedTextStyle}"/>
                            <Grid Grid.Column="2" VerticalAlignment="Center">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="52"/>
                                </Grid.ColumnDefinitions>
                                <ProgressBar Height="14"
                                             Margin="0,0,10,0"
                                             Minimum="0"
                                             Maximum="100"
                                             Value="{Binding DashboardState.MappingProgress}"/>
                                <TextBlock Grid.Column="1"
                                           Text="{Binding DashboardState.MappingProgress, StringFormat={}{0:F1}%}"
                                           VerticalAlignment="Center"
                                           HorizontalAlignment="Right"
                                           Foreground="{StaticResource ProcessHeaderAccentBrush}"
                                           FontWeight="Bold"/>
                            </Grid>
                        </Grid>
                    </Border>

                    <Grid Grid.Row="1" Margin="0,12,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="12"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Border Style="{StaticResource DashboardMapBorderStyle}">
                            <custom:DieMapPanelControl Caption="基板 (Substrate) Map 绘图区"
                                                       MapModel="{Binding SubstrateMapModel}"/>
                        </Border>

                        <Border Grid.Column="2" Style="{StaticResource DashboardMapBorderStyle}">
                            <custom:DieMapPanelControl Caption="晶圆 (Wafer) Map 绘图区"
                                                       MapModel="{Binding WaferMapModel}"/>
                        </Border>
                    </Grid>
                </Grid>
            </GroupBox>
        </Grid>

        <Grid Grid.Column="2" Margin="0,6,6,6">
            <Grid.RowDefinitions>
                <RowDefinition Height="190"/>
                <RowDefinition Height="120"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="200"/>
            </Grid.RowDefinitions>

            <GroupBox Header="IDENTIFICATION / 标识" Margin="0,0,0,8" BorderBrush="#C7C7C7" BorderThickness="1">
                <Grid Margin="8">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Border BorderBrush="#C7C7C7" BorderThickness="1" Background="#EFEFEF" Margin="0,0,0,6">
                        <StackPanel Margin="8,6">
                            <TextBlock Text="产品 ID" Foreground="#6B7280"/>
                            <TextBox Text="{Binding DashboardState.ProductId}"
                                     Foreground="#0B4A8B"
                                     FontSize="20"
                                     Width="Auto"
                                     FontWeight="Bold"
                                     IsReadOnly="True"
                                     IsReadOnlyCaretVisible="True"
                                     BorderThickness="0"
                                     Background="Transparent"
                                     Padding="0"
                                     Cursor="IBeam"
                                     TextWrapping="Wrap"
                                     AcceptsReturn="False"
                                     HorizontalAlignment="Stretch"
                                     HorizontalScrollBarVisibility="Auto"
                                     VerticalScrollBarVisibility="Disabled"
                                     HorizontalContentAlignment="Left"
                                     VerticalContentAlignment="Center"
                                     ToolTip="{Binding DashboardState.ProductId}"/>
                        </StackPanel>
                    </Border>
                    <Border Grid.Row="1" BorderBrush="#C7C7C7" BorderThickness="1" Background="#EFEFEF">
                        <StackPanel Margin="8,6">
                            <TextBlock Text="芯片 ID" Foreground="#6B7280"/>
                            <TextBox Text="{Binding DashboardState.ChipId}"
                                     Foreground="#EA580C"
                                     Width="Auto"
                                     FontSize="20"
                                     FontWeight="Bold"
                                     IsReadOnly="True"
                                     IsReadOnlyCaretVisible="True"
                                     BorderThickness="0"
                                     Background="Transparent"
                                     Padding="0"
                                     Cursor="IBeam"
                                     TextWrapping="Wrap"
                                     AcceptsReturn="False"
                                     HorizontalAlignment="Stretch"
                                     HorizontalScrollBarVisibility="Auto"
                                     VerticalScrollBarVisibility="Disabled"
                                     HorizontalContentAlignment="Left"
                                     VerticalContentAlignment="Center"
                                     ToolTip="{Binding DashboardState.ChipId}"/>
                        </StackPanel>
                    </Border>
                </Grid>
            </GroupBox>

            <GroupBox Grid.Row="1" Header="RECIPE / 配方" Margin="0,0,0,8" BorderBrush="#C7C7C7" BorderThickness="1">
                <StackPanel Margin="10,8">
                    <TextBlock Text="基板配方:" Foreground="#6B7280"/>
                    <TextBlock Text="{Binding SubstrateRecipe}" FontWeight="Bold"/>
                    <TextBlock Text="芯片配方:" Foreground="#6B7280" Margin="0,4,0,0"/>
                    <TextBlock Text="{Binding ChipRecipe}" FontWeight="Bold"/>
                </StackPanel>
            </GroupBox>

            <GroupBox Grid.Row="2" BorderBrush="#C7C7C7" BorderThickness="1">
                <GroupBox.Header>
                    <Grid Width="250">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="PRODUCTION STATS/ 统计" VerticalAlignment="Center"  Foreground="#0B4A8B" FontWeight="Bold"/>
                        <Button Grid.Column="1"
                                Content="↺ 清除统计"
                                Click="{mw:Action ClearStatistics}"
                                Style="{StaticResource ClearStatsButtonStyle}"/>
                    </Grid>
                </GroupBox.Header>
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Grid Grid.Row="0" Margin="0,2,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="加工基板总数"/>
                        <TextBlock Grid.Column="1" Text="{Binding DashboardState.ProcessedSubstrateTotal}" FontWeight="Bold" Foreground="#1D4ED8"/>
                        <TextBlock Grid.Column="2" Text="PCS" Margin="6,0,0,0" Foreground="#94A3B8"/>
                    </Grid>

                    <Grid Grid.Row="1" Margin="0,10,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="当前基板待加工芯片"/>
                        <TextBlock Grid.Column="1" Text="{Binding DashboardState.PendingCount}" FontWeight="Bold"/>
                    </Grid>

                    <Grid Grid.Row="2" Margin="0,10,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="当前基板已加工芯片"/>
                        <TextBlock Grid.Column="1" Text="{Binding DashboardState.ProcessedCount}" FontWeight="Bold" Foreground="#16A34A"/>
                    </Grid>

                    <Grid Grid.Row="3" Margin="0,12,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="8"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Border BorderBrush="#C7C7C7" BorderThickness="1" Background="#EFF6FF" Padding="8,6">
                            <StackPanel>
                                <TextBlock Text="实时 UPH" Foreground="#2563EB" HorizontalAlignment="Center"/>
                                <TextBlock Text="{Binding DashboardState.RealTimeUph}" Foreground="#1D4ED8" FontSize="28" FontWeight="Bold" HorizontalAlignment="Center"/>
                            </StackPanel>
                        </Border>

                        <Border Grid.Column="2" BorderBrush="#C7C7C7" BorderThickness="1" Background="#F0FDF4" Padding="8,6">
                            <StackPanel>
                                <TextBlock Text="综合 UPH" Foreground="#16A34A" HorizontalAlignment="Center"/>
                                <TextBlock Text="{Binding DashboardState.YieldUph}" Foreground="#16A34A" FontSize="28" FontWeight="Bold" HorizontalAlignment="Center"/>
                            </StackPanel>
                        </Border>
                    </Grid>
                </Grid>
            </GroupBox>

            <GroupBox Grid.Row="3" Header="LIVE CONTROL / 运行控制" Margin="0,8,0,0" BorderBrush="#C7C7C7" BorderThickness="1">
                <Grid Margin="8,8">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="6"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="6"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="6"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Button Grid.Row="0" Content="启动 START"
                            Click="{mw:Action StartProcess}"
                            Width="190"
                            Height="34"
                            Margin="0"
                            Padding="8,0"
                            FontSize="15"
                            FontWeight="Bold"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            IsEnabled="{Binding Runner.IsRunning, Converter={StaticResource boolToInversionConverter}}"
                            Style="{StaticResource StartButtonStyle}"/>
                    <Button Grid.Row="2" Content="暂停 PAUSE"
                            Click="{mw:Action PauseProcess}"
                            Width="190"
                            Height="34"
                            Margin="0"
                            Padding="8,0"
                            FontSize="15"
                            FontWeight="Bold"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            IsEnabled="{Binding CanPauseProcess}"
                            Style="{StaticResource StopButtonStyle}"/>
                    <Button Grid.Row="4" Content="恢复 RESUME"
                            Click="{mw:Action ResumeProcess}"
                            Width="190"
                            Height="34"
                            Margin="0"
                            Padding="8,0"
                            FontSize="15"
                            FontWeight="Bold"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            IsEnabled="{Binding CanResumeProcess}"
                            Style="{StaticResource StartButtonStyle}"/>
                    <Button Grid.Row="6" Content="停止 STOP"
                            Click="{mw:Action StopProcess}"
                            Width="190"
                            Height="34"
                            Margin="0"
                            Padding="8,0"
                            FontSize="15"
                            FontWeight="Bold"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            IsEnabled="{Binding Runner.IsRunning}"
                            Style="{StaticResource StopButtonStyle}"/>
                </Grid>
            </GroupBox>
        </Grid>
    </Grid>
</UserControl>
