<UserControl x:Class="HistoryUI.Views.Status"
             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:hc="https://handyorg.github.io/handycontrol"
             xmlns:local="clr-namespace:HistoryUI.Views"
             mc:Ignorable="d" 
             xmlns:ScottPlot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid Background="{StaticResource BackgroundColor}">
        <Grid Background="#50FFFFFF" Panel.ZIndex="1" Visibility="{Binding Loading}">
            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                <TextBlock FontSize="32">Loading Data...</TextBlock>
            </StackPanel>
        </Grid>
        <Grid  Margin="8">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="0"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="8"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="16"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="8"/>
            </Grid.RowDefinitions>

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

                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition Width="8"/>
                </Grid.ColumnDefinitions>
                <GroupBox Header="Start Time" BorderThickness="0">
                    <hc:DateTimePicker MinWidth="160" Height="32" SelectedDateTime="{Binding StartTime}"/>
                </GroupBox>
                <GroupBox Grid.Column="2" Header="End Time" BorderThickness="0">
                    <hc:DateTimePicker MinWidth="160" Height="32" SelectedDateTime="{Binding EndTime}"/>
                </GroupBox>
                <GroupBox Grid.Column="4" Header="Mini8" BorderThickness="0">
                    <hc:ComboBox MinWidth="160" Height="32" ItemsSource="{Binding Hardwares.Mini8s}" SelectedItem="{Binding SelectedMini8}" DisplayMemberPath="Value.Name"/>
                </GroupBox>
                <GroupBox Grid.Column="6" Header="Channel" BorderThickness="0">
                    <hc:ComboBox MinWidth="160" Height="32" ItemsSource="{Binding Channels}" SelectedItem="{Binding SelectedChannel}" DisplayMemberPath="Value.Name"/>
                </GroupBox>

                <Button Grid.Column="8" Margin="0,8,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource FunctionButton}" Command="{Binding QueryCommand}" MinWidth="80" Height="32">Query</Button>
                <Button Grid.Column="10" Margin="0,8,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource FunctionButton}" Command="{Binding ExitCommand}" Background="{StaticResource EmergencyColor}" MinWidth="80" Height="32">Exit</Button>
            </Grid>

            <ContentControl Grid.Row="2" Content="{Binding PlotControl, Mode=OneTime}" TouchEnter="ContentControl_TouchEnter" TouchLeave="ContentControl_TouchLeave"  TouchMove="ContentControl_TouchMove"/>

            <Grid Grid.Row="4" Margin="16,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition Width="8"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="8"/>
                    <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>

                <GroupBox Header="Legend">
                    <Grid  VerticalAlignment="Center" Margin="0,2,0,0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                            <RowDefinition Height="4"/>
                            <RowDefinition Height="auto"/>
                        </Grid.RowDefinitions>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="4"/>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="4"/>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="4"/>
                                <ColumnDefinition Width="auto"/>
                                <!--<ColumnDefinition Width="4"/>
                                <ColumnDefinition Width="auto"/>-->
                            </Grid.ColumnDefinitions>
                            <StackPanel VerticalAlignment="Center">
                                <TextBlock HorizontalAlignment="Center">Limit</TextBlock>
                                <Line X1="10" Y1="0" X2="70" Y2="0" HorizontalAlignment="Center" Margin="0,2" Stroke="#FF0000" StrokeThickness="2"  StrokeDashArray="2,2"/>
                            </StackPanel>
                            <StackPanel Grid.Column="2" VerticalAlignment="Center">
                                <TextBlock HorizontalAlignment="Center">Warning</TextBlock>
                                <Line X1="10" Y1="0" X2="70" Y2="0" HorizontalAlignment="Center" Margin="0,2" Stroke="#FFA500" StrokeThickness="2"  StrokeDashArray="2,2"/>
                            </StackPanel>
                            <StackPanel Grid.Column="4" VerticalAlignment="Center">
                                <TextBlock HorizontalAlignment="Center">SetPoint</TextBlock>
                                <Line X1="10" Y1="0" X2="70" Y2="0" HorizontalAlignment="Center" Margin="0,2" Stroke="#54FF9F" StrokeThickness="2"  StrokeDashArray="2,2"/>
                            </StackPanel>
                            <!--<StackPanel Grid.Column="6" VerticalAlignment="Center">
                                <TextBlock HorizontalAlignment="Center">Trend</TextBlock>
                                <Line X1="10" Y1="0" X2="70" Y2="0" HorizontalAlignment="Center" Margin="0,2" Stroke="#ff9933" StrokeThickness="2" StrokeDashArray="2,2"/>
                            </StackPanel>-->
                            <StackPanel Grid.Column="6" VerticalAlignment="Center">
                                <TextBlock HorizontalAlignment="Center">PV</TextBlock>
                                <Line X1="10" Y1="0" X2="70" Y2="0" HorizontalAlignment="Center" Margin="0,2" Stroke="#0000CD" StrokeThickness="2"/>
                            </StackPanel>
                        </Grid>
                        <Grid Grid.Row="2" Margin="8,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="auto"/>
                            </Grid.ColumnDefinitions>
                            <StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal">
                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Margin="4">AutoTune</TextBlock>

                                <Rectangle Width="12" Height="24" HorizontalAlignment="Center" Margin="0" Fill="#4000FF00" StrokeThickness="0"/>
                            </StackPanel>
                            <StackPanel Grid.Row="2"  Grid.Column="2" Orientation="Horizontal">
                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Margin="4">AutoTune Failed</TextBlock>
                                <Rectangle Width="12" Height="24" HorizontalAlignment="Center" Margin="0" Fill="#40828282" StrokeThickness="0"/>

                            </StackPanel>
                            <StackPanel Grid.Row="2"  Grid.Column="4" Orientation="Horizontal">
                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Margin="4">TcBorken</TextBlock>
                                <Rectangle Width="12" Height="24" HorizontalAlignment="Center" Margin="0" Fill="#40ff6666" StrokeThickness="0"/>

                            </StackPanel>
                        </Grid>
                    </Grid>
                </GroupBox>

                <StackPanel Grid.ColumnSpan="6" VerticalAlignment="Center">
                    <!--<TextBlock Background="{StaticResource WarningColor}" VerticalAlignment="Center" HorizontalAlignment="Center" Padding="4" TextDecorations="Underline">
                        <Run>Trend Line:  </Run>
                        <Run Text="{Binding Hint}"/>
                    </TextBlock>-->
                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">
                    <Run Text="{Binding Results.Count, Mode=OneWay}"/>
                    <Run> Item(s)</Run>
                    </TextBlock>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">

                        <Button VerticalAlignment="Center" HorizontalAlignment="Center" Height="24" Style="{StaticResource FunctionButton}" 
                                Command="{Binding ShowCommand}" CommandParameter="Show" Margin="2">Show Data</Button>
                        <Button VerticalAlignment="Center" HorizontalAlignment="Center" Height="24" Style="{StaticResource FunctionButton}"
                                Command="{Binding ShowCommand}" CommandParameter="Hide" Margin="2">Hide Data</Button>
                    </StackPanel>
                </StackPanel>
                <StackPanel Grid.Column="4" HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Horizontal">
                    <Button VerticalAlignment="Center" Width="48" Command="{Binding ReScaleCommand}" CommandParameter="add">↑</Button>
                    <Button VerticalAlignment="Center" Width="48" Margin="8,0" Command="{Binding ReScaleCommand}" CommandParameter="minus">↓</Button>
                    <Button VerticalAlignment="Center" Width="48" Command="{Binding ReScaleCommand}" CommandParameter="+">+</Button>
                    <Button VerticalAlignment="Center" Width="48" Margin="8,0" Command="{Binding ReScaleCommand}" CommandParameter="-">-</Button>
                    <Button VerticalAlignment="Center"  Command="{Binding ReScaleCommand}" CommandParameter="Re">AutoScale</Button>
                </StackPanel>
            </Grid>

            <DataGrid Grid.Row="6" ItemsSource="{Binding Results}" Visibility="{Binding DataGirdVis}" 
                      Background="Transparent" BorderThickness="1" Margin="16,0" Height="380"
                      CanUserAddRows="False" 
                      CanUserDeleteRows="False" 
                      CanUserResizeColumns="False"
                      CanUserResizeRows="False"
                      IsReadOnly="True"
                      HorizontalScrollBarVisibility="Disabled"
                      TextElement.FontSize="16">
                <DataGrid.Columns>
                    <!--<DataGridTextColumn Width="50" CanUserSort="True" Header="Index" Binding="{Binding Key}"/>-->
                    <DataGridTemplateColumn Width="170" Header="Time">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock>
                                      <Run Text="{Binding Value.DateTime , StringFormat={}{0:yyyy-MM-dd}}"/>
                                      <Run> </Run>
                                      <Run Text="{Binding Value.DateTime , StringFormat={}{0:HH:mm:ss}}"/>
                                </TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Width="*" CanUserSort="False" Header="Tc" Binding="{Binding Value.SensorBreakAlarm}"/>
                    <DataGridTextColumn Width="*" CanUserSort="False" Header="SetPoint" Binding="{Binding Value.SetPoint,StringFormat=0.0}"/>
                    <DataGridTextColumn Width="*" CanUserSort="False" Header="PV" Binding="{Binding Value.PV ,StringFormat=0.0}"/>
                    <DataGridTextColumn Width="*" CanUserSort="False" Header="Caps" Binding="{Binding Value.Caps}"/>
                    <DataGridTextColumn Width="*" CanUserSort="False" Header="CapsW" Binding="{Binding Value.CapsWarning}"/>
                    <DataGridTextColumn Width="*" CanUserSort="False" Header="FloorW" Binding="{Binding Value.FloorWarning}"/>
                    <DataGridTextColumn Width="*" CanUserSort="False" Header="Floor" Binding="{Binding Value.Floor}"/>
                    <DataGridTextColumn Width="*" CanUserSort="False" Header="AT" Binding="{Binding Value.AutoTuneStatus}"/>
                    <DataGridTemplateColumn Width="*" Header="Output">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock>
                                    <Run Text="{Binding Value.WorkingOutput, StringFormat=0.0}"/>
                                    <Run> %</Run>
                                </TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Width="auto" MinWidth="48" CanUserSort="False" Header="P" Binding="{Binding Value.Running_P ,StringFormat=0.0}"/>
                    <DataGridTextColumn Width="auto" MinWidth="48" CanUserSort="False" Header="I" Binding="{Binding Value.Running_I ,StringFormat=0.0}"/>
                    <DataGridTextColumn Width="auto" MinWidth="48" CanUserSort="False" Header="D" Binding="{Binding Value.Running_D ,StringFormat=0.0}"/>
                </DataGrid.Columns>
            </DataGrid>

        </Grid>
    </Grid>


</UserControl>
