<UserControl x:Class="GsConfigTool.Views.ProjectsPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:GsConfigTool.ViewModels"
             xmlns:helpers="clr-namespace:GsConfigTool.Helpers"
             Background="{StaticResource BgBrush}">

    <Grid Margin="32,28">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Title -->
        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,8">
            <TextBlock Text="📁 " FontSize="22"/>
            <TextBlock Text="Meine Projekte" Style="{StaticResource PageTitle}"/>
        </StackPanel>
        <TextBlock Grid.Row="1" Text="Alle gespeicherten Server-Konfigurationen."
                   Foreground="{StaticResource TextMutedBrush}" Margin="0,0,0,20"/>

        <!-- Toolbar -->
        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="0,0,0,16">
            <Button Style="{StaticResource PrimaryButton}" Content="＋ ASE-Projekt"
                    Command="{Binding NewArkSECommand}" Margin="0,0,8,0"
                    Background="{StaticResource ArkBrush}"/>
            <Button Style="{StaticResource PrimaryButton}" Content="＋ ASA-Projekt"
                    Command="{Binding NewArkSACommand}" Margin="0,0,8,0"
                    Background="{StaticResource ArkSABrush}"/>
            <Button Style="{StaticResource PrimaryButton}" Content="＋ Palworld-Projekt"
                    Command="{Binding NewPalworldCommand}" Margin="0,0,8,0"
                    Background="{StaticResource PalworldBrush}" Foreground="White"/>
            <Button Style="{StaticResource SecondaryButton}" Content="🔄 Aktualisieren"
                    Command="{Binding RefreshCommand}" Margin="0,0,8,0"/>
            <Button Style="{StaticResource DangerButton}" Content="🗑 Löschen"
                    Command="{Binding DeleteCommand}"/>
        </StackPanel>

        <!-- Project List -->
        <Border Grid.Row="3" Style="{StaticResource CardBorder}" Padding="0">
            <Grid>
                <!-- Empty State (shown when list is empty via code-behind) -->
                <StackPanel x:Name="EmptyState" HorizontalAlignment="Center" VerticalAlignment="Center"
                            Visibility="Collapsed">
                    <TextBlock Text="📂" FontSize="48" HorizontalAlignment="Center" Opacity="0.3"/>
                    <TextBlock Text="Noch keine Projekte" FontSize="16" HorizontalAlignment="Center"
                               Foreground="{StaticResource TextMutedBrush}" Margin="0,12,0,4"/>
                    <TextBlock Text="Erstelle ein neues Projekt über die Buttons oben."
                               Foreground="{StaticResource TextMutedBrush}" FontSize="12"
                               HorizontalAlignment="Center"/>
                </StackPanel>

                <ListBox ItemsSource="{Binding Projects}"
                         SelectedItem="{Binding SelectedProject}"
                         BorderThickness="0" Background="Transparent"
                         ScrollViewer.VerticalScrollBarVisibility="Auto">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="4,2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>

                                <!-- Game Badge -->
                                <Border Grid.Column="0" Width="46" Height="46" CornerRadius="8"
                                        Margin="0,0,14,0" VerticalAlignment="Center">
                                    <Border.Style>
                                        <Style TargetType="Border">
                                            <Setter Property="Background" Value="#1A2A1A"/>
                                        </Style>
                                    </Border.Style>
                                    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                                               FontSize="20">
                                        <TextBlock.Style>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="Text" Value="🎮"/>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>
                                </Border>

                                <!-- Info -->
                                <StackPanel Grid.Column="1" VerticalAlignment="Center">
                                    <TextBlock Text="{Binding Name}" FontWeight="SemiBold" FontSize="14"/>
                                    <StackPanel Orientation="Horizontal" Margin="0,3,0,0">
                                        <Border CornerRadius="3" Padding="6,2" Margin="0,0,8,0">
                                            <Border.Style>
                                                <Style TargetType="Border">
                                                    <Setter Property="Background" Value="#1F3A5F"/>
                                                </Style>
                                            </Border.Style>
                                            <TextBlock Text="{Binding GameShortName}" FontSize="10"
                                                       FontWeight="SemiBold"
                                                       Foreground="{StaticResource AccentBrush}"/>
                                        </Border>
                                        <TextBlock Text="{Binding GameDisplayName}" FontSize="12"
                                                   Foreground="{StaticResource TextMutedBrush}"
                                                   VerticalAlignment="Center"/>
                                    </StackPanel>
                                    <TextBlock Text="{Binding Description}" FontSize="11"
                                               Foreground="{StaticResource TextMutedBrush}"
                                               Margin="0,2,0,0" MaxWidth="500" TextTrimming="CharacterEllipsis"/>
                                </StackPanel>

                                <!-- Dates -->
                                <StackPanel Grid.Column="2" VerticalAlignment="Center" Margin="16,0">
                                    <TextBlock Text="Geändert" FontSize="10"
                                               Foreground="{StaticResource TextMutedBrush}"/>
                                    <TextBlock Text="{Binding UpdatedAt, StringFormat='{}{0:dd.MM.yyyy HH:mm}'}"
                                               FontSize="11" Foreground="{StaticResource TextMutedBrush}"/>
                                </StackPanel>

                                <!-- Open Button -->
                                <Button Grid.Column="3" Style="{StaticResource PrimaryButton}"
                                        Content="Öffnen" Padding="12,6"
                                        Tag="{Binding}"
                                        Click="OpenProject_Click"/>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </Border>
    </Grid>
</UserControl>
