<Window 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:ctrl="using:ClawdDotNet.Desktop.Controls"
        xmlns:conv="using:ClawdDotNet.Desktop.Converters"
        mc:Ignorable="d" d:DesignWidth="1440" d:DesignHeight="860"
        x:Class="ClawdDotNet.Desktop.Views.MainWindow"
        x:DataType="vm:MainWindowViewModel"
        Icon="/Assets/app.ico"
        Title="ClawdDotNet"
        Width="1440" Height="860"
        MinWidth="960" MinHeight="600">

  <Design.DataContext>
    <vm:MainWindowViewModel />
  </Design.DataContext>

  <Grid ColumnDefinitions="Auto,*">

    <!-- ══════════════════════════ Seitenleiste ══════════════════════════ -->
    <Border Grid.Column="0" Classes="sidebar" Width="{Binding SidebarWidth}">
      <DockPanel>

        <!-- Marke und Einklapp-Schalter -->
        <Border DockPanel.Dock="Top" BorderBrush="{DynamicResource DividerBrush}"
                BorderThickness="0,0,0,1" Padding="14">
          <DockPanel>
            <ctrl:StrokeIcon Data="{StaticResource IconLogo}" Width="20" Height="20"
                             Foreground="{DynamicResource AccentBrush}"
                             DockPanel.Dock="Left" VerticalAlignment="Center" />

            <Button DockPanel.Dock="Right" Classes="flat"
                    Command="{Binding ToggleSidebarCommand}"
                    ToolTip.Tip="Seitenleiste ein-/ausklappen"
                    Padding="4" VerticalAlignment="Center">
              <ctrl:StrokeIcon Width="14" Height="14"
                               Data="{Binding IsSidebarCollapsed,
                                      Converter={x:Static conv:SidebarChevronConverter.Instance}}" />
            </Button>

            <TextBlock Classes="h2" Text="ClawdDotNet" Margin="8,0,0,0"
                       VerticalAlignment="Center"
                       IsVisible="{Binding IsSidebarExpanded}" />
          </DockPanel>
        </Border>

        <!-- Fußzeile: Version -->
        <TextBlock DockPanel.Dock="Bottom" Classes="caption" Margin="16,10"
                   Text="{Binding VersionText}"
                   IsVisible="{Binding IsSidebarExpanded}" />

        <!-- Instanz-Schalter: der einzige Knopf mit Akzentrahmen, weil er als
             einziger die ganze Anwendung umschaltet. -->
        <Button DockPanel.Dock="Top" Classes="instance" Margin="12,12,12,0"
                Command="{Binding OpenInstanceManagerCommand}"
                ToolTip.Tip="Instanz wechseln">
          <DockPanel>
            <ctrl:StrokeIcon Data="{StaticResource IconLogo}" Width="15" Height="15"
                             DockPanel.Dock="Left" VerticalAlignment="Center" />

            <ctrl:StrokeIcon Data="{StaticResource IconChevronDown}" Width="14" Height="14"
                             DockPanel.Dock="Right" VerticalAlignment="Center"
                             IsVisible="{Binding IsSidebarExpanded}" />

            <StackPanel Margin="8,0,0,0" IsVisible="{Binding IsSidebarExpanded}">
              <TextBlock Text="INSTANZ" FontSize="9" LetterSpacing="1"
                         Foreground="{DynamicResource AccentBrush}" />
              <TextBlock Classes="h2" FontSize="13" Text="{Binding InstanceName}"
                         TextTrimming="CharacterEllipsis" />
            </StackPanel>
          </DockPanel>
        </Button>

        <!-- Navigation -->
        <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="10,14">
          <ItemsControl ItemsSource="{Binding NavGroups}">
            <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                <StackPanel Spacing="18" />
              </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="vm:NavGroupViewModel">
                <StackPanel Spacing="2">

                  <TextBlock Classes="navgroup" Text="{Binding Title}"
                             IsVisible="{Binding IsTitleVisible}" />

                  <ItemsControl ItemsSource="{Binding Items}">
                    <ItemsControl.ItemsPanel>
                      <ItemsPanelTemplate>
                        <StackPanel Spacing="2" />
                      </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                    <ItemsControl.ItemTemplate>
                      <DataTemplate DataType="vm:NavItemViewModel">
                        <Button Classes="nav"
                                Classes.selected="{Binding IsSelected}"
                                Classes.collapsed="{Binding IsCollapsed}"
                                Command="{Binding SelectCommand}"
                                ToolTip.Tip="{Binding Title}">
                          <StackPanel Orientation="Horizontal" Spacing="10">
                            <ctrl:StrokeIcon Width="17" Height="17"
                                             VerticalAlignment="Center"
                                             Data="{Binding IconKey,
                                                    Converter={x:Static conv:IconConverter.Instance}}" />
                            <TextBlock Text="{Binding Title}" VerticalAlignment="Center"
                                       IsVisible="{Binding !IsCollapsed}" />
                          </StackPanel>
                        </Button>
                      </DataTemplate>
                    </ItemsControl.ItemTemplate>
                  </ItemsControl>

                </StackPanel>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </ScrollViewer>

      </DockPanel>
    </Border>

    <!-- ══════════════════════════ Hauptbereich ══════════════════════════ -->
    <DockPanel Grid.Column="1">

      <!-- Kopfzeile -->
      <Border DockPanel.Dock="Top" Classes="topbar">
        <DockPanel>
          <TextBlock Classes="h1" Text="{Binding PageTitle}" VerticalAlignment="Center" />

          <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" Spacing="10"
                      HorizontalAlignment="Right" VerticalAlignment="Center">
            <TextBlock Classes="caption" Text="{Binding ThemeLabel}" VerticalAlignment="Center" />
            <Button Classes="icon" Command="{Binding ToggleThemeCommand}"
                    ToolTip.Tip="Dunkelmodus umschalten">
              <ctrl:StrokeIcon Width="16" Height="16"
                               Data="{Binding ThemeIconKey,
                                      Converter={x:Static conv:IconConverter.Instance}}" />
            </Button>
          </StackPanel>
        </DockPanel>
      </Border>

      <!-- Benachrichtigungsleiste: nimmt keinen Platz, solange nichts anliegt -->
      <ItemsControl DockPanel.Dock="Top" ItemsSource="{Binding Notifications}"
                    IsVisible="{Binding HasNotifications}">
        <ItemsControl.ItemTemplate>
          <DataTemplate DataType="vm:NotificationViewModel">
            <Panel>
              <!-- Zwei Grundflächen statt eines umgerechneten Pinsels: So bleiben die
                   Farben an DynamicResource gebunden und wechseln mit dem Thema mit. -->
              <Border Background="{DynamicResource DangerBarBrush}"
                      BorderBrush="{DynamicResource DangerBrush}" BorderThickness="3,0,0,0"
                      IsVisible="{Binding IsError}" />
              <Border Background="{DynamicResource WarningSoftBrush}"
                      BorderBrush="{DynamicResource WarningBrush}" BorderThickness="3,0,0,0"
                      IsVisible="{Binding IsWarning}" />

              <DockPanel Margin="24,8">
                <ctrl:StrokeIcon Data="{StaticResource IconAlert}" Width="15" Height="15"
                                 DockPanel.Dock="Left" VerticalAlignment="Center"
                                 Margin="0,0,10,0" />

                <Button DockPanel.Dock="Right" Classes="flat" Padding="2"
                        Command="{Binding DismissCommand}" ToolTip.Tip="Ausblenden"
                        VerticalAlignment="Center">
                  <ctrl:StrokeIcon Data="{StaticResource IconClose}" Width="13" Height="13" />
                </Button>

                <TextBlock FontSize="12.5" VerticalAlignment="Center" TextWrapping="Wrap">
                  <Run Text="{Binding InstanceName}" FontWeight="SemiBold" /><Run Text=": " /><Run Text="{Binding Text}" />
                </TextBlock>
              </DockPanel>
            </Panel>
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>

      <!-- Statusleiste -->
      <Border DockPanel.Dock="Bottom" Classes="statusbar">
        <DockPanel>
          <StackPanel Orientation="Horizontal" Spacing="8">
            <TextBlock Classes="caption" Text="{Binding InstanceName, StringFormat='Instanz: {0}'}" />
            <TextBlock Classes="caption" Text="·" />

            <StackPanel Orientation="Horizontal" Spacing="5" VerticalAlignment="Center">
              <Ellipse Width="7" Height="7" VerticalAlignment="Center"
                       Fill="{DynamicResource SuccessBrush}"
                       IsVisible="{Binding IsApiReachable}" />
              <Ellipse Width="7" Height="7" VerticalAlignment="Center"
                       Fill="{DynamicResource DangerBrush}"
                       IsVisible="{Binding !IsApiReachable}" />
              <TextBlock Classes="caption" Text="{Binding ApiStatusText, StringFormat='API {0}'}" />
            </StackPanel>

            <TextBlock Classes="caption" Text="·"
                       IsVisible="{Binding UsageText, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" />
            <TextBlock Classes="caption" Text="{Binding UsageText}"
                       ToolTip.Tip="{Binding UsageTooltip}"
                       IsVisible="{Binding UsageText, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" />

            <!-- Der Trenner gehoert zum Abschnitt dahinter: Ohne Guthabenangabe stuende
                 sonst ein Punkt am Ende der Zeile, der nichts mehr trennt. -->
            <TextBlock Classes="caption" Text="·"
                       IsVisible="{Binding CreditsText, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" />
            <TextBlock Classes="caption" Text="{Binding CreditsText}"
                       IsVisible="{Binding CreditsText, Converter={x:Static StringConverters.IsNotNullOrEmpty}}" />
          </StackPanel>

          <TextBlock DockPanel.Dock="Right" Classes="caption" HorizontalAlignment="Right"
                     Text="{Binding ThemeLabel}" />
        </DockPanel>
      </Border>

      <!-- Inhalt -->
      <ContentControl Content="{Binding SelectedPage}" Margin="20" />

    </DockPanel>
  </Grid>

</Window>
