<UserControl x:Class="InABox.Wpf.Console.ConsoleControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:InABox.Wpf.Console"
		xmlns:wpf="clr-namespace:InABox.WPF"
        mc:Ignorable="d"
        x:Name="Window"
        DataContext="{Binding ElementName=Window}">
    <UserControl.Resources>
        <wpf:BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"
                                       TrueValue="Visible" FalseValue="Collapsed"/>

        <Style TargetType="ItemsControl" x:Key="LogViewerStyle">

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <ScrollViewer CanContentScroll="True">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <DataTemplate DataType="{x:Type local:LogEntry}">
            <Grid IsSharedSizeScope="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="Date" Width="120" />
                    <ColumnDefinition SharedSizeGroup="Type" Width="60" />
                    <ColumnDefinition SharedSizeGroup="User" Width="120" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding DateTime}" Grid.Column="0" FontFamily="Courier New"
                           Margin="5,0,5,0" />

                <TextBlock Text="{Binding Type}" Grid.Column="1" FontFamily="Courier New"
                           Margin="5,0,5,0" />

                <TextBlock Text="{Binding User}" Grid.Column="2" FontFamily="Courier New"
                           Margin="0,0,2,0" />

                <TextBlock Text="{Binding Message}" Grid.Column="3" FontFamily="Courier New"
                           TextWrapping="Wrap" />
            </Grid>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:CollapsibleLogEntry}">
            <Grid IsSharedSizeScope="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="Date" Width="120" />
                    <ColumnDefinition SharedSizeGroup="Type" Width="60" />
                    <ColumnDefinition SharedSizeGroup="User" Width="120" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <TextBlock Text="{Binding DateTime}" Grid.Column="0" FontFamily="Courier New"
                           Margin="5,0,5,0" />

                <TextBlock Text="{Binding Type}" Grid.Column="1" FontFamily="Courier New"
                           Margin="5,0,5,0" />

                <TextBlock Text="{Binding User}" Grid.Column="2" FontFamily="Courier New"
                           Margin="0,0,2,0" />

                <TextBlock Text="{Binding Message}" Grid.Column="3" FontFamily="Courier New"
                           TextWrapping="Wrap" />

                <ToggleButton x:Name="Expander" Grid.Row="1" Grid.Column="0"
                              VerticalAlignment="Top" Content="+" HorizontalAlignment="Right" />

                <ItemsControl ItemsSource="{Binding Contents}" Style="{StaticResource LogViewerStyle}"
                              Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
                              x:Name="Contents" Visibility="Collapsed" />

            </Grid>
            <DataTemplate.Triggers>
                <Trigger SourceName="Expander" Property="IsChecked" Value="True">
                    <Setter TargetName="Contents" Property="Visibility" Value="Visible" />
                    <Setter TargetName="Expander" Property="Content" Value="-" />
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>

    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Label Content="Search" VerticalContentAlignment="Center" HorizontalAlignment="Left" />
            <TextBox x:Name="Search" Margin="5,0,0,0" DockPanel.Dock="Right" Background="LightYellow"
                     VerticalContentAlignment="Center" PreviewKeyDown="Search_KeyDown" TextChanged="Search_TextChanged"
                     HorizontalAlignment="Stretch" Grid.Column="1" />
            <CheckBox x:Name="UseRegEx" Content="Use Regular Expressions" HorizontalAlignment="Right"
                      VerticalAlignment="Center" Visibility="Visible" Margin="5,0,5,0" Grid.Column="2"
                      Checked="UseRegEx_Checked" Unchecked="UseRegEx_Unchecked" />
            <CheckBox x:Name="ShowImportant" Content="Show only Important" HorizontalAlignment="Right"
                      VerticalAlignment="Center" Visibility="Visible" Margin="5,0,5,0" Grid.Column="3"
                      Checked="ShowImportant_Checked" Unchecked="ShowImportant_Unchecked"/>
            <Button x:Name="LoadLog" Grid.Column="4" Content="Load Log File" Padding="4,1,4,1" Margin="5,5,5,5"
                    Click="LoadLog_Click" Visibility="{Binding Path=ShowLoadLogButton,Mode=OneWay,Converter={StaticResource BoolToVisibilityConverter}}" />
            <Button x:Name="CloseLog" Grid.Column="4" Content="Close Log File" Padding="4,1,4,1" Margin="5,5,5,5"
                    Click="CloseLog_Click" Visibility="{Binding Path=ShowCloseLogButton,Mode=OneWay,Converter={StaticResource BoolToVisibilityConverter}}" />
        </Grid>
        <Border x:Name="LogBorder" BorderBrush="Gray" Grid.Row="1" BorderThickness="0.75" Margin="5" Padding="2">
            <Border.Style>
                <Style TargetType="Border">
                    <Setter Property="Background" Value="LightYellow"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Enabled,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:ConsoleControl}}}" Value="False">
                            <Setter Property="Background" Value="WhiteSmoke"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <ItemsControl x:Name="Log" DataContext="{Binding Filtered}" ItemsSource="{Binding}" Style="{StaticResource LogViewerStyle}">
                <ItemsControl.Template>
                    <ControlTemplate>
                        <ScrollViewer CanContentScroll="True">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </ControlTemplate>
                </ItemsControl.Template>
            </ItemsControl>
        </Border>
        <Label x:Name="Error" Content="" Grid.Row="2" HorizontalAlignment="Right" Visibility="Collapsed" />
    </Grid>
</UserControl>