<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.ImportScreen"
             x:DataType="vm:ImportViewModel">

  <!--
    Importing ~/.ssh/config.

    A preview and then a button, rather than one action, and that is the whole design. This reads a file
    the application did not write, out of the user's home directory, and a real ssh_config often holds
    forty entries for machines that stopped existing years ago. So scanning writes nothing and the list
    says what each entry means; importing is a separate press on a set somebody has looked at.

    Reachable from the preferences screen and not from the nav rail. It is a task rather than a
    destination — done once, or once a year — and a seventh rail entry would cost every screen a slot for
    something almost nobody is looking at.

    ── ◆ THE ONE TICK THAT READS PRIVATE KEYS ─────────────────────────────────────────────────────────────
    Below the list, off, and drawn only where the scan actually found an IdentityFile. It is the only control
    in this application that opens private key material out of a directory the user did not point at file by
    file, so three things about how it is drawn are load-bearing rather than tidy: it is a default nobody
    arrives at by accident, the sentence beside it names the directory rather than saying "keys", and nothing
    is read until IMPORT is pressed — so ticking it, reading the sentence and unticking it costs nothing.

    What comes back afterwards is the report under the list: one line per key file, saying which were stored,
    which are protected by a passphrase this cannot know, and which were not there at all. That is reported
    rather than previewed for the same reason — previewing would mean reading them.
  -->

  <Grid RowDefinitions="Auto,Auto,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="IMPORT SSH CONFIG" FontSize="12"
                   FontWeight="SemiBold" LetterSpacing="1" Foreground="{StaticResource Text}"
                   VerticalAlignment="Center" />
        <TextBlock Grid.Column="1" Classes="mono" Text="{Binding ConfigPath}" FontSize="10.5"
                   Foreground="{StaticResource TextFaint}" Margin="10,0" VerticalAlignment="Center"
                   TextTrimming="CharacterEllipsis" />
        <Button Grid.Column="2" Classes="ghost" Content="SCAN" Command="{Binding ScanCommand}"
                IsEnabled="{Binding !IsBusy}"
                ToolTip.Tip="Reads the file and shows what it found. Nothing is stored." />
      </Grid>
    </Border>

    <TextBlock Grid.Row="1" Classes="hint" Text="{Binding Status}" FontSize="12" Margin="14,12,14,0"
               TextWrapping="Wrap" />

    <!--
      What could not be honoured, above the list rather than beside it. Every one of these is a way the
      import is quieter than the file — an ignored Match block, a dropped ProxyCommand — and a person
      comparing the two needs to be told before they conclude the parser lost something.
    -->
    <Border Grid.Row="2" Margin="14,12,14,0" Padding="10,8" CornerRadius="4"
            Background="{StaticResource WarnWash}" BorderBrush="{StaticResource WarnSoft}"
            BorderThickness="1" IsVisible="{Binding HasWarnings}">
      <ItemsControl ItemsSource="{Binding Warnings}">
        <ItemsControl.ItemTemplate>
          <DataTemplate x:DataType="x:String">
            <TextBlock Text="{Binding}" Foreground="{StaticResource WarnText}" FontSize="11"
                       TextWrapping="Wrap" Margin="0,2" />
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
    </Border>

    <Grid Grid.Row="3" RowDefinitions="Auto,*" Margin="0,12,0,0" IsVisible="{Binding HasRows}">

      <Grid Grid.Row="0" ColumnDefinitions="34,1.1*,1.4*,1.6*,96" Margin="14,0,14,6">
        <TextBlock Grid.Column="1" Classes="label" Text="NAME" FontSize="9.5" LetterSpacing="1" />
        <TextBlock Grid.Column="2" Classes="label" Text="ADDRESS" FontSize="9.5" LetterSpacing="1" />
        <TextBlock Grid.Column="3" Classes="label" Text="AUTHENTICATION" FontSize="9.5" LetterSpacing="1" />
        <TextBlock Grid.Column="4" Classes="label" Text="STATE" FontSize="9.5" LetterSpacing="1" />
      </Grid>

      <ScrollViewer Grid.Row="1">
        <ItemsControl ItemsSource="{Binding Rows}">
          <ItemsControl.ItemTemplate>
            <DataTemplate x:DataType="vm:ImportRowViewModel">
              <StackPanel Margin="14,0">
                <Grid ColumnDefinitions="34,1.1*,1.4*,1.6*,96" Margin="0,7">
                  <CheckBox Grid.Column="0" IsChecked="{Binding IsSelected}" VerticalAlignment="Center" />
                  <TextBlock Grid.Column="1" Classes="mono" Text="{Binding Alias}" FontSize="12"
                             FontWeight="Medium" Foreground="{StaticResource Text}" Margin="0,0,8,0"
                             TextTrimming="CharacterEllipsis" VerticalAlignment="Center" />
                  <TextBlock Grid.Column="2" Classes="mono" Text="{Binding Address}" FontSize="10.5"
                             Foreground="{StaticResource TextDim}" Margin="0,0,8,0"
                             TextTrimming="CharacterEllipsis" VerticalAlignment="Center" />
                  <TextBlock Grid.Column="3" Classes="mono" Text="{Binding Authentication}" FontSize="10.5"
                             Foreground="{StaticResource TextFaint}" Margin="0,0,8,0"
                             TextTrimming="CharacterEllipsis" VerticalAlignment="Center" />
                  <Border Grid.Column="4" Classes="chip" HorizontalAlignment="Left"
                          VerticalAlignment="Center" IsVisible="{Binding HasBadge}">
                    <TextBlock Text="{Binding Badge}" FontSize="9.5" />
                  </Border>
                </Grid>
                <TextBlock Classes="hint" Text="{Binding Warnings}" FontSize="10.5" Margin="34,0,0,8"
                           TextWrapping="Wrap" Foreground="{StaticResource WarnText}"
                           IsVisible="{Binding HasWarnings}" />
              </StackPanel>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
      </ScrollViewer>
    </Grid>

    <Border Grid.Row="4" Padding="14,10" Background="{StaticResource Panel}"
            BorderBrush="{StaticResource Border}" BorderThickness="0,1,0,0"
            IsVisible="{Binding HasRows}">
      <StackPanel Spacing="8">
        <!--
          Said before the button, not after. Whether the key material comes with the host is the difference
          between a bookmark that connects and one that asks for a password, and somebody who is not told
          will conclude the import was broken.
        -->
        <TextBlock Classes="hint" FontSize="11" TextWrapping="Wrap"
                   IsVisible="{Binding !ImportsKeys}"
                   Text="Key files are not read. Where ssh_config names an IdentityFile the path is recorded as a note, and the host asks for a password until you bind it to a key in your keychain." />

        <!--
          ◆ THE TICK. Hidden entirely where the scan found no IdentityFile anywhere — an offer to read ~/.ssh
          on a screen where it would read nothing is a control that teaches people to ignore it.

          The warning sentence appears only when it is on, and it is the one place this application says out
          loud that it is about to open private keys. It names the directory rather than saying "your keys",
          because what somebody is agreeing to is a read of that directory.
        -->
        <StackPanel Spacing="6" IsVisible="{Binding HasKeyFiles}">
          <CheckBox IsChecked="{Binding ImportsKeys}">
            <TextBlock Classes="mono" FontSize="11.5" TextWrapping="Wrap"
                       Text="Also import the private keys these hosts point at" />
          </CheckBox>
          <TextBlock Classes="hint" FontSize="11" TextWrapping="Wrap"
                     IsVisible="{Binding ImportsKeys}" Foreground="{StaticResource WarnText}"
                     Text="Pressing IMPORT will read each host's first IdentityFile out of ~/.ssh, store it in this vault encrypted, and bind the host to it. One key is stored per file however many hosts name it, and a file already in your keychain is bound to rather than stored twice. A key protected by a passphrase comes in without one — nothing on disk says what it is — and the report below will name it." />
        </StackPanel>

        <StackPanel Orientation="Horizontal" Spacing="8">
          <Button Classes="accent" Content="{Binding ImportLabel}" Command="{Binding ImportCommand}"
                  IsEnabled="{Binding !IsBusy}" />
          <Button Classes="ghost" Content="TICK ALL / NONE" Command="{Binding ToggleAllCommand}" />
        </StackPanel>

        <!--
          ◆ What became of each key file, after the fact. Below the button because it is the answer rather
          than the offer, and capped with a scroll viewer because a config with thirty keyed hosts would
          otherwise push IMPORT off the window — the one control this screen must never lose.
        -->
        <Border IsVisible="{Binding HasKeyReport}" Padding="10,8" CornerRadius="4"
                Background="{StaticResource Panel}" BorderBrush="{StaticResource Border}"
                BorderThickness="1">
          <ScrollViewer MaxHeight="120">
            <ItemsControl ItemsSource="{Binding KeyReport}">
              <ItemsControl.ItemTemplate>
                <DataTemplate x:DataType="x:String">
                  <TextBlock Text="{Binding}" Classes="hint" FontSize="10.5" TextWrapping="Wrap"
                             Margin="0,2" />
                </DataTemplate>
              </ItemsControl.ItemTemplate>
            </ItemsControl>
          </ScrollViewer>
        </Border>
      </StackPanel>
    </Border>

  </Grid>

</UserControl>
