<UserControl x:Class="MainShell.Manual.View.DieRecheckView"
             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:behavior="clr-namespace:MainShell.Common.ControlAttribute"
             xmlns:custom="clr-namespace:MainShell.Resources.CustomControl"
             xmlns:mw="http://www.maxwell-gp.com/"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <ResourceDictionary>
            <Style x:Key="DieRecheckActionButtonStyle" TargetType="Button" BasedOn="{StaticResource CommonButtonStyle}">
                <Setter Property="Margin" Value="3,2"/>
                <Setter Property="MinWidth" Value="110"/>
            </Style>

            <Style x:Key="DieRecheckSectionTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource ProcessReadOnlyValueTextStyle}">
                <Setter Property="FontWeight" Value="SemiBold"/>
                <Setter Property="Margin" Value="0,0,0,6"/>
            </Style>

            <Style x:Key="DieRecheckThresholdCardStyle" TargetType="Border">
                <Setter Property="Background" Value="White"/>
                <Setter Property="BorderBrush" Value="#D8E0EC"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="CornerRadius" Value="8"/>
                <Setter Property="Padding" Value="12,10"/>
                <Setter Property="Margin" Value="0,0,8,0"/>
            </Style>

            <Style x:Key="DieRecheckThresholdLabelStyle" TargetType="TextBlock" BasedOn="{StaticResource ProcessReadOnlyValueTextStyle}">
                <Setter Property="FontWeight" Value="SemiBold"/>
                <Setter Property="Margin" Value="0,0,0,6"/>
            </Style>

            <Style x:Key="DieRecheckRuleListBorderStyle" TargetType="Border">
                <Setter Property="Background" Value="White"/>
                <Setter Property="BorderBrush" Value="#D8E0EC"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="CornerRadius" Value="8"/>
            </Style>

            <Style x:Key="DieRecheckRuleItemBorderStyle" TargetType="Border">
                <Setter Property="Padding" Value="12,10"/>
                <Setter Property="BorderBrush" Value="#E7ECF4"/>
                <Setter Property="BorderThickness" Value="0,0,0,1"/>
            </Style>

            <Style x:Key="DieRecheckRuleRadioStyle" TargetType="RadioButton">
                <Setter Property="FontSize" Value="13"/>
                <Setter Property="Foreground" Value="#425466"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="RadioButton">
                            <StackPanel Orientation="Horizontal">
                                <Grid Width="18" Height="18" Margin="0,0,10,0">
                                    <Ellipse Stroke="#B8C6DA" StrokeThickness="2" Fill="White"/>
                                    <Ellipse x:Name="InnerDot" Width="8" Height="8" Fill="#3D7DFF" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Grid>
                                <ContentPresenter VerticalAlignment="Center"/>
                            </StackPanel>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter TargetName="InnerDot" Property="Visibility" Value="Visible"/>
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Opacity" Value="0.5"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid Background="{StaticResource ProcessPageBackgroundBrush}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Style="{StaticResource ProcessCameraHostBorderStyle}">
            <ContentControl Content="{Binding CameraAxisViewModel}" />
        </Border>
        <ScrollViewer Grid.Column="1"
                      Margin="0,4,4,4"
                      VerticalScrollBarVisibility="Auto"
                      HorizontalScrollBarVisibility="Disabled"
                      Style="{StaticResource ProcessScrollViewerStyle}"
                      CanContentScroll="False">
            <Grid Margin="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <custom:RegionEditorView Title="{DynamicResource DieRecheckRegionEditorTitle}"
                                             DataContext="{Binding RecheckRegion}"/>
                

                <GroupBox Grid.Row="1" Header="{DynamicResource DieRecheckFunctionGroup}" Style="{StaticResource ProcessWideCardGroupBoxStyle}" Margin="0,6,0,0">
                    <StackPanel>
                        <TextBlock Text="{DynamicResource DieRecheckThresholdSectionTitle}" Style="{StaticResource DieRecheckSectionTitleStyle}"/>
                        <Grid Margin="0,0,0,8">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>

                            <Border Grid.Column="0" Style="{StaticResource DieRecheckThresholdCardStyle}">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{DynamicResource DieRecheckXThresholdSetting}" Style="{StaticResource DieRecheckThresholdLabelStyle}"/>
                                    <TextBox Style="{StaticResource ProcessTextBoxStyle}"
                                             behavior:ControlBehavior.IsNumericOnly="True"
                                             Text="{Binding FilterState.XThreshold, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat={}{0:F4}}"/>
                                </StackPanel>
                            </Border>

                            <Border Grid.Column="1" Style="{StaticResource DieRecheckThresholdCardStyle}" Margin="8,0,0,0">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{DynamicResource DieRecheckYThresholdSetting}" Style="{StaticResource DieRecheckThresholdLabelStyle}"/>
                                    <TextBox Style="{StaticResource ProcessTextBoxStyle}"
                                             behavior:ControlBehavior.IsNumericOnly="True"
                                             Text="{Binding FilterState.YThreshold, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat={}{0:F4}}"/>
                                </StackPanel>
                            </Border>
                        </Grid>

                        <TextBlock Text="{DynamicResource DieRecheckRuleSectionTitle}" Style="{StaticResource DieRecheckSectionTitleStyle}"/>
                        <Border Style="{StaticResource DieRecheckRuleListBorderStyle}" Margin="0,0,0,6">
                            <StackPanel>
                                <Border Style="{StaticResource DieRecheckRuleItemBorderStyle}">
                                    <RadioButton Style="{StaticResource DieRecheckRuleRadioStyle}"
                                                 GroupName="DieRecheckFilterRule"
                                                 Content="{DynamicResource DieRecheckFilterMissingBond}"
                                                 IsChecked="{Binding FilterState.IsMissingBondRuleSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                </Border>
                                <Border Style="{StaticResource DieRecheckRuleItemBorderStyle}">
                                    <RadioButton Style="{StaticResource DieRecheckRuleRadioStyle}"
                                                 GroupName="DieRecheckFilterRule"
                                                 IsChecked="{Binding FilterState.IsXThresholdRuleSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                        <TextBlock>
                                            <Run Text="{DynamicResource DieRecheckFilterXThreshold}"/>
                                            <Run Text=" ("/>
                                            <Run Text="{Binding FilterState.XThreshold, StringFormat={}{0:F4}}"/>
                                            <Run Text=")"/>
                                        </TextBlock>
                                    </RadioButton>
                                </Border>
                                <Border Style="{StaticResource DieRecheckRuleItemBorderStyle}">
                                    <RadioButton Style="{StaticResource DieRecheckRuleRadioStyle}"
                                                 GroupName="DieRecheckFilterRule"
                                                 IsChecked="{Binding FilterState.IsYThresholdRuleSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                        <TextBlock>
                                            <Run Text="{DynamicResource DieRecheckFilterYThreshold}"/>
                                            <Run Text=" ("/>
                                            <Run Text="{Binding FilterState.YThreshold, StringFormat={}{0:F4}}"/>
                                            <Run Text=")"/>
                                        </TextBlock>
                                    </RadioButton>
                                </Border>
                                <Border Padding="12,10">
                                    <RadioButton Style="{StaticResource DieRecheckRuleRadioStyle}"
                                                 GroupName="DieRecheckFilterRule"
                                                 Content="{DynamicResource DieRecheckFilterXYThreshold}"
                                                 IsChecked="{Binding FilterState.IsXYThresholdRuleSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                </Border>
                            </StackPanel>
                        </Border>

                        <WrapPanel HorizontalAlignment="Center">
                            <Button Style="{StaticResource DieRecheckActionButtonStyle}"
                                    Content="{DynamicResource DieRecheckMoveToSelectedPoint}"
                                    Click="{mw:Action MoveToSelectedPoint}"
                                    IsEnabled="{Binding SelectedPointResult, Converter={StaticResource ObjectToBoolConverter}}"/>
                            <Button Style="{StaticResource DieRecheckActionButtonStyle}"
                                    Content="{DynamicResource DieRecheckResetFilters}"
                                    Click="{mw:Action ResetFilters}"/>
                        </WrapPanel>
                    </StackPanel>
                </GroupBox>

                <GroupBox Grid.Row="2" Header="{DynamicResource DieRecheckCurrentPoints}" Style="{StaticResource ProcessWideCardGroupBoxStyle}" Margin="0,6,0,0">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <DataGrid Grid.Row="0"
                                  Style="{StaticResource ProcessDataGridStyle}"
                                  AutoGenerateColumns="False"
                                  IsReadOnly="True"
                                  CanUserAddRows="False"
                                  ItemsSource="{Binding PointResults}"
                                  SelectedItem="{Binding SelectedPointResult, Mode=TwoWay}"
                                  SelectionUnit="FullRow"
                                  SelectionMode="Single"
                                  Height="320"
                                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                                  ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                  MinHeight="260">
                            <DataGrid.Resources>
                                <Style TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource ProcessScrollViewerStyle}"/>
                            </DataGrid.Resources>
                            <DataGrid.Columns>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckStepIndex}" Binding="{Binding StepIndex}"/>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckPadRow}" Binding="{Binding PadRow}"/>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckPadColumn}" Binding="{Binding PadColumn}"/>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckDieRow}" Binding="{Binding DieRow}"/>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckDieColumn}" Binding="{Binding DieColumn}"/>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckXError}" Binding="{Binding XError, StringFormat={}{0:F4}}"/>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckYError}" Binding="{Binding YError, StringFormat={}{0:F4}}"/>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckStatus}" Binding="{Binding StatusSummary}"/>
                                <DataGridTextColumn Width="*" Header="{DynamicResource DieRecheckType}" Binding="{Binding TransPathType}"/>
                            </DataGrid.Columns>
                        </DataGrid>

                        <custom:PaginationControl Grid.Row="1" DataContext="{Binding Pagination}" Margin="0,10,0,0"/>
                    </Grid>
                </GroupBox>

                <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,8,0,0">
                    <Button Content="{DynamicResource Start}" Click="{mw:Action StartProcess}" Style="{StaticResource StartButtonStyle}" Margin="5"
                            IsEnabled="{Binding Runner.IsRunning, Converter={StaticResource boolToInversionConverter}}" />
                    <Button Content="{DynamicResource Stop}" Click="{mw:Action StopProcess}" Style="{StaticResource StopButtonStyle}" Margin="5"
                            IsEnabled="{Binding Runner.IsRunning}" />
                </StackPanel>
            </Grid>
        </ScrollViewer>
    </Grid>
</UserControl>
