<Page x:Class="FLY.Thick.Base.UI.PgGrid"
      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:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
      xmlns:local="clr-namespace:FLY.Thick.Base.UI"
      xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
      mc:Ignorable="d" 
      d:DesignHeight="1000" d:DesignWidth="1024"
	Title="PgGrid" >
    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="PgGridStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <local:PgGridVmUt x:Key="viewModel" IsADMode="True"/>
        </ResourceDictionary>
    </Page.Resources>
    <Grid d:DataContext="{StaticResource viewModel}" Background="{StaticResource Brushes.Card.Background}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid Background="{StaticResource Brushes.TitleBar.Background}" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <StackPanel Orientation="Horizontal" >
                <Button Style="{StaticResource Styles.TitleBar.BackButton2}" Command="BrowseBack"/>
                <TextBlock Style="{StaticResource Styles.TitleBar.Text}" Text="控制器调试"/>
                <StackPanel VerticalAlignment="Center">
                    <TextBlock Text="Grid图" FontSize="30" VerticalAlignment="Center" Margin="{StaticResource ControlMargin}" Foreground="{StaticResource Brushes.NoAct}"/>
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Style="{StaticResource Styles.Text.FieldHeader}" Text="AD模式" Foreground="{StaticResource Brushes.NoAct}"/>
                        <ToggleButton Style="{StaticResource Styles.ToggleButton.Check}" HorizontalAlignment="Left" IsChecked="{Binding IsADMode}" Width="30" Height="auto"  Foreground="White" Background="#02FFFFFF" />
                    </StackPanel>
                </StackPanel>
                
            </StackPanel>
            <local:CtMicroGage Grid.Column="1" x:Name="mircoGage" Background="Transparent" VerticalAlignment="Bottom"/>
        </Grid>

        <ScrollViewer CanContentScroll="False" PanningMode="Both" Grid.Row="1"  >
            <StackPanel Margin="{StaticResource ControlMargin}">
                <Border Style="{StaticResource Styles.Card.Border}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <Grid >
                            <StackPanel Orientation="Horizontal" Visibility="{Binding IsCanDownload,Converter={StaticResource visbilityconv}}">
                                <StackPanel Margin="{StaticResource ControlMargin}">
                                    <TextBlock Style="{StaticResource Styles.Text.FieldHeader.Editable}" Text="目标位置" />
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBox HorizontalAlignment="Left" MaxWidth="150" Style="{StaticResource Styles.Text.FieldContent.Input}"
                                                 Text="{Binding TargetPos}" />
                                        <StackPanel Orientation="Horizontal" >
                                            <TextBlock Style="{StaticResource Styles.Text.FieldContent.Separator}" Text="|" />
                                            <TextBlock Style="{StaticResource Styles.Text.FieldContent.Unit}">
                                                <Run Text="{Binding TargetMm,Mode=OneWay}"/><Run Text="mm"/>
                                            </TextBlock>
                                            
                                        </StackPanel>
                                    </StackPanel>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                    <Button Content="运行至" Style="{StaticResource Styles.Button.Square.Accent2}" Command="{Binding RunToCmd}"
                                        />
                                    <TextBlock Text="{Binding Msg}" Style="{StaticResource Styles.Text.FieldContent.Unit}"/>
                                </StackPanel>

                            </StackPanel>
                        </Grid>

                        <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Top">
                            <Button Content="删除" Style="{StaticResource Styles.Button.Square2}" Command="{Binding ClearCmd}" Background="{StaticResource MahApps.Brushes.ValidationSummary3}"/>
                            <Button Content="保存" Style="{StaticResource Styles.Button.Square2}" Command="{Binding SaveCmd}"/>
                            
                        </StackPanel>
                    </Grid>
                </Border>

                <Border Style="{StaticResource Styles.Card.Border}" >
                    <StackPanel>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="auto"/>
                            </Grid.ColumnDefinitions>
                            <ItemsControl ItemsSource="{Binding SeriesInfos}" Margin="5" x:Name="itemSelect">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Button Style="{StaticResource Styles.Button.Empty}" Command="{Binding DataContext.SelectCmd, ElementName=itemSelect}" CommandParameter="{Binding }">
                                            <Border x:Name="bd" Background="WhiteSmoke" BorderBrush="{Binding Fill}" BorderThickness="2" Margin="2" Height="35" Width="80" >
                                                <TextBlock x:Name="tb" Text="{Binding Header}" FontSize="24" Foreground="{Binding Fill}" FontWeight="Bold"  VerticalAlignment="Center" HorizontalAlignment="Center"  />
                                            </Border>
                                        </Button>
                                        <DataTemplate.Triggers>
                                            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                                <Setter TargetName="bd" Property="Background" Value="{Binding Fill}"/>
                                                <Setter TargetName="bd" Property="BorderBrush" Value="LightGray"/>
                                                <Setter TargetName="tb" Property="Foreground" Value="WhiteSmoke"/>
                                            </DataTrigger>
                                        </DataTemplate.Triggers>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                            <StackPanel Grid.Column="1" Orientation="Horizontal">
                                <Button Content="获取正向" Style="{StaticResource Styles.Button.Square.Accent2}" Command="{Binding GetFDataCmd}" />
                                <Button Content="获取反向" Style="{StaticResource Styles.Button.Square2}" Command="{Binding GetBDataCmd}"  />
                            </StackPanel>
                            
                        </Grid>
                        

                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <lvc:CartesianChart x:Name="chart1" Grid.Row="1" Margin="5" Height="350" MouseMove="UIElement_OnMouseMove" MouseDown="UIElement_MouseDown" MouseUp="UIElement_MouseUp"
                                            DisableAnimations="True" Hoverable="False" DataTooltip="{x:Null}"
                                            Series="{Binding Series}"
                                            >
                                <lvc:CartesianChart.AxisX>
                                    <lvc:Axis FontSize="16" >
                                        <lvc:Axis.Separator>
                                            <lvc:Separator Stroke="LightGray"/>
                                        </lvc:Axis.Separator>
                                        <lvc:Axis.Sections>
                                            <lvc:AxisSection Style="{StaticResource Styles.Axis.Section}" Value="{Binding SectionValue}" SectionWidth="{Binding SectionWidth}" />
                                        </lvc:Axis.Sections>
                                    </lvc:Axis>
                                </lvc:CartesianChart.AxisX>
                                <lvc:CartesianChart.AxisY>
                                    <lvc:Axis FontSize="16" >
                                        <lvc:Axis.Separator>
                                            <lvc:Separator Stroke="LightGray"/>
                                        </lvc:Axis.Separator>
                                    </lvc:Axis>
                                </lvc:CartesianChart.AxisY>
                            </lvc:CartesianChart>
                            <Controls:RangeSlider Grid.Row="1" Grid.Column="1"
                                                  Margin="0,10"
                                                  Controls:SliderHelper.ChangeValueBy="LargeChange"
                                                  Controls:SliderHelper.EnableMouseWheel="MouseHover"
                                                  AutoToolTipPlacement="TopLeft"
                                                  Orientation="Vertical"
                                                  LargeChange="10"
                                                  SmallChange="1"
                                                  Maximum="{Binding YMax}"
                                                  Minimum="{Binding YMin}"
                                                  
                                                  LowerValue="{Binding YLower}"
                                                  UpperValue="{Binding YUpper}" />
                            <Controls:RangeSlider 
                                                  Margin="10,0"
                                                  Controls:SliderHelper.ChangeValueBy="LargeChange"
                                                  Controls:SliderHelper.EnableMouseWheel="MouseHover"
                                                  AutoToolTipPlacement="TopLeft"
                                                  LargeChange="10"
                                                  Maximum="{Binding XMax}"
                                                  Minimum="{Binding XMin}"
                                                  Orientation="Horizontal"
                                                  SmallChange="1"
                                                  LowerValue="{Binding XLower}"
                                                  UpperValue="{Binding XUpper}" />
                        </Grid>
                        
                    </StackPanel>
                </Border>
                <Border Style="{StaticResource Styles.Card.Border}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <StackPanel Orientation="Horizontal" >
                            <StackPanel Margin="{StaticResource ControlMargin}">
                                <TextBlock Style="{StaticResource Styles.Text.FieldHeader.Editable}" Text="测量位置" />
                                <StackPanel Orientation="Horizontal" >
                                    <TextBox HorizontalAlignment="Left" MaxWidth="150" Style="{StaticResource Styles.Text.FieldContent.Input}"
                                                 Text="{Binding MeasureValuePos}" />
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBlock Style="{StaticResource Styles.Text.FieldContent.Separator}" Text="|" />
                                        <TextBlock Style="{StaticResource Styles.Text.FieldContent.Unit}">
                                                <Run Text="{Binding MeasureValueMm,Mode=OneWay}"/><Run Text="mm"/>
                                        </TextBlock>

                                    </StackPanel>
                                </StackPanel>
                            </StackPanel>
                            <StackPanel Margin="{StaticResource ControlMargin}">
                                <TextBlock Style="{StaticResource Styles.Text.FieldHeader.Editable}" Text="测量宽度" />
                                <StackPanel Orientation="Horizontal" >
                                    <TextBox HorizontalAlignment="Left" MaxWidth="150" Style="{StaticResource Styles.Text.FieldContent.Input}"
                                                 Text="{Binding MeasureWidthPos}" />
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBlock Style="{StaticResource Styles.Text.FieldContent.Separator}" Text="|" />
                                        <TextBlock Style="{StaticResource Styles.Text.FieldContent.Unit}">
                                                <Run Text="{Binding MeasureWidthMm,Mode=OneWay}"/><Run Text="mm"/>
                                        </TextBlock>

                                    </StackPanel>
                                </StackPanel>
                            </StackPanel>
                            <Button Content="测量" Style="{StaticResource Styles.Button.Square.Accent2}" Command="{Binding MeasureCmd}"/>

                            

                        </StackPanel>
                        <ItemsControl ItemsSource="{Binding SeriesInfos}" Grid.Column="1" Margin="{StaticResource ControlMargin}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Border x:Name="bd" Background="WhiteSmoke" BorderBrush="{Binding Fill}" BorderThickness="2" Margin="2" Height="35" Width="80" >
                                        <Viewbox>
                                            <TextBlock x:Name="tb" Text="{Binding Avg,StringFormat={}{0:F1}}" FontSize="24" Foreground="{Binding Fill}" FontWeight="Bold"  VerticalAlignment="Center" HorizontalAlignment="Center"  />
                                        </Viewbox>
                                    </Border>
                                    <DataTemplate.Triggers>
                                        <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                            <Setter TargetName="bd" Property="Background" Value="{Binding Fill}"/>
                                            <Setter TargetName="bd" Property="BorderBrush" Value="LightGray"/>
                                            <Setter TargetName="tb" Property="Foreground" Value="WhiteSmoke"/>
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Grid>
                </Border>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Page>