<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:Avalonia.Diagnostics.ViewModels"
             xmlns:conv="clr-namespace:Avalonia.Diagnostics.Converters"
             xmlns:controls="clr-namespace:Avalonia.Diagnostics.Controls"
             x:Class="Avalonia.Diagnostics.Views.EventsPageView"
             Margin="2">
  <UserControl.Styles>
    <Style Selector="TextBlock.nav" >
      <Setter Property="TextDecorations">
        <TextDecorationCollection>
          <TextDecoration Location="Underline" Stroke="Black" StrokeThickness="1" StrokeDashArray="2,2"/>
        </TextDecorationCollection>
      </Setter>
    </Style>

    <Style Selector="TextBlock.nav:pointerover" >
      <Setter Property="Foreground" Value="{DynamicResource ThemeAccentBrush}" />
      <Setter Property="Cursor" Value="Help" />
    </Style>

    <Style Selector="ListBoxItem" >
      <Setter Property="BorderThickness" Value="1" />
    </Style>

    <Style Selector="ListBoxItem:selected /template/ ContentPresenter" >
      <Setter Property="BorderBrush" Value="Black" />
    </Style>

    <Style Selector="ListBoxItem.handled" >
      <Setter Property="Background" Value="#d9ffdc" />
    </Style>
  </UserControl.Styles>

  <Grid ColumnDefinitions="1.1*,4,3*">

    <Grid Grid.Column="0" RowDefinitions="Auto,*,Auto">

      <controls:FilterTextBox Grid.Row="0" Margin="0,0,0,2"
                              DataContext="{Binding EventsFilter}"
                              Text="{Binding FilterString}"
                              Watermark="Filter events"
                              UseCaseSensitiveFilter="{Binding UseCaseSensitiveFilter}"
                              UseWholeWordFilter="{Binding UseWholeWordFilter}"
                              UseRegexFilter="{Binding UseRegexFilter}" />

      <TreeView Grid.Row="1" Items="{Binding Nodes}" SelectedItem="{Binding SelectedNode, Mode=TwoWay}" >
        <TreeView.DataTemplates>
          <TreeDataTemplate DataType="vm:EventTreeNodeBase"
                            ItemsSource="{Binding Children}">
            <CheckBox Content="{Binding Text}" IsChecked="{Binding IsEnabled, Mode=TwoWay}" />
          </TreeDataTemplate>
        </TreeView.DataTemplates>
        <TreeView.Styles>
          <Style Selector="TreeViewItem">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsVisible" Value="{Binding IsVisible}" />
          </Style>
        </TreeView.Styles>
      </TreeView>

      <StackPanel Grid.Row="2" Margin="0,2" Orientation="Horizontal" Spacing="2">
        <Button Content="Disable all" Command="{Binding DisableAll}" />
        <Button Content="Enable default" Command="{Binding EnableDefault}" />
      </StackPanel>

    </Grid>

    <GridSplitter Width="4" Grid.Column="1" />

    <Grid RowDefinitions="*,4,2*,Auto" Grid.Column="2">

      <ListBox Name="EventsList" Items="{Binding RecordedEvents}"
               SelectedItem="{Binding SelectedEvent, Mode=TwoWay}">

        <ListBox.ItemTemplate>
          <DataTemplate>
            <ListBoxItem Classes.handled="{Binding IsHandled}">
              <Grid ColumnDefinitions="Auto,Auto,*,Auto">

                <StackPanel Grid.Column="0" Spacing="2" Orientation="Horizontal" >
                  <TextBlock Tag="{Binding Event}" DoubleTapped="NavigateTo" Text="{Binding Event.Name}" FontWeight="Bold" Classes="nav" />
                  <TextBlock Text="on" />
                  <TextBlock Tag="{Binding Originator}" DoubleTapped="NavigateTo" Text="{Binding Originator.HandlerName}" Classes="nav" />
                </StackPanel>

                <StackPanel Margin="2,0,0,0" Grid.Column="1" Spacing="2" Orientation="Horizontal" IsVisible="{Binding IsHandled}" >
                  <TextBlock Text="::" />
                  <TextBlock Text="Handled by" />
                  <TextBlock Tag="{Binding HandledBy}" DoubleTapped="NavigateTo" Text="{Binding HandledBy.HandlerName}" Classes="nav" />
                </StackPanel>

                <StackPanel Grid.Column="3" Orientation="Horizontal" HorizontalAlignment="Right">
                  <TextBlock Text="Routing (" />
                  <TextBlock Text="{Binding Event.RoutingStrategies}"/>
                  <TextBlock Text=")"/>
                </StackPanel>

              </Grid>
            </ListBoxItem>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

      <GridSplitter Height="4" Grid.Row="1" />

      <DockPanel Grid.Row="2" LastChildFill="True">
        <TextBlock DockPanel.Dock="Top" FontSize="16" Text="Event chain:" />

        <ListBox Items="{Binding SelectedEvent.EventChain}">
          <ListBox.ItemTemplate>
            <DataTemplate>
              <ListBoxItem Classes.handled="{Binding Handled}">
                <StackPanel Orientation="Vertical">

                  <Rectangle IsVisible="{Binding BeginsNewRoute}" StrokeDashArray="2,2" StrokeThickness="1" Stroke="Gray" />

                  <StackPanel Orientation="Horizontal" Spacing="2">
                    <TextBlock Text="{Binding Route}" FontWeight="Bold" />
                    <TextBlock Tag="{Binding}" DoubleTapped="NavigateTo" Text="{Binding HandlerName}" Classes="nav" />
                  </StackPanel>

                </StackPanel>
              </ListBoxItem>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>

      </DockPanel>

      <StackPanel Orientation="Horizontal" Grid.Row="3" Spacing="2" Margin="0,2">
        <Button Content="Clear" Command="{Binding Clear}" />
      </StackPanel>

    </Grid>
  </Grid>
</UserControl>
