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

  <!--
    v5c-2: Tags, against Settings-Tags.dc.html — a real management page over VaultViewModel's own tag
    commands (Tags, NewTag, EditTagRow, DeleteTagRow), which used to be reachable only from the keychain
    screen's TAGS category. Both doors stay open — the keychain screen's own panel is untouched — because
    they lead to the same commands and the same rows; nothing here is a second implementation.

    ── THE INTRO SENTENCE IS TRUE, AND IT IS THE DESIGN'S OWN ─────────────────────────────────────────────
    "Renaming here is one write and every host follows" — verified against SaveTagAsync and TagRowViewModel's
    own remark: a host names a tag's id in its TagSet, never its label, so renaming touches nothing but the
    tag item itself. Kept rather than rewritten.

    ── EDIT AND DELETE ARE WRAPPER COMMANDS, UNLIKE THE GROUPS PAGE'S ──────────────────────────────────────
    EditTagCommand and DeleteTagCommand read VaultViewModel.SelectedTag rather than taking a row argument —
    they were built for the keychain screen's own ListBox selection, which this page has no equivalent of.
    VaultViewModel.EditTagRow/DeleteTagRow (v5c-2, additive) select the row and then call the real command, so
    every guard and every sentence either one already has is still the one that runs.

    ── WHAT THE DESIGN DREW AND THIS PAGE DOES NOT ─────────────────────────────────────────────────────────
    The LAST APPLIED column: no timestamp of when a tag was last put on a host exists anywhere in this
    client, and a column of "just now" / "2 min ago" would be inventing one.
  -->

  <ScrollViewer>
    <StackPanel MaxWidth="1100" Margin="40" HorizontalAlignment="Stretch">

      <Grid ColumnDefinitions="*,Auto">
        <StackPanel Grid.Column="0" Orientation="Horizontal" Spacing="14" VerticalAlignment="Center">
          <TextBlock Classes="settingstitle" Text="Tags" />
          <Border Background="{StaticResource Chip}" CornerRadius="9" MinWidth="34" Height="30"
                  IsVisible="{Binding Vault, Converter={x:Static ObjectConverters.IsNotNull}}">
            <TextBlock Text="{Binding Vault.Tags.Count}" FontSize="13.5" FontWeight="SemiBold"
                       Foreground="{StaticResource TextFaint}" Margin="8,0"
                       HorizontalAlignment="Center" VerticalAlignment="Center" />
          </Border>
        </StackPanel>
        <Button Grid.Column="1" Classes="accent" Height="40" Content="+ NEW TAG"
                Command="{Binding Vault.NewTagCommand}"
                IsEnabled="{Binding Vault, Converter={x:Static ObjectConverters.IsNotNull}}" />
      </Grid>

      <TextBlock Classes="settingsrowcaption" Margin="0,12,0,0" TextWrapping="Wrap"
                 Text="A tag is a name shared by every host that carries it. Renaming here is one write — every host, filter and snippet rule follows." />

      <!-- The tag editor, in place — the same one-box form the keychain screen's own panel draws, restyled
           into this page's card idiom. -->
      <Border Classes="settingscard" Margin="0,20,0,0" Padding="20"
              IsVisible="{Binding Vault.IsEditingTag}">
        <StackPanel Spacing="8">
          <TextBlock Classes="settingsrowtitle" Text="Tag" />
          <TextBox PlaceholderText="name" Text="{Binding Vault.TagEditorLabel}">
            <TextBox.KeyBindings>
              <KeyBinding Gesture="Enter" Command="{Binding Vault.SaveTagCommand}" />
            </TextBox.KeyBindings>
          </TextBox>
          <TextBlock Classes="settingsrowcaption"
                     Text="Renaming a tag changes it everywhere at once. No host is rewritten — each one names this tag rather than repeating its name." />
          <StackPanel Orientation="Horizontal" Spacing="8">
            <Button Classes="accent" Height="36" Content="SAVE" Command="{Binding Vault.SaveTagCommand}" />
            <Button Classes="ghost" Height="36" Content="CANCEL" Command="{Binding Vault.CancelTagEditCommand}" />
          </StackPanel>
        </StackPanel>
      </Border>

      <!-- The delete confirmation, in place — the same shared control the groups and vaults pages reuse. -->
      <Border Margin="0,20,0,0" Padding="20" CornerRadius="12"
              Background="{StaticResource DangerWash}" BorderBrush="{StaticResource DangerSoft}"
              BorderThickness="1"
              IsVisible="{Binding Vault.PendingDeletion, Converter={x:Static ObjectConverters.IsNotNull}}">
        <views:ConfirmDeleteCard DataContext="{Binding Vault}" />
      </Border>

      <Border Classes="settingscard" Margin="0,24,0,40" Padding="0"
              IsVisible="{Binding Vault, Converter={x:Static ObjectConverters.IsNotNull}}">
        <StackPanel>

          <Grid ColumnDefinitions="1.4*,*,*,Auto" Margin="20,14,20,10">
            <TextBlock Grid.Column="0" Classes="label" Text="TAG" />
            <TextBlock Grid.Column="1" Classes="label" Text="USED BY" />
            <TextBlock Grid.Column="2" Classes="label" Text="VAULT" />
            <TextBlock Grid.Column="3" Classes="label" Text=" " />
          </Grid>

          <ItemsControl ItemsSource="{Binding Vault.Tags}">
            <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                <StackPanel />
              </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
              <DataTemplate x:DataType="vm:TagRowViewModel">
                <Border Classes="settingsrow last" Padding="20,10">
                  <Grid ColumnDefinitions="1.4*,*,*,Auto">

                    <Border Grid.Column="0" Classes="chip" HorizontalAlignment="Left" VerticalAlignment="Center">
                      <TextBlock Text="{Binding Label}" />
                    </Border>

                    <TextBlock Grid.Column="1" Classes="mono" FontSize="11.5"
                               Foreground="{StaticResource TextFaint}" VerticalAlignment="Center"
                               Text="{Binding Description}" />

                    <!--
                      No vault chip here — unlike a group, TagRowViewModel does not carry which vault it lives
                      in (tags are read from the active vault alone; see VaultViewModel.NewTag's own remark),
                      so there is no second vault this column could honestly name.
                    -->
                    <TextBlock Grid.Column="2" />

                    <StackPanel Grid.Column="3" Orientation="Horizontal" Spacing="8" VerticalAlignment="Center">
                      <Button Classes="paneicon" Width="30" Height="30" FontSize="13"
                              Command="{Binding #Root.((vm:MainWindowViewModel)DataContext).Vault.EditTagRowCommand}"
                              CommandParameter="{Binding}" ToolTip.Tip="Rename this tag.">
                        <TextBlock FontFamily="{StaticResource IconFont}" Text="&#xE3C9;" />
                      </Button>
                      <Button Classes="paneicon danger" Width="30" Height="30" FontSize="13"
                              Command="{Binding #Root.((vm:MainWindowViewModel)DataContext).Vault.DeleteTagRowCommand}"
                              CommandParameter="{Binding}" ToolTip.Tip="Delete this tag. Hosts wearing it simply stop showing the chip.">
                        <TextBlock FontFamily="{StaticResource IconFont}" Text="&#xE872;" />
                      </Button>
                    </StackPanel>

                  </Grid>
                </Border>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>

          <TextBlock Classes="settingsrowcaption" Margin="20,14,20,18" TextWrapping="Wrap"
                     IsVisible="{Binding !Vault.HasTagItems}"
                     Text="No tags yet. Add one from here, or from a host's own editor." />

        </StackPanel>
      </Border>

      <TextBlock Classes="settingsrowcaption" Margin="0,20,0,40" TextWrapping="Wrap"
                 IsVisible="{Binding Vault, Converter={x:Static ObjectConverters.IsNull}}"
                 Text="Unlock your keychain to see and manage your tags." />

    </StackPanel>
  </ScrollViewer>

</UserControl>
