<Window x:Class="TeamsISO.App.PresetsDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
        Title="Operator presets"
        Icon="/Assets/teamsiso.ico"
        Width="460" Height="520"
        WindowStartupLocation="CenterOwner"
        WindowStyle="None"
        ResizeMode="NoResize"
        Background="{DynamicResource Wd.Canvas}"
        UseLayoutRounding="True"
        TextOptions.TextFormattingMode="Ideal"
        TextOptions.TextRenderingMode="ClearType">

    <shell:WindowChrome.WindowChrome>
        <shell:WindowChrome
            CaptionHeight="32"
            ResizeBorderThickness="0"
            CornerRadius="0"
            GlassFrameThickness="0"
            UseAeroCaptionButtons="False"/>
    </shell:WindowChrome.WindowChrome>

    <Border BorderBrush="{DynamicResource Wd.Border}" BorderThickness="1">
        <Grid Margin="24,16,24,20">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <!-- Caption -->
            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="OPERATOR PRESETS"
                           Style="{StaticResource Wd.Text.Caption}"
                           VerticalAlignment="Center"/>
                <Button Grid.Column="1"
                        Style="{StaticResource Wd.Button.CaptionClose}"
                        Click="OnCancel"
                        shell:WindowChrome.IsHitTestVisibleInChrome="True">
                    <Path Data="M 0,0 L 10,10 M 10,0 L 0,10"
                          Stroke="{DynamicResource Wd.Text.Primary}"
                          StrokeThickness="1.2"
                          Width="10" Height="10"
                          Stretch="None"/>
                </Button>
            </Grid>

            <TextBlock Grid.Row="1"
                       Text="Save the current ISO assignments as a named preset, or load an existing preset to restore them."
                       Style="{StaticResource Wd.Text.Subtle}"
                       Foreground="{DynamicResource Wd.Text.Tertiary}"
                       FontSize="12"
                       TextWrapping="Wrap"
                       Margin="0,12,0,16"/>

            <!-- Save row: name textbox + Save button -->
            <Grid Grid.Row="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBox x:Name="NameBox"
                         ToolTip="Name for the new preset (or pick an existing one to overwrite)"
                         VerticalContentAlignment="Center"
                         Margin="0,0,12,0"/>
                <Button Grid.Column="1"
                        Style="{StaticResource Wd.Button.Primary}"
                        Content="Save"
                        Click="OnSave"
                        Padding="20,8"/>
            </Grid>

            <!-- Existing presets list -->
            <Border Grid.Row="3"
                    Style="{StaticResource Wd.Card}"
                    Padding="0"
                    Margin="0,16,0,0">
                <Grid>
                    <ListBox x:Name="PresetsList"
                             Background="Transparent"
                             BorderThickness="0"
                             SelectionMode="Single"
                             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                             SelectionChanged="OnSelectionChanged">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="Padding" Value="12,8"/>
                                <Setter Property="Foreground" Value="{DynamicResource Wd.Text.Primary}"/>
                                <Setter Property="Background" Value="Transparent"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ListBoxItem">
                                            <Border x:Name="Bd"
                                                    Background="{TemplateBinding Background}"
                                                    Padding="{TemplateBinding Padding}"
                                                    CornerRadius="4"
                                                    Margin="4,2">
                                                <ContentPresenter/>
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource Wd.SurfaceHover}"/>
                                                </Trigger>
                                                <Trigger Property="IsSelected" Value="True">
                                                    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource Wd.SurfaceElevated}"/>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Name}"
                                               Style="{StaticResource Wd.Text.Body}"
                                               FontWeight="Medium"/>
                                    <TextBlock Style="{StaticResource Wd.Text.Mono}"
                                               FontSize="10"
                                               Foreground="{DynamicResource Wd.Text.Tertiary}">
                                        <Run Text="{Binding SavedAtDisplay, Mode=OneWay}"/>
                                        <Run Text=" · "/>
                                        <Run Text="{Binding AssignmentCount, Mode=OneWay}"/>
                                        <Run Text=" assignments"/>
                                    </TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                    <!-- Empty-state inside the card -->
                    <TextBlock x:Name="EmptyState"
                               Text="No presets yet. Type a name above and click Save."
                               Style="{StaticResource Wd.Text.Subtle}"
                               Foreground="{DynamicResource Wd.Text.Tertiary}"
                               FontSize="12"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Visibility="Collapsed"/>
                </Grid>
            </Border>

            <!-- Footer buttons -->
            <Grid Grid.Row="4" Margin="0,16,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0"
                        Style="{StaticResource Wd.Button.Ghost}"
                        Content="Delete"
                        Click="OnDelete"
                        IsEnabled="False"
                        x:Name="DeleteButton"
                        Padding="14,8"/>
                <Button Grid.Column="1"
                        Style="{StaticResource Wd.Button.Ghost}"
                        Content="Duplicate"
                        Click="OnDuplicate"
                        IsEnabled="False"
                        x:Name="DuplicateButton"
                        Margin="8,0,0,0"
                        Padding="14,8"
                        ToolTip="Copy the selected preset to a new name. Useful when iterating on variants of a recurring show."/>
                <Button Grid.Column="2"
                        Style="{StaticResource Wd.Button.Ghost}"
                        Content="Export…"
                        Click="OnExport"
                        Margin="8,0,0,0"
                        Padding="14,8"
                        ToolTip="Save every preset as a single .json bundle. Useful for moving a curated library between machines, or sharing with a colleague."/>
                <Button Grid.Column="3"
                        Style="{StaticResource Wd.Button.Ghost}"
                        Content="Import…"
                        Click="OnImport"
                        Margin="8,0,0,0"
                        Padding="14,8"
                        ToolTip="Load presets from a .json bundle. Existing presets with the same name are skipped unless you confirm overwrite."/>
                <Button Grid.Column="5"
                        Style="{StaticResource Wd.Button.Ghost}"
                        Content="Cancel"
                        Click="OnCancel"
                        Margin="0,0,8,0"
                        Padding="14,8"/>
                <Button Grid.Column="6"
                        Style="{StaticResource Wd.Button.Primary}"
                        Content="Apply"
                        Click="OnApply"
                        IsEnabled="False"
                        x:Name="ApplyButton"
                        Padding="20,8"/>
            </Grid>
        </Grid>
    </Border>
</Window>
