<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="using:ClawdDotNet.Desktop.ViewModels"
             xmlns:models="using:ClawdDotNet.App.Models"
             xmlns:ctrl="using:ClawdDotNet.Desktop.Controls"
             xmlns:conv="using:ClawdDotNet.Desktop.Converters"
             mc:Ignorable="d" d:DesignWidth="1200" d:DesignHeight="740"
             x:Class="ClawdDotNet.Desktop.Views.TasksPageView"
             x:DataType="vm:TasksPageViewModel">

  <DockPanel>

    <!-- Segmentleiste statt Registerkarten. Die Knöpfe überlappen sich um einen
         Bildpunkt, damit zwischen ihnen nur eine Linie steht und nicht zwei. -->
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal"
                HorizontalAlignment="Left" Margin="0,0,0,14">
      <ToggleButton Content="Geplante Jobs" Margin="0,0,-1,0"
                    IsChecked="{Binding IsJobsTab, Mode=OneWay}"
                    Command="{Binding SelectTabCommand}" CommandParameter="Jobs" />
      <ToggleButton Content="Dienste" Margin="0,0,-1,0"
                    IsChecked="{Binding IsServicesTab, Mode=OneWay}"
                    Command="{Binding SelectTabCommand}" CommandParameter="Services" />
      <ToggleButton Content="Verlauf"
                    IsChecked="{Binding IsHistoryTab, Mode=OneWay}"
                    Command="{Binding SelectTabCommand}" CommandParameter="History" />
    </StackPanel>

    <!-- Statuszeile des Bereichs -->
    <TextBlock DockPanel.Dock="Bottom" Classes="caption" Margin="0,10,0,0"
               Text="{Binding StatusText}" TextWrapping="Wrap" />

    <Panel>

      <!-- ══════════════════ Geplante Jobs ══════════════════ -->
      <DockPanel IsVisible="{Binding IsJobsTab}">

        <WrapPanel DockPanel.Dock="Top" Classes="toolbar">
          <Button Classes="toolbar primary" Command="{Binding AddJobCommand}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconPlus}" Width="14" Height="14" />
              <TextBlock Text="Job hinzufügen" />
            </StackPanel>
          </Button>

          <Button Classes="toolbar" Command="{Binding EditJobCommand}"
                  IsEnabled="{Binding SelectedJob, Converter={x:Static ObjectConverters.IsNotNull}}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconPencil}" Width="14" Height="14" />
              <TextBlock Text="Bearbeiten" />
            </StackPanel>
          </Button>

          <Button Classes="toolbar" Command="{Binding RemoveJobCommand}"
                  IsEnabled="{Binding SelectedJob, Converter={x:Static ObjectConverters.IsNotNull}}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconTrash}" Width="14" Height="14" />
              <TextBlock Text="Entfernen" />
            </StackPanel>
          </Button>

          <Button Classes="toolbar" Command="{Binding ToggleJobEnabledCommand}"
                  IsEnabled="{Binding SelectedJob, Converter={x:Static ObjectConverters.IsNotNull}}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconPower}" Width="14" Height="14" />
              <TextBlock Text="Aktivieren/Deaktivieren" />
            </StackPanel>
          </Button>

          <Border Classes="sep" />

          <Button Classes="toolbar" Command="{Binding RunJobNowCommand}"
                  IsEnabled="{Binding SelectedJob, Converter={x:Static ObjectConverters.IsNotNull}}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconPlay}" Width="12" Height="12" IsFilled="True" />
              <TextBlock Text="Jetzt ausführen" />
            </StackPanel>
          </Button>

          <Button Classes="toolbar" Command="{Binding RefreshJobListCommand}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconRefresh}" Width="14" Height="14" />
              <TextBlock Text="Aktualisieren" />
            </StackPanel>
          </Button>
        </WrapPanel>

        <Border DockPanel.Dock="Top" Classes="thead">
          <Grid ColumnDefinitions="1.1*,1.3*,2.2*,1.1*,1.3*,1.3*,0.9*">
            <TextBlock Grid.Column="0" Classes="th" Text="TYP" />
            <TextBlock Grid.Column="1" Classes="th" Text="AGENT" />
            <TextBlock Grid.Column="2" Classes="th" Text="TOOL / NACHRICHT" />
            <TextBlock Grid.Column="3" Classes="th" Text="CRON" />
            <TextBlock Grid.Column="4" Classes="th" Text="NÄCHSTER LAUF" />
            <TextBlock Grid.Column="5" Classes="th" Text="LETZTER LAUF" />
            <TextBlock Grid.Column="6" Classes="th" Text="STATUS" />
          </Grid>
        </Border>

        <ListBox Classes="table" ItemsSource="{Binding JobEntries}"
                 SelectedItem="{Binding SelectedJob}">
          <ListBox.ItemTemplate>
            <DataTemplate DataType="vm:JobDisplayItemViewModel">
              <Border Classes="tr" Padding="7">
                <Grid ColumnDefinitions="1.1*,1.3*,2.2*,1.1*,1.3*,1.3*,0.9*">
                  <TextBlock Grid.Column="0" Classes="td" Text="{Binding JobType}" />
                  <TextBlock Grid.Column="1" Classes="td" Text="{Binding AgentName}" />
                  <TextBlock Grid.Column="2" Classes="td" Text="{Binding TaskMessage}" />
                  <TextBlock Grid.Column="3" Classes="td mono" Text="{Binding CronExpression}" />
                  <TextBlock Grid.Column="4" Classes="td" Text="{Binding NextRun}" />
                  <TextBlock Grid.Column="5" Classes="td" Text="{Binding LastRun}" />

                  <Border Grid.Column="6" Classes="tag"
                          Classes.ok="{Binding Status,
                              Converter={x:Static conv:StringEqualsConverter.Instance},
                              ConverterParameter=Aktiv}">
                    <TextBlock Text="{Binding Status}" />
                  </Border>
                </Grid>
              </Border>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
      </DockPanel>

      <!-- ══════════════════ Dienste ══════════════════ -->
      <DockPanel IsVisible="{Binding IsServicesTab}">

        <WrapPanel DockPanel.Dock="Top" Classes="toolbar">
          <Button Classes="toolbar primary" Command="{Binding AddServiceCommand}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconPlus}" Width="14" Height="14" />
              <TextBlock Text="Dienst hinzufügen" />
            </StackPanel>
          </Button>

          <Button Classes="toolbar" Command="{Binding RemoveServiceCommand}"
                  IsEnabled="{Binding SelectedService, Converter={x:Static ObjectConverters.IsNotNull}}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconTrash}" Width="14" Height="14" />
              <TextBlock Text="Entfernen" />
            </StackPanel>
          </Button>

          <Border Classes="sep" />

          <!-- Der Entwurf zeigt hier „Start"/„Stop". Umgelegt wird aber das Kennzeichen
               in der Konfiguration, nicht ein laufender Prozess — deshalb die ehrlichere
               Beschriftung. -->
          <Button Classes="toolbar" Command="{Binding ToggleServiceEnabledCommand}"
                  IsEnabled="{Binding SelectedService, Converter={x:Static ObjectConverters.IsNotNull}}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconPower}" Width="14" Height="14" />
              <TextBlock Text="Aktivieren/Deaktivieren" />
            </StackPanel>
          </Button>

          <Button Classes="toolbar" Command="{Binding RefreshServiceListCommand}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconRefresh}" Width="14" Height="14" />
              <TextBlock Text="Aktualisieren" />
            </StackPanel>
          </Button>
        </WrapPanel>

        <Border DockPanel.Dock="Top" Classes="thead">
          <Grid ColumnDefinitions="1.8*,1*,0.7*,1*,3*">
            <TextBlock Grid.Column="0" Classes="th" Text="DIENSTNAME" />
            <TextBlock Grid.Column="1" Classes="th" Text="TYP" />
            <TextBlock Grid.Column="2" Classes="th" Text="PORT" />
            <TextBlock Grid.Column="3" Classes="th" Text="STATUS" />
            <TextBlock Grid.Column="4" Classes="th" Text="BESCHREIBUNG" />
          </Grid>
        </Border>

        <ListBox Classes="table" ItemsSource="{Binding ServiceEntries}"
                 SelectedItem="{Binding SelectedService}">
          <ListBox.ItemTemplate>
            <DataTemplate DataType="vm:ServiceDisplayItemViewModel">
              <Border Classes="tr" Padding="7">
                <Grid ColumnDefinitions="1.8*,1*,0.7*,1*,3*">
                  <TextBlock Grid.Column="0" Classes="td" FontWeight="SemiBold" Text="{Binding Name}" />
                  <TextBlock Grid.Column="1" Classes="td" Text="{Binding Type}" />
                  <TextBlock Grid.Column="2" Classes="td" Text="{Binding Port}" />

                  <Border Grid.Column="3" Classes="tag"
                          Classes.ok="{Binding Status,
                              Converter={x:Static conv:StringEqualsConverter.Instance},
                              ConverterParameter=Bereit}">
                    <TextBlock Text="{Binding Status}" />
                  </Border>

                  <TextBlock Grid.Column="4" Classes="td dim" Text="{Binding Description}" />
                </Grid>
              </Border>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
      </DockPanel>

      <!-- ══════════════════ Verlauf ══════════════════ -->
      <DockPanel IsVisible="{Binding IsHistoryTab}">

        <StackPanel DockPanel.Dock="Top" Classes="toolbar">
          <Button Classes="toolbar" Command="{Binding RefreshJobHistoryCommand}">
            <StackPanel Orientation="Horizontal" Spacing="6">
              <ctrl:StrokeIcon Data="{StaticResource IconRefresh}" Width="14" Height="14" />
              <TextBlock Text="Verlauf aktualisieren" />
            </StackPanel>
          </Button>
        </StackPanel>

        <Border DockPanel.Dock="Top" Classes="thead">
          <Grid ColumnDefinitions="1.3*,1.6*,1.4*,2*,0.9*,2.4*">
            <TextBlock Grid.Column="0" Classes="th" Text="ZEITPUNKT" />
            <TextBlock Grid.Column="1" Classes="th" Text="JOB" />
            <TextBlock Grid.Column="2" Classes="th" Text="AGENT" />
            <TextBlock Grid.Column="3" Classes="th" Text="BESCHREIBUNG" />
            <TextBlock Grid.Column="4" Classes="th" Text="STATUS" />
            <TextBlock Grid.Column="5" Classes="th" Text="DETAILS" />
          </Grid>
        </Border>

        <ItemsControl ItemsSource="{Binding JobHistoryEntries}">
          <ItemsControl.ItemTemplate>
            <DataTemplate DataType="models:JobHistoryEntry">
              <Border Classes="tr" Padding="7">
                <Grid ColumnDefinitions="1.3*,1.6*,1.4*,2*,0.9*,2.4*">
                  <TextBlock Grid.Column="0" Classes="td"
                             Text="{Binding Time, StringFormat='{}{0:dd.MM. HH:mm}'}" />
                  <TextBlock Grid.Column="1" Classes="td" Text="{Binding JobName}" />
                  <TextBlock Grid.Column="2" Classes="td" Text="{Binding Agent}" />
                  <TextBlock Grid.Column="3" Classes="td dim" Text="{Binding JobDescription}" />

                  <Border Grid.Column="4" Classes="tag"
                          Classes.ok="{Binding Status,
                              Converter={x:Static conv:StringEqualsConverter.Instance},
                              ConverterParameter=Success}"
                          Classes.bad="{Binding Status,
                              Converter={x:Static conv:StringEqualsConverter.Instance},
                              ConverterParameter=Error}">
                    <TextBlock Text="{Binding Status}" />
                  </Border>

                  <TextBlock Grid.Column="5" Classes="td dim" Text="{Binding Info}" />
                </Grid>
              </Border>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
      </DockPanel>

    </Panel>
  </DockPanel>

</UserControl>
