<v:ChromelessWindow 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:s="using:SourceGit"
                    xmlns:vm="using:SourceGit.ViewModels"
                    xmlns:v="using:SourceGit.Views"
                    mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
                    x:Class="SourceGit.Views.Launcher"
                    x:DataType="vm:Launcher"
                    x:Name="ThisControl"
                    Icon="/App.ico"
                    Title="{Binding Title}"
                    MinWidth="1024" MinHeight="600">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="{Binding #ThisControl.CaptionHeight}"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!-- Custom TitleBar -->
    <Grid Grid.Row="0" ColumnDefinitions="Auto,32,32,*,Auto">
      <!-- Bottom border -->
      <Border Grid.Column="0" Grid.ColumnSpan="5"
              Background="{DynamicResource Brush.TitleBar}"
              BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
              DoubleTapped="MaximizeOrRestoreWindow"
              PointerPressed="BeginMoveWindow"/>

      <!-- Caption Buttons (macOS) -->
      <Border Grid.Column="0" Width="72" IsVisible="{Binding #ThisControl.HasLeftCaptionButton}"/>

      <!-- Menu (Windows/Linux) -->
      <Button Grid.Column="0" Classes="icon_button" VerticalAlignment="Bottom" Margin="6,0,0,3" IsVisible="{OnPlatform True, macOS=False}">
        <Button.Flyout>
          <MenuFlyout Placement="BottomEdgeAlignedLeft" VerticalOffset="-6">
            <MenuItem Header="{DynamicResource Text.Preferences}" Command="{x:Static s:App.OpenPreferencesCommand}" InputGesture="Ctrl+,">
              <MenuItem.Icon>
                <Path Width="14" Height="14" Data="{StaticResource Icons.Settings}"/>
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="{DynamicResource Text.OpenAppDataDir}" Command="{x:Static s:App.OpenAppDataDirCommand}">
              <MenuItem.Icon>
                <Path Width="14" Height="14" Data="{StaticResource Icons.Explore}"/>
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="{DynamicResource Text.Hotkeys}" Command="{x:Static s:App.OpenHotkeysCommand}" InputGesture="F1">
              <MenuItem.Icon>
                <Path Width="14" Height="14" Data="{StaticResource Icons.Hotkeys}"/>
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="-"/>
            <MenuItem Header="{DynamicResource Text.SelfUpdate}" Command="{x:Static s:App.CheckForUpdateCommand}" IsVisible="{x:Static s:App.IsCheckForUpdateCommandVisible}">
              <MenuItem.Icon>
                <Path Width="14" Height="14" Data="{StaticResource Icons.SoftwareUpdate}"/>
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="-" IsVisible="{x:Static s:App.IsCheckForUpdateCommandVisible}"/>
            <MenuItem Header="{DynamicResource Text.About}" Command="{x:Static s:App.OpenAboutCommand}">
              <MenuItem.Icon>
                <Path Width="14" Height="14" Data="{StaticResource Icons.Info}"/>
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="{DynamicResource Text.Quit}" Command="{x:Static s:App.QuitCommand}" InputGesture="Ctrl+Q">
              <MenuItem.Icon>
                <Path Width="14" Height="14" Data="{StaticResource Icons.Quit}"/>
              </MenuItem.Icon>
            </MenuItem>
          </MenuFlyout>
        </Button.Flyout>
        <Path Width="12" Height="12" Data="{StaticResource Icons.Menu}"/>
      </Button>

      <!-- Workspace Dropdown Menu -->
      <Button Grid.Column="1" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenWorkspaceMenu" HotKey="{OnPlatform Ctrl+Alt+P, macOS=⌘+Alt+P}">
        <ToolTip.Tip>
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="{DynamicResource Text.Workspace}" FontWeight="Bold" Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"/>
            <TextBlock Text="{Binding ActiveWorkspace.Name}"/>
            <TextBlock Margin="16,0,0,0"
                       Text="{OnPlatform Ctrl+Alt+P, macOS=⌘+Alt+P}"
                       FontSize="11"
                       Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"
                       VerticalAlignment="Center"/>
          </StackPanel>
        </ToolTip.Tip>
        <Path Width="14" Height="14"
              Data="{StaticResource Icons.Workspace}"
              Fill="{Binding ActiveWorkspace.Brush}"/>
      </Button>

      <!-- Pages Switcher Toggle Button -->
      <Button Grid.Column="2" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenPagesCommandPalette" HotKey="{OnPlatform Ctrl+P, macOS=⌘+P}">
        <ToolTip.Tip>
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="{DynamicResource Text.Launcher.Pages}"
                       VerticalAlignment="Center"/>
            <TextBlock Margin="8,0,0,0"
                       Text="{OnPlatform Ctrl+P, macOS=⌘+P}"
                       FontSize="11"
                       Foreground="{DynamicResource MenuFlyoutItemKeyboardAcceleratorTextForeground}"
                       VerticalAlignment="Center"/>
          </StackPanel>
        </ToolTip.Tip>
        <Path Width="13" Height="13" Margin="0,2,0,0" Data="{StaticResource Icons.Tabs}"/>
      </Button>

      <!-- Pages Tabs -->
      <v:LauncherTabBar Grid.Column="3" Height="30" Margin="0,0,16,0" VerticalAlignment="Bottom"/>

      <!-- Caption Buttons (Windows/Linux) -->
      <Border Grid.Column="4" Margin="16,0,0,0" IsVisible="{Binding #ThisControl.HasRightCaptionButton}">
        <v:CaptionButtons Height="30" VerticalAlignment="Top"/>
      </Border>
    </Grid>

    <!-- Page -->
    <ContentControl Grid.Row="1" Content="{Binding ActivePage}">
      <ContentControl.DataTemplates>
        <DataTemplate DataType="vm:LauncherPage">
          <v:LauncherPage/>
        </DataTemplate>
      </ContentControl.DataTemplates>
    </ContentControl>

    <!-- Command Palette -->
    <Border Grid.Row="0" Grid.RowSpan="2"
            Background="Transparent"
            IsVisible="{Binding CommandPalette, Converter={x:Static ObjectConverters.IsNotNull}}"
            PointerPressed="OnCloseCommandPalette">
      <Border Width="400" HorizontalAlignment="Center" VerticalAlignment="Center" Effect="drop-shadow(0 0 12 #A0000000)">
        <Border Background="{DynamicResource Brush.Popup}" CornerRadius="8">
          <ContentControl Margin="16,10,16,12" Content="{Binding CommandPalette}">
            <ContentControl.DataTemplates>
              <v:CommandPaletteDataTemplates/>
            </ContentControl.DataTemplates>
          </ContentControl>
        </Border>
      </Border>
    </Border>
  </Grid>
</v:ChromelessWindow>
