<?xml version="1.0" encoding="utf-8" ?>
<UserControl
    x:Class="Ghost.Editor.Views.Controls.LogViewer"
    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:fic="using:FluentIcons.WinUI"
    xmlns:local="using:Ghost.Editor.Views.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <UserControl.Resources>
        <local:LogLevelToColorConverter x:Key="LogLevelToColorConverter" />
        <local:LogLevelToSymbolConverter x:Key="LogLevelToSymbolConverter" />

        <DataTemplate x:Key="LogItemTemplate">
            <ItemContainer>
                <Grid Padding="12,8">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <fic:FluentIcon
                        Margin="0,0,8,0"
                        VerticalAlignment="Center"
                        Foreground="{Binding Level, Converter={StaticResource LogLevelToColorConverter}}"
                        Icon="{Binding Level, Converter={StaticResource LogLevelToSymbolConverter}}"
                        IconSize="Size20"
                        IconVariant="Filled" />

                    <TextBlock
                        Grid.Column="1"
                        Margin="0,0,8,0"
                        VerticalAlignment="Center"
                        FontFamily="Consolas"
                        FontSize="11"
                        Foreground="{StaticResource TextFillColorSecondaryBrush}">
                        <Run Text="[" />
                        <Run Text="{Binding Timestamp}" />
                        <Run Text="]" />
                    </TextBlock>

                    <TextBlock
                        Grid.Column="2"
                        VerticalAlignment="Center"
                        Text="{Binding Message}"
                        TextTrimming="WordEllipsis"
                        TextWrapping="NoWrap" />
                </Grid>
            </ItemContainer>
        </DataTemplate>
    </UserControl.Resources>

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

        <!--  Toolbar  -->
        <Border
            Grid.Row="0"
            Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
            BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
            BorderThickness="0,0,0,1">
            <StackPanel Margin="8,4" Orientation="Horizontal">
                <Button
                    x:Name="ClearButton"
                    Margin="0,0,8,0"
                    Click="ClearButton_Click"
                    Content="Clear" />
                <CheckBox
                    x:Name="AutoScrollCheckBox"
                    Margin="0,0,8,0"
                    Content="Auto Scroll"
                    IsChecked="True" />
                <CheckBox
                    x:Name="ShowStackTraceCheckBox"
                    Margin="0,0,8,0"
                    Checked="ShowStackTraceCheckBox_Checked"
                    Content="Stack Trace"
                    Unchecked="ShowStackTraceCheckBox_Unchecked" />

                <!--  Log level filters  -->
                <TextBlock
                    Margin="16,0,8,0"
                    VerticalAlignment="Center"
                    Text="Show:" />
                <CheckBox
                    x:Name="ShowInfoCheckBox"
                    Margin="0,0,4,0"
                    Content="Info"
                    IsChecked="True" />
                <CheckBox
                    x:Name="ShowWarningCheckBox"
                    Margin="0,0,4,0"
                    Content="Warning"
                    IsChecked="True" />
                <CheckBox
                    x:Name="ShowErrorCheckBox"
                    Margin="0,0,4,0"
                    Content="Error"
                    IsChecked="True" />
                <CheckBox
                    x:Name="ShowDebugCheckBox"
                    Margin="0,0,4,0"
                    Content="Debug"
                    IsChecked="True" />
            </StackPanel>
        </Border>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="0.75*" MaxWidth="1000" />
            </Grid.ColumnDefinitions>

            <!--  Log display  -->
            <ScrollView
                x:Name="LogScrollView"
                Grid.Column="0"
                HorizontalScrollBarVisibility="Hidden"
                HorizontalScrollMode="Disabled"
                VerticalScrollBarVisibility="Auto"
                VerticalScrollMode="Auto"
                ZoomMode="Disabled">
                <ItemsView
                    x:Name="LogItemsView"
                    Padding="4"
                    ItemTemplate="{StaticResource LogItemTemplate}"
                    SelectionChanged="LogItemsView_SelectionChanged" />
            </ScrollView>

            <ScrollView
                Grid.Column="1"
                BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
                BorderThickness="1,0,0,0"
                HorizontalScrollMode="Disabled">
                <StackPanel
                    Padding="12"
                    Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
                    Spacing="8">
                    <TextBlock Style="{StaticResource BodyLargeStrongTextBlockStyle}" Text="Selected Log Details" />
                    <TextBlock
                        Margin="8,0,0,0"
                        Style="{StaticResource BodyStrongTextBlockStyle}"
                        Text="Message:" />
                    <TextBlock
                        x:Name="SelectedLogMessageTextBlock"
                        Margin="16,0,0,0"
                        TextWrapping="Wrap" />
                    <TextBlock
                        Margin="8,0,0,0"
                        Style="{StaticResource BodyStrongTextBlockStyle}"
                        Text="Stack Trace:" />
                    <TextBlock
                        x:Name="SelectedLogStackTraceTextBlock"
                        Margin="16,0,0,0"
                        TextWrapping="Wrap" />
                </StackPanel>
            </ScrollView>
        </Grid>
    </Grid>
</UserControl>
