<UserControl x:Class="MainShell.PageCalib.OriginCalib.View.OriginCalibView"
             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/"
             mc:Ignorable="d"
             d:DesignHeight="800"
             d:DesignWidth="1200">
    <UserControl.Resources>
        <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="AvoidanceAxisNameStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="#E8821A"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <Style x:Key="TableHeaderStyle" TargetType="TextBlock">
            <Setter Property="FontSize" Value="11"/>
            <Setter Property="Foreground" Value="#8892A0"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
        </Style>

        <Style x:Key="CalibAxisNameStyle" TargetType="TextBlock">
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Foreground" Value="#2D3748"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <Style x:Key="TargetValueStyle" TargetType="mw:NumberBox">
            <Setter Property="Width" Value="85"/>
            <Setter Property="Height" Value="28"/>
            <Setter Property="Margin" Value="0,1"/>
            <Setter Property="mw:NumericKeypadAttach.IsEnabled" Value="True"/>
            <Setter Property="Minimum" Value="-99999"/>
            <Setter Property="Maximum" Value="99999"/>
            <Setter Property="DecimalPlaces" Value="3"/>
            <Setter Property="Foreground" Value="#1568D5"/>
        </Style>

        <Style x:Key="SmallIconButtonStyle" TargetType="Button">
            <Setter Property="Width" Value="28"/>
            <Setter Property="Height" Value="24"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Margin" Value="1,0"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="bd"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="1"
                                CornerRadius="4">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bd" Property="Background" Value="#F0F4F8"/>
                                <Setter TargetName="bd" Property="BorderBrush" Value="#CBD5E0"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="bd" Property="Background" Value="#E2E8F0"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="AddAxisButtonStyle" TargetType="Button" BasedOn="{StaticResource SmallIconButtonStyle}">
            <Setter Property="Width" Value="24"/>
            <Setter Property="Height" Value="24"/>
            <Setter Property="Foreground" Value="#28A745"/>
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>

        <Style x:Key="ModuleActionButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseRoundedButtonStyle}">
            <Setter Property="Width" Value="110"/>
            <Setter Property="Height" Value="32"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="4,0"/>
        </Style>

        <Style x:Key="CalibStartButtonStyle" TargetType="Button" BasedOn="{StaticResource ModuleActionButtonStyle}">
            <Setter Property="Background" Value="#1A202C"/>
            <Setter Property="BorderBrush" Value="#1A202C"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#2D3748"/>
                    <Setter Property="BorderBrush" Value="#2D3748"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="#0F141B"/>
                    <Setter Property="BorderBrush" 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}"/>
                    <Setter Property="Cursor" Value="Arrow"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="BadgeStyle" TargetType="Border">
            <Setter Property="CornerRadius" Value="3"/>
            <Setter Property="Padding" Value="5,1"/>
            <Setter Property="Margin" Value="3,0"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <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="{DynamicResource OriginCalibMonitorTitle}"
                                   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 CameraAxisViewModel}"/>
            </Grid>
        </Border>

        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Border Grid.Row="0" Background="White" BorderBrush="#E0E4EA" BorderThickness="0,0,0,1" Padding="8,0">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
                        <Button Content="{DynamicResource OriginCalibSaveConfig}"
                            Click="{mw:Action SaveConfig}"
                            Style="{StaticResource SaveButtonStyle}"
                            Width="160"
                            Height="34"
                            Margin="5,0"/>
                        <Button Content="{DynamicResource OriginCalibResetAll}"
                            Click="{mw:Action ResetAll}"
                            Style="{StaticResource ResetButtonStyle}"
                            Width="130"
                            Height="34"
                            Margin="5,0"/>
                </StackPanel>
            </Border>

            <ScrollViewer Grid.Row="1"
                          VerticalScrollBarVisibility="Auto"
                          HorizontalScrollBarVisibility="Disabled"
                          CanContentScroll="False"
                          PanningMode="VerticalOnly"
                          Padding="8,8,8,0">
                <ItemsControl ItemsSource="{Binding Modules}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Border Style="{StaticResource ModuleCardStyle}">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="36"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="46"/>
                                    </Grid.RowDefinitions>

                                    <Border Grid.Row="0"
                                            Background="#F7F9FC"
                                            BorderBrush="#E0E4EA"
                                            BorderThickness="0,0,0,1"
                                            CornerRadius="6,6,0,0"
                                            Padding="10,0">
                                        <Grid>
                                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                                <TextBlock Text="[] " Foreground="#8892A0" FontSize="14" VerticalAlignment="Center"/>
                                                <TextBlock FontWeight="SemiBold" FontSize="13" Foreground="#2D3748" VerticalAlignment="Center">
                                                    <Run Text="{DynamicResource OriginCalibModuleLabel}"/>
                                                    <Run Text=" "/>
                                                    <Run Text="{Binding Index, StringFormat=D2}"/>
                                                    <Run Text=": "/>
                                                    <Run Text="{Binding ModuleName}"/>
                                                </TextBlock>
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
                                                <Border Style="{StaticResource BadgeStyle}"
                                                        Background="#EDF2F7"
                                                        Visibility="{Binding ShowIndependentBadge}">
                                                    <TextBlock Text="{DynamicResource OriginCalibBadgeIndependent}" FontSize="10" Foreground="#718096"/>
                                                </Border>
                                                <Border Style="{StaticResource BadgeStyle}"
                                                        Background="#FFF5EB"
                                                        Visibility="{Binding ShowAvoidanceBadge}">
                                                    <TextBlock Text="{DynamicResource OriginCalibBadgeAvoidance}" FontSize="10" Foreground="#E8821A"/>
                                                </Border>
                                            </StackPanel>
                                        </Grid>
                                    </Border>

                                    <Grid Grid.Row="1" MinHeight="170">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>

                                        <Border Grid.Row="0" BorderBrush="#E0E4EA" BorderThickness="0,0,0,1" Padding="10,8">
                                            <StackPanel>
                                                <Grid Margin="0,0,0,6">
                                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                                        <Ellipse Width="8" Height="8" Fill="#E8821A" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                                        <TextBlock Text="{DynamicResource OriginCalibAvoidanceHeader}"
                                                                   Foreground="#E8821A"
                                                                   FontWeight="SemiBold"
                                                                   FontSize="12"
                                                                   VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                    <Button Content="+"
                                                            HorizontalAlignment="Right"
                                                            Style="{StaticResource AddAxisButtonStyle}"
                                                            IsEnabled="{Binding CanStartCalib}"
                                                            Click="{mw:Action AddAvoidanceAxis}"
                                                            mw:View.ActionTarget="{Binding}"
                                                            ToolTip="{DynamicResource OriginCalibAddAvoidanceAxis}"/>
                                                </Grid>

                                                <ItemsControl ItemsSource="{Binding AvoidanceAxes}">
                                                    <ItemsControl.ItemTemplate>
                                                        <DataTemplate>
                                                            <Grid Margin="0,2">
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition Width="*"/>
                                                                    <ColumnDefinition Width="90"/>
                                                                    <ColumnDefinition Width="30"/>
                                                                    <ColumnDefinition Width="30"/>
                                                                </Grid.ColumnDefinitions>
                                                                <TextBlock Grid.Column="0"
                                                                           Text="{Binding AxisName}"
                                                                           Style="{StaticResource AvoidanceAxisNameStyle}"
                                                                           TextTrimming="CharacterEllipsis"
                                                                           ToolTip="{DynamicResource OriginCalibAxisName}"/>
                                                                <mw:NumberBox Grid.Column="1"
                                                                              Value="{Binding Position, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                                              mw:NumericKeypadAttach.IsEnabled="True"
                                                                              IsEnabled="{Binding CanTeach}"
                                                                              Minimum="-99999"
                                                                              Maximum="99999"
                                                                              DecimalPlaces="2"
                                                                              Width="68"
                                                                              Height="26"
                                                                              Margin="0,1"
                                                                              HorizontalAlignment="Right"/>
                                                                <Button Grid.Column="2"
                                                                        Style="{StaticResource SmallIconButtonStyle}"
                                                                        IsEnabled="{Binding CanTeach}"
                                                                        Click="{mw:Action TeachPosition}"
                                                                        mw:View.ActionTarget="{Binding}"
                                                                        ToolTip="{DynamicResource OriginCalibTeachAvoidancePosition}">
                                                                    <TextBlock Text="T" FontSize="12" Foreground="#4A5568"/>
                                                                </Button>
                                                                <Button Grid.Column="3"
                                                                        Style="{StaticResource SmallIconButtonStyle}"
                                                                        IsEnabled="{Binding CanTeach}"
                                                                        Click="{mw:Action Remove}" mw:View.ActionTarget="{Binding}"
                                                                        ToolTip="{DynamicResource OriginCalibRemoveAxis}">
                                                                    <TextBlock Text="X" FontSize="11" Foreground="#A0AEC0"/>
                                                                </Button>
                                                            </Grid>
                                                        </DataTemplate>
                                                    </ItemsControl.ItemTemplate>
                                                </ItemsControl>
                                            </StackPanel>
                                        </Border>

                                        <Grid Grid.Row="1" Margin="10,8">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="26"/>
                                                <RowDefinition Height="*"/>
                                            </Grid.RowDefinitions>

                                            <Grid Grid.Row="0">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="65"/>
                                                </Grid.ColumnDefinitions>
                                                <Border Grid.ColumnSpan="5" Background="#F7F9FC" BorderBrush="#E0E4EA" BorderThickness="0,0,0,1"/>
                                                <TextBlock Grid.Column="0" Text="{DynamicResource OriginCalibAxisHeader}" Style="{StaticResource TableHeaderStyle}" Margin="6,0"/>
                                                <TextBlock Grid.Column="1" Text="{DynamicResource OriginCalibTargetHeader}" Style="{StaticResource TableHeaderStyle}" HorizontalAlignment="Center"/>
                                                <TextBlock Grid.Column="2" Text="{DynamicResource OriginCalibActualHeader}" Style="{StaticResource TableHeaderStyle}" HorizontalAlignment="Center"/>
                                                <TextBlock Grid.Column="3" Text="{DynamicResource OriginCalibOffsetHeader}" Style="{StaticResource TableHeaderStyle}" HorizontalAlignment="Center"/>
                                                <TextBlock Grid.Column="4" Text="{DynamicResource OriginCalibStatusHeader}" Style="{StaticResource TableHeaderStyle}" HorizontalAlignment="Center"/>
                                            </Grid>

                                            <ItemsControl Grid.Row="1" ItemsSource="{Binding CalibrationAxes}">
                                                <ItemsControl.ItemTemplate>
                                                    <DataTemplate>
                                                        <Grid MinHeight="30">
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="*"/>
                                                                <ColumnDefinition Width="*"/>
                                                                <ColumnDefinition Width="*"/>
                                                                <ColumnDefinition Width="*"/>
                                                                <ColumnDefinition Width="65"/>
                                                            </Grid.ColumnDefinitions>
                                                            <Border Grid.ColumnSpan="5" BorderBrush="#F0F4F8" BorderThickness="0,0,0,1"/>
                                                            <TextBlock Grid.Column="0"
                                                                       Text="{Binding AxisName}"
                                                                       Style="{StaticResource CalibAxisNameStyle}"
                                                                       Margin="6,0"
                                                                       TextTrimming="CharacterEllipsis"/>
                                                            <TextBlock Grid.Column="1"
                                                                       Text="{Binding TargetPosition, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                                       HorizontalAlignment="Center"
                                                                       VerticalAlignment="Center"
                                                                       FontSize="12"
                                                                       FontWeight="SemiBold"/>
                                                            <TextBlock Grid.Column="2"
                                                                       Text="{Binding LastPosition, StringFormat=F3}"
                                                                       HorizontalAlignment="Center"
                                                                       VerticalAlignment="Center"
                                                                       FontSize="12"
                                                                       Foreground="#4A5568"/>
                                                            <TextBlock Grid.Column="3"
                                                                       Text="{Binding OffsetText}"
                                                                       Foreground="{Binding OffsetBrush}"
                                                                       HorizontalAlignment="Center"
                                                                       VerticalAlignment="Center"
                                                                       FontSize="12"
                                                                       FontWeight="SemiBold"/>
                                                            <Ellipse Grid.Column="4"
                                                                     Width="12"
                                                                     Height="12"
                                                                     Fill="{Binding StatusBrush}"
                                                                     HorizontalAlignment="Center"
                                                                     VerticalAlignment="Center"/>
                                                        </Grid>
                                                    </DataTemplate>
                                                </ItemsControl.ItemTemplate>
                                            </ItemsControl>
                                        </Grid>
                                    </Grid>

                                    <Border Grid.Row="2"
                                            BorderBrush="#E0E4EA"
                                            BorderThickness="0,1,0,0"
                                            Background="#FAFBFC"
                                            CornerRadius="0,0,6,6"
                                            Padding="10,0">
                                        <Grid>
                                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                                <Button Content="{DynamicResource OriginCalibTeachTarget}"
                                                        Click="{mw:Action TeachCalibPositions}"
                                                        mw:View.ActionTarget="{Binding}"
                                                        Style="{StaticResource CommonButtonStyle}"
                                                        Width="110"
                                                        Height="32"
                                                        Margin="0,0,6,0"/>
                                                <Button Content="{DynamicResource MakeModel}"
                                                        Click="{mw:Action MakeTemplate}"
                                                        mw:View.ActionTarget="{Binding}"
                                                        Style="{StaticResource CommonButtonStyle}"
                                                        Width="110"
                                                        Height="32"
                                                        Margin="0,0,6,0"/>
                                                <Button Content="{DynamicResource OriginCalibStartCalib}"
                                                        Click="{mw:Action StartCalib}"
                                                        mw:View.ActionTarget="{Binding}"
                                                        Style="{StaticResource CalibStartButtonStyle}"
                                                        IsEnabled="{Binding CanStartCalib}"
                                                        Width="110"
                                                        Height="32"
                                                        Margin="0,0,6,0"/>
                                            </StackPanel>
                                            <Button HorizontalAlignment="Right"
                                                    VerticalAlignment="Center"
                                                    Style="{StaticResource SmallIconButtonStyle}"
                                                    ToolTip="{DynamicResource OriginCalibModuleSettings}">
                                                <TextBlock Text="..." FontSize="13" Foreground="#718096"/>
                                            </Button>
                                        </Grid>
                                    </Border>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </Grid>

    </Grid>
</UserControl>
