<UserControl x:Class="ksBroadcastingTestClient.Autopilot.AutopilotView"
             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:ksBroadcastingTestClient.Autopilot"
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Name="viewAutopilot">
    <UserControl.Resources>
        <CollectionViewSource x:Key="src" Source="{Binding Cars}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Position" Direction="Ascending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <!-- Control Panel -->
        <Grid Grid.ColumnSpan="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="230" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Button Content="{Binding AutopilotStateText}" Command="{Binding ToggleAutopilotCommand}" Width="60" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="3,3,0,0" />
                <Expander IsExpanded="True" Grid.Column="1" Grid.ColumnSpan="99">
                    <StackPanel Orientation="Horizontal">
                        <!-- Pressure weight sliders-->
                        <ItemsControl ItemsSource="{Binding PressureCategoryWeights}" Width="200">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel  Orientation="Vertical"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>

                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="60" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="40" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="{Binding Category}" />
                                        <Slider Value="{Binding RawValue}" Minimum="0" Maximum="2" Grid.Column="1" />
                                        <TextBlock Text="{Binding RawValue, StringFormat=P0}" Grid.Column="2" />
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <!-- Camera panel -->
                        <local:CameraManagementView DataContext="{Binding CamManagerVM}" />
                    </StackPanel>
                </Expander>
                <TextBlock Text="{Binding CamManagerVM.CameraState}" Grid.Column="3" />
                <TextBlock Text="{Binding AutopilotState}" Grid.Column="4" />

            </Grid>
        </Grid>

        <!-- Car list -->
        <ScrollViewer Grid.Row="1">
            <ItemsControl Name="listViewCars" ItemsSource="{Binding Source={StaticResource src}}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel  Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>

                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <local:AutopilotCarView Margin="0,3,0,0" >
                            <local:AutopilotCarView.InputBindings>
                                <MouseBinding Gesture="LeftDoubleClick" Command="{Binding ElementName=viewAutopilot, Path=DataContext.RequestFocusedCarCommand}" CommandParameter="{Binding}" />
                            </local:AutopilotCarView.InputBindings>
                        </local:AutopilotCarView>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
        
        <!-- MiniLog -->
        <ScrollViewer Grid.Row="1" Grid.Column="1">
            <ItemsControl ItemsSource="{Binding MiniLog}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel  Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>

                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</UserControl>
