<Window
  x:Class="Ocr.Gui.Views.ImageView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:views="clr-namespace:Ocr.Gui.Views"
  xmlns:controls="clr-namespace:Ocr.Gui.Controls"
  Title="OcrView"
  Width="800"
  Height="450"
  d:DataContext="{d:DesignInstance views:ImageViewModel}"
  mc:Ignorable="d">
  <Window.Resources>
    <CollectionViewSource
      x:Key="View.Words"
      Source="{Binding Words}">
      <CollectionViewSource.SortDescriptions>
        <componentModel:SortDescription
          Direction="Descending"
          PropertyName="Confidence" />
      </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
  </Window.Resources>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition
        Height="*"
        MinHeight="300" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Menu Grid.Row="0">
      <MenuItem Header="File">
        <MenuItem
          Command="{Binding OpenFileCommand}"
          Header="Open image..." />
        <MenuItem
          Command="{Binding SaveEditedImageCommand}"
          Header="Save edited image" />
      </MenuItem>
    </Menu>
    <Grid Grid.Row="1">
      <Grid.RowDefinitions>
        <RowDefinition
          Height="*"
          MinHeight="120" />
        <RowDefinition Height="4" />
        <RowDefinition
          Height="2*"
          MinHeight="120" />
      </Grid.RowDefinitions>
      <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
          <ColumnDefinition
            Width="*"
            MinWidth="120" />
          <ColumnDefinition Width="4" />
          <ColumnDefinition
            Width="2*"
            MinWidth="120" />
        </Grid.ColumnDefinitions>
        <ScrollViewer
          Grid.Column="0"
          HorizontalScrollBarVisibility="Visible"
          VerticalScrollBarVisibility="Disabled">
          <controls:ImageControl Image="{Binding Image}" />
        </ScrollViewer>
        <GridSplitter
          Grid.Column="1"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch"
          ResizeBehavior="PreviousAndNext"
          ResizeDirection="Columns" />
        <ListBox
          Grid.Column="2"
          ItemsSource="{Binding Edited}"
          ScrollViewer.CanContentScroll="False"
          ScrollViewer.HorizontalScrollBarVisibility="Visible"
          ScrollViewer.VerticalScrollBarVisibility="Disabled">
          <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
              <StackPanel
                IsItemsHost="True"
                Orientation="Horizontal" />
            </ItemsPanelTemplate>
          </ListBox.ItemsPanel>
          <ListBox.ItemTemplate>
            <DataTemplate>
              <Border
                BorderBrush="CornflowerBlue"
                BorderThickness="2">
                <controls:ImageControl
                  VerticalAlignment="Stretch"
                  Image="{Binding NotifyOnSourceUpdated=True}" />
              </Border>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
      </Grid>
      <GridSplitter
        Grid.Row="1"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        ResizeBehavior="PreviousAndNext"
        ResizeDirection="Rows" />
      <Grid Grid.Row="2">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="2*" MinWidth="60"></ColumnDefinition>
          <ColumnDefinition Width="4"></ColumnDefinition>
          <ColumnDefinition Width="*" MinWidth="60"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="2" VerticalContentAlignment="Top" HorizontalContentAlignment="Left"
                 TextWrapping="Wrap" Text="{Binding ScannedText}">
        </TextBox>
        <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ResizeBehavior="PreviousAndNext"
                      ResizeDirection="Columns">
        </GridSplitter>
        <ListBox Grid.Column="0"
                 d:ItemsSource="{d:SampleData ItemCount=25}"
                 ItemsSource="{Binding Source={StaticResource View.Words}}"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled">
          <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
              <WrapPanel
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                IsItemsHost="True" />
            </ItemsPanelTemplate>
          </ListBox.ItemsPanel>
          <ListBox.ItemTemplate>
            <DataTemplate>
              <Grid ToolTip="{Binding Text}">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="36" />
                  <ColumnDefinition Width="80" />
                </Grid.ColumnDefinitions>
                <TextBlock
                  Grid.Column="0"
                  Text="{Binding Confidence,
                               StringFormat=0.00}" />
                <TextBlock
                  Grid.Column="1"
                  FontWeight="Bold"
                  Text="{Binding Text}"
                  TextTrimming="CharacterEllipsis"
                  TextWrapping="NoWrap" />
              </Grid>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
      </Grid>
    </Grid>
    <UniformGrid
      IsEnabled="{Binding IsIdle}"
      Grid.Row="2"
      Margin="16"
      Columns="3">
      <CheckBox
        x:Name="EnableThreshold"
        Content="Apply Threshold"
        IsChecked="{Binding ProcessorConfig.EnableThresholding}" />
      <CheckBox
        Content="Resize"
        IsChecked="{Binding ProcessorConfig.EnableResizing}" />
      <Grid Margin="4">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0">Border:</TextBlock>
        <Slider
          x:Name="SldBorder"
          Grid.Column="1"
          Margin="4,0"
          Maximum="25"
          Minimum="0"
          Thumb.DragCompleted="SldBorder_OnDragCompleted"
          Value="10" />
        <TextBlock Grid.Column="2">
          <Run Text="{Binding Value, ElementName=SldBorder, FallbackValue=0, StringFormat=0.00}" />
        </TextBlock>
      </Grid>
    </UniformGrid>
  </Grid>
</Window>