<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:DodoSSH.Client.Shell.ViewModels"
             x:Class="DodoSSH.Client.App.Views.KnownHostsScreen"
             x:DataType="vm:KnownHostsViewModel">

  <!--
    The host keys this keychain has approved.

    These were a category on the keychain screen, alongside SSH keys and passwords, and they do not belong
    there: the other two are things a person creates and edits, and a pin is a decision recorded at the
    moment of connecting. Nobody goes looking for one in a list of credentials. They are also the only items
    with a workflow of their own — compare a fingerprint against what the operator published — and that
    workflow needs a filter and a column layout the shared table could not give them.

    The data layer did not move and did not change. Every pin is still a vault item, still end-to-end
    encrypted, still synced; see KnownHostSecret. What is here is a screen over VaultViewModel.KnownHostPins.
  -->

  <Grid ColumnDefinitions="*,244">

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

      <Border Grid.Row="0" Padding="14,0" Height="44"
              BorderBrush="{StaticResource Border}" BorderThickness="0,0,0,1">
        <Grid ColumnDefinitions="Auto,*,Auto" VerticalAlignment="Center">
          <TextBlock Grid.Column="0" Classes="mono" Text="HOST KEYS" FontSize="12"
                     FontWeight="SemiBold" LetterSpacing="1" Foreground="{StaticResource Text}"
                     VerticalAlignment="Center" />
          <TextBlock Grid.Column="1" Classes="mono" Text="{Binding Summary}" FontSize="10.5"
                     Foreground="{StaticResource TextFaint}" Margin="10,0,0,0"
                     TextTrimming="CharacterEllipsis" VerticalAlignment="Center" />

          <!--
            Matches fingerprints as well as host names, which is the point of it. What somebody does with
            this screen is check whether a published SHA256:… is the one they approved, and searching only
            by name would answer a different question.
          -->
          <TextBox Grid.Column="2" x:Name="PinFilter" Text="{Binding Filter}" Width="240"
                   PlaceholderText="filter by host or fingerprint" VerticalAlignment="Center" />
        </Grid>
      </Border>

      <Grid Grid.Row="1" ColumnDefinitions="2,1.4*,58,104,*,96" Margin="0,6,14,6"
            IsVisible="{Binding HasVisiblePins}">
        <TextBlock Grid.Column="1" Classes="label" Text="HOST" FontSize="9.5" LetterSpacing="1"
                   Margin="12,0,8,0" />
        <TextBlock Grid.Column="2" Classes="label" Text="PORT" FontSize="9.5" LetterSpacing="1" />
        <TextBlock Grid.Column="3" Classes="label" Text="ALGORITHM" FontSize="9.5" LetterSpacing="1" />
        <TextBlock Grid.Column="4" Classes="label" Text="FINGERPRINT" FontSize="9.5" LetterSpacing="1" />
        <TextBlock Grid.Column="5" Classes="label" Text="APPROVED" FontSize="9.5" LetterSpacing="1" />
      </Grid>

      <ListBox Grid.Row="2" x:Name="PinList" Focusable="True"
               ItemsSource="{Binding VisiblePins}"
               SelectedItem="{Binding Selected}">
        <ListBox.ItemTemplate>
          <DataTemplate x:DataType="vm:KnownHostRowViewModel">
            <Grid ColumnDefinitions="2,1.4*,58,104,*,96" Margin="0,7,14,7">
              <Border Grid.Column="0" Classes="rowmark" />
              <TextBlock Grid.Column="1" Classes="mono" Text="{Binding Host}" FontSize="12"
                         FontWeight="Medium" Foreground="{StaticResource Text}" Margin="12,0,8,0"
                         TextTrimming="CharacterEllipsis" VerticalAlignment="Center" />
              <TextBlock Grid.Column="2" Classes="mono" Text="{Binding Port}" FontSize="10.5"
                         Foreground="{StaticResource TextDim}" VerticalAlignment="Center" />
              <TextBlock Grid.Column="3" Classes="mono" Text="{Binding Algorithm}" FontSize="10"
                         Foreground="{StaticResource TextDim}" Margin="0,0,8,0"
                         TextTrimming="CharacterEllipsis" VerticalAlignment="Center" />
              <!--
                Never trimmed, and this column is why the table is laid out the way it is. The only thing
                anybody does with a fingerprint is compare it character by character against one an operator
                published; an ellipsis in the middle turns that into a glance, which is the habit the whole
                mechanism exists to replace.
              -->
              <TextBlock Grid.Column="4" Classes="mono" Text="{Binding Fingerprint}" FontSize="10.5"
                         Foreground="{StaticResource TextFaint}" Margin="0,0,8,0"
                         VerticalAlignment="Center" />
              <TextBlock Grid.Column="5" Classes="mono" Text="{Binding Approved}" FontSize="10"
                         Foreground="{StaticResource TextFaint}" VerticalAlignment="Center" />
            </Grid>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

      <TextBlock Grid.Row="2" Classes="hint" Text="{Binding EmptyMessage}" FontSize="12"
                 Margin="24" HorizontalAlignment="Center" VerticalAlignment="Center"
                 TextAlignment="Center" MaxWidth="340"
                 IsVisible="{Binding !HasVisiblePins}" />

    </Grid>

    <Border Grid.Column="1" Background="{StaticResource Sidebar}"
            BorderBrush="{StaticResource Border}" BorderThickness="1,0,0,0">
      <ScrollViewer>
        <StackPanel Margin="14,16" Spacing="6">

          <TextBlock Classes="hint" FontSize="12"
                     Text="Choose a pinned key to see it in full, and to withdraw it."
                     IsVisible="{Binding !HasSelection}" />

          <StackPanel Spacing="6" IsVisible="{Binding HasSelection}">
            <TextBlock Classes="mono" Text="{Binding Selected.Label}" FontSize="13"
                       FontWeight="SemiBold" Foreground="{StaticResource Text}" TextWrapping="Wrap" />

            <Border Classes="chip warn" HorizontalAlignment="Left"
                    IsVisible="{Binding !Selected.IsDialledByAHost}">
              <TextBlock Text="no host uses this" />
            </Border>

            <TextBlock Classes="label" Text="FINGERPRINT" Margin="0,12,0,4" />
            <Border Background="{StaticResource Raised}" BorderBrush="{StaticResource Border}"
                    BorderThickness="1" CornerRadius="4" Padding="8">
              <SelectableTextBlock Classes="mono" Text="{Binding Selected.Fingerprint}"
                                   FontSize="10.5" Foreground="{StaticResource TextDim}"
                                   TextWrapping="Wrap" />
            </Border>

            <TextBlock Classes="label" Text="APPROVED" Margin="0,12,0,4" />
            <TextBlock Classes="mono" Text="{Binding Selected.Approved}" FontSize="11"
                       Foreground="{StaticResource TextDim}" />
            <!--
              Said rather than implied. No vault item carries a timestamp, so this date is read back out of
              the item's own version 7 id — which records when the pin was created and knows nothing about
              it being re-approved since. Presenting that as "last used" would be inventing a fact.
            -->
            <TextBlock Classes="hint" FontSize="11" TextWrapping="Wrap"
                       Text="Taken from the item's identifier, so it is when this key was first approved — not when it was last checked. Nothing here records that." />

            <TextBlock Classes="hint" FontSize="11" TextWrapping="Wrap" Margin="0,12,0,0"
                       Text="A pin outlives whatever it was approved for: deleting a host leaves it, and so does changing a host's address. That is deliberate — trust is about the endpoint, not the bookmark." />

            <Button Classes="danger" Content="FORGET THIS HOST KEY" Margin="0,12,0,0"
                    HorizontalAlignment="Left"
                    Command="{Binding ForgetSelectedCommand}"
                    ToolTip.Tip="Withdraws trust. The next connection to this endpoint asks you to check the fingerprint again, which is the safe direction to be wrong in — and it is the way back from a server that was legitimately rebuilt." />
          </StackPanel>

        </StackPanel>
      </ScrollViewer>
    </Border>

  </Grid>

</UserControl>
