<UserControl x:Class="FLY.Thick.Base.UI.CtMicroGage"
             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:FLY.Thick.Base.UI"
             xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
             mc:Ignorable="d" 
             >
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/FLY.ControlLibrary;component/Converter/Converters.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            
            <!--Item 标题 例如 AD 12000-->
            <Style TargetType="TextBlock" x:Key="ItemHeaderStyle">
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="FontFamily" Value="Microsoft Sans Serif"/>
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="Foreground" Value="{StaticResource MahApps.Brushes.Text}"/>
                <Setter Property="Margin" Value="5,0"/>
            </Style>

            <!--Item 值 例如 AD 12000-->
            <Style TargetType="TextBlock" x:Key="ItemValueStyle">
                <Setter Property="FontFamily" Value="Microsoft Sans Serif"/>
                <Setter Property="FontSize" Value="24"/>
                <Setter Property="TextAlignment" Value="Center"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="Foreground" Value="{StaticResource MahApps.Brushes.Text}"/>
            </Style>

            <!--Item 单位 例如 m/min-->
            <Style TargetType="TextBlock" x:Key="ItemValueUnitStyle" BasedOn="{StaticResource Styles.Text.FieldContent.Unit}">
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="Foreground" Value="{StaticResource MahApps.Brushes.Text}"/>
            </Style>

            <!--Item 值 小字体-->
            <Style TargetType="TextBlock" x:Key="ItemValueStyle2" BasedOn="{StaticResource ItemValueStyle}">
                <Setter Property="FontSize" Value="12"/>
            </Style>

            <!--Item 单位 小字体-->
            <Style TargetType="TextBlock" x:Key="ItemValueUnitStyle2" BasedOn="{StaticResource ItemValueUnitStyle}">
                <Setter Property="FontSize" Value="10"/>
                <Setter Property="VerticalAlignment" Value="Bottom"/>
            </Style>

            <!--XY的灯颜色-->
            <SolidColorBrush x:Key="Brushes.IO.ON" Color="{StaticResource MahApps.Colors.ThemeBackground}" />
            <SolidColorBrush x:Key="Brushes.IO.OFF" Color="{StaticResource MahApps.Colors.ThemeForeground}" />


            <!--XY的灯-->
            <Style x:Key="IOStyle" TargetType="Rectangle" >
                <Setter Property="Width"  Value="10" />
                <Setter Property="Height"  Value="12" />
                <Setter Property="Margin"  Value="1" />
            </Style>

            <!--XY的灯里面的字-->
            <Style x:Key="IOTextStyle" TargetType="TextBlock">
                <Setter Property="Foreground"  Value="{StaticResource MahApps.Brushes.Gray4}" />
                <Setter Property="HorizontalAlignment"  Value="Center" />
                <Setter Property="VerticalAlignment"  Value="Center" />
                <Setter Property="FontSize"  Value="8"/>
            </Style>

            <!--停止正反转的按钮图标-->
            <Style x:Key="IconStyle" TargetType="{x:Type Control}" >
                <Setter Property="Margin" Value="6"/>
                <Setter Property="Height" Value="30"/>
                <Setter Property="Width" Value="30"/>
            </Style>

            <!--停止正反转的按钮-->
            <Style x:Key="BtnStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource Styles.Button.Empty}">
                <Setter Property="Margin" Value="2"/>
                <Setter Property="Foreground" Value="{StaticResource MahApps.Brushes.Accent}"/>
                <Setter Property="Background" Value="{StaticResource MahApps.Brushes.Text.Validation}"/>
                <Setter Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect BlurRadius="10" RenderingBias="Quality" Color="Black" Direction="270" ShadowDepth="2" Opacity="0.5" />
                    </Setter.Value>
                </Setter>
                <Style.Resources>
                    <Style TargetType="{x:Type Control}" BasedOn="{StaticResource IconStyle}">
                        <Setter Property="Margin" Value="6"/>
                        <Setter Property="Height" Value="30"/>
                        <Setter Property="Width" Value="30"/>
                    </Style>
                </Style.Resources>
            </Style>

            
            <local:IO2BitColorConverter x:Key="io2bitcolorconv"/>
            <local:IONumberConverter x:Key="ioNumberConv"/>
            <local:CtMircoGageVmUt x:Key="viewModel" 
                Thick="300.3" AD="12000" ADMax="65535" Position="5001" PosLength="8900" PosMm="500.1" Velocity="5.1" ControllerState="定点"
                OStatus="0x00F5" IStatus="0xff7f" IsIOShowNo ="True"
                IsError="True" ErrMsg="枫莱尔控制器连接断开" 
                />
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid d:DataContext="{StaticResource viewModel}" >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button Command="{Binding StopCmd}" Style="{StaticResource BtnStyle}" Background="{StaticResource MahApps.Brushes.ValidationSummary1}" >
                        <iconPacks:PackIconMaterial Kind="Close" Style="{StaticResource IconStyle}" />
                    </Button>
                    <Button Command="{Binding OrgCmd}" Style="{StaticResource BtnStyle}" >
                        <iconPacks:PackIconMaterial Kind="Adjust" Style="{StaticResource IconStyle}" />
                    </Button>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button Command="{Binding BackwCmd}" Style="{StaticResource BtnStyle}"  >
                        <iconPacks:PackIconMaterial Kind="ArrowLeft" Style="{StaticResource IconStyle}"/>
                    </Button>
                    <Button Command="{Binding ForwCmd}" Style="{StaticResource BtnStyle}" >
                        <iconPacks:PackIconMaterial Kind="ArrowRight" Style="{StaticResource IconStyle}"/>
                    </Button>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="4,0">
                    <StackPanel Grid.Column="1" Orientation="Horizontal">
                        <!--<TextBlock Text="{StaticResource str.CtMicroGage.Production}" Style="{StaticResource ItemHeaderStyle}"  />-->
                        <TextBlock Style="{StaticResource ItemValueStyle2}"
                                   Text="{Binding FilmVelocity,StringFormat={}{0:F1},Mode=OneWay}" />
                        <TextBlock Style="{StaticResource ItemValueUnitStyle}"
                                   Text="m/min"/>
                    </StackPanel>
                </StackPanel>
            </StackPanel>
            <StackPanel Margin="2" Grid.Column="2" Width="250">
                <Grid Margin="2" Name="grid_ad" Background="{StaticResource MahApps.Brushes.Gray8}">
                    <Rectangle Fill="{StaticResource MahApps.Brushes.Gray5}" HorizontalAlignment="Left">
                        <Rectangle.Width>
                            <MultiBinding Converter="{StaticResource ratioconv}" Mode="OneWay">
                                <Binding Path="AD" />
                                <Binding Path="ADMax" />
                                <Binding Path="ActualWidth" ElementName="grid_ad"/>
                            </MultiBinding>
                        </Rectangle.Width>
                    </Rectangle>
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Style="{StaticResource ItemHeaderStyle}" 
                                       Text="AD" />
                            <TextBlock Style="{StaticResource ItemValueStyle}"
                                       Text="{Binding AD}"  />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Grid.Column="1">
                            <TextBlock Style="{StaticResource ItemHeaderStyle}" 
                                       Text="{StaticResource str.CtMicroGage.Value}" />
                            <TextBlock Style="{StaticResource ItemValueStyle}" 
                                       Text="{Binding Thick,StringFormat={}{0:F1}}" />
                        </StackPanel>
                    </Grid>
                </Grid>
                <Grid Margin="2" Background="{StaticResource MahApps.Brushes.Gray8}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="auto"/>
                    </Grid.ColumnDefinitions>
                    <Rectangle Fill="{StaticResource MahApps.Brushes.Gray5}" HorizontalAlignment="Left" Grid.ColumnSpan="2">
                        <Rectangle.Width>
                            <MultiBinding Converter="{StaticResource ratioconv}" Mode="OneWay">
                                <Binding Path="Position" />
                                <Binding Path="PosLength" />
                                <Binding Path="ActualWidth" ElementName="grid_ad"/>
                            </MultiBinding>
                        </Rectangle.Width>
                    </Rectangle>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Style="{StaticResource ItemHeaderStyle}" 
                                   Text="{StaticResource str.CtMicroGage.Position}"/>
                        <TextBlock Style="{StaticResource ItemValueStyle}" >
                            <Run Text="{Binding Position}"/>
                            <Run Text="|"/>
                        </TextBlock>
                        <TextBlock Style="{StaticResource ItemValueUnitStyle}">
                            <Run Text="{Binding PosMm,StringFormat={}{0:F1}}"/><Run Text="mm"/>
                        </TextBlock>
                    </StackPanel>
                    <StackPanel Grid.Column="1" Orientation="Horizontal">
                        <TextBlock Style="{StaticResource ItemValueStyle}" 
                                   Text="{Binding Velocity,StringFormat={}{0:F1},Mode=OneWay}" />
                        <TextBlock Style="{StaticResource ItemValueUnitStyle}"
                                   Text="m/min"/>
                    </StackPanel>
                </Grid>
                <Grid>
                    <UniformGrid Columns="20" Margin="2,0" VerticalAlignment="Top" HorizontalAlignment="Left">
                        <UniformGrid.Resources>
                            <Style TargetType="Rectangle" BasedOn="{StaticResource IOStyle}"/>
                            <Style TargetType="TextBlock" BasedOn="{StaticResource IOTextStyle}"/>
                        </UniformGrid.Resources>

                        <!--X-->
                        <TextBlock >
                            <TextBlock.Style>
                                <Style TargetType="TextBlock" BasedOn="{StaticResource IOTextStyle}">
                                    <Setter Property="Text" Value="X"/>
                                    <Setter Property="Foreground" Value="{StaticResource MahApps.Brushes.Text}"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsIOShowNo}" Value="True">
                                            <Setter Property="Text" Value="I"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                        <Grid>
                            <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=0}" />
                            <TextBlock Text="{Binding IsIOShowNo,Converter={StaticResource ioNumberConv},ConverterParameter=0}" />
                        </Grid>
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=1}" />
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=2}" />
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=3}" />
                        <Grid/>
                        <Grid>
                            <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=4}" />
                            <TextBlock Text="{Binding IsIOShowNo,Converter={StaticResource ioNumberConv},ConverterParameter=4}" />
                        </Grid>
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=5}" />
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=6}" />
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=7}" />
                        <Grid/>
                        <Grid>
                            <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=8}" />
                            <TextBlock Text="{Binding IsIOShowNo,Converter={StaticResource ioNumberConv},ConverterParameter=8}" />
                        </Grid>
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=9}" />
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=10}" />
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=11}" />
                        <Grid/>
                        <Grid>
                            <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=12}" />
                            <TextBlock Text="{Binding IsIOShowNo,Converter={StaticResource ioNumberConv},ConverterParameter=12}" />
                        </Grid>
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=13}" />
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=14}" />
                        <Rectangle Fill="{Binding IStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=15}" />
                        <!--Y-->

                        <TextBlock >
                            <TextBlock.Style>
                                <Style TargetType="TextBlock" BasedOn="{StaticResource IOTextStyle}">
                                    <Setter Property="Text" Value="Y"/>
                                    <Setter Property="Foreground" Value="{StaticResource MahApps.Brushes.Text}"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsIOShowNo}" Value="True">
                                            <Setter Property="Text" Value="O"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                        <Grid>
                            <Rectangle Fill="{Binding OStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=0}"/>
                            <TextBlock Text="{Binding IsIOShowNo,Converter={StaticResource ioNumberConv},ConverterParameter=0}"/>
                        </Grid>
                        <Rectangle Fill="{Binding OStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=1}" />
                        <Rectangle Fill="{Binding OStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=2}" />
                        <Rectangle Fill="{Binding OStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=3}" />
                        <Grid/>
                        <Grid>
                            <Rectangle Fill="{Binding OStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=4}"/>
                            <TextBlock Text="{Binding IsIOShowNo,Converter={StaticResource ioNumberConv},ConverterParameter=4}"/>
                        </Grid>
                        <Rectangle Fill="{Binding OStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=5}" />
                        <Rectangle Fill="{Binding OStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=6}" />
                        <Rectangle Fill="{Binding OStatus,Converter={StaticResource io2bitcolorconv},ConverterParameter=7}" />
                    </UniformGrid>

                </Grid>
                <Grid Margin="1" >
                    <TextBlock Style="{StaticResource ItemHeaderStyle}" VerticalAlignment="Center" Text="{Binding ControllerState}" HorizontalAlignment="Right"/>
                    <Grid Visibility="{Binding IsError,Converter={StaticResource visbilityconv}}" Background="{StaticResource MahApps.Brushes.ValidationSummary1}" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <iconPacks:PackIconMaterial Kind="AlertCircleOutline" Foreground="White" Width="12" Height="12" SnapsToDevicePixels="True" Margin="5,2" >
                            <iconPacks:PackIconMaterial.Style>
                                <Style TargetType="iconPacks:PackIconMaterial">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsError}" Value="True">
                                            <DataTrigger.EnterActions>
                                                <BeginStoryboard>
                                                    <Storyboard RepeatBehavior="Forever" >
                                                        <DoubleAnimation BeginTime="0:0:0" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="00:00:01" />
                                                        <DoubleAnimation BeginTime="0:0:1" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="00:00:01" />
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </DataTrigger.EnterActions>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </iconPacks:PackIconMaterial.Style>
                        </iconPacks:PackIconMaterial>
                        <TextBlock Grid.Column="1" Style="{StaticResource ItemHeaderStyle}" VerticalAlignment="Center" Foreground="White" Text="{Binding ErrMsg}"/>
                    </Grid>
                </Grid>
            </StackPanel>
        </Grid>
    </Grid>
</UserControl>
