<?xml version="1.0" encoding="utf-8" ?>
<Page
    x:Class="AmiReel.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    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"
    mc:Ignorable="d">

    <Grid x:Name="RootGrid" Padding="20" RowSpacing="16" Background="{ThemeResource PageBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Orientation="Horizontal" Spacing="8">
            <Button x:Name="VideoTabButton" Content="Video" Click="VideoTab_Click" Style="{StaticResource PrimaryButtonStyle}" MinWidth="120" />
            <Button x:Name="ShortsTabButton" Content="Shorts" Click="ShortsTab_Click" MinWidth="120" />
        </StackPanel>

        <Grid Grid.Row="1" ColumnSpacing="16" x:Name="VideoContentPanel">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Border Grid.Column="0" Padding="18" Style="{StaticResource CardBorderStyle}">
                <StackPanel Spacing="12">
                    <TextBlock Text="Source recordings" Style="{StaticResource SectionTitleStyle}" />
                    <StackPanel Orientation="Horizontal" Spacing="10">
                        <Button Content="Add video files..." Click="AddInputs_Click" />
                        <Button Content="Clear" Click="ClearInputs_Click" />
                    </StackPanel>
                    <ListView x:Name="InputList"
                              Height="130"
                              SelectionMode="Single"
                              SelectionChanged="InputList_SelectionChanged"
                              AllowDrop="True"
                              DragOver="InputList_DragOver"
                              Drop="InputList_Drop"
                              ToolTipService.ToolTip="Drag and drop video files here to add them" />
                    <Button x:Name="PreviewSourceButton"
                            Content="Preview selected"
                            Click="PreviewSource_Click"
                            HorizontalAlignment="Left"
                            IsEnabled="False" />
                </StackPanel>
            </Border>

            <Border Grid.Column="1" Padding="18" Style="{StaticResource CardBorderStyle}">
                <StackPanel Spacing="12">
                    <TextBlock Text="End card and output" Style="{StaticResource SectionTitleStyle}" />
                    <TextBlock Text="Leave blank to use a built-in black end card." Style="{StaticResource MutedTextStyle}" />

                    <Grid ColumnSpacing="10" RowSpacing="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <TextBox x:Name="EndCardBox" Grid.Row="0" MinHeight="40" />
                        <Button Grid.Row="0" Grid.Column="1" Content="Browse..." Click="BrowseEndCard_Click" VerticalAlignment="Stretch" MinWidth="120" />

                        <TextBox x:Name="OutputFolderBox" Grid.Row="1" MinHeight="40" />
                        <Button Grid.Row="1" Grid.Column="1" Content="Browse..." Click="BrowseOutput_Click" VerticalAlignment="Stretch" MinWidth="120" />

                        <TextBox x:Name="OutputNameBox" Grid.Row="2" Grid.ColumnSpan="2" MinHeight="40" />
                    </Grid>
                </StackPanel>
            </Border>
        </Grid>

        <Grid Grid.Row="1" RowSpacing="16" x:Name="ShortsContentPanel" Visibility="Collapsed">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid ColumnSpacing="16">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Border Grid.Column="0" Padding="18" Style="{StaticResource CardBorderStyle}">
                    <StackPanel Spacing="12">
                        <TextBlock Text="Short source and clip" Style="{StaticResource SectionTitleStyle}" />
                        <Grid ColumnSpacing="10" RowSpacing="10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <TextBox x:Name="ShortInputBox" Grid.Row="0" MinHeight="40" ToolTipService.ToolTip="Source video for this Short" />
                            <Button Grid.Row="0" Grid.Column="1" Content="Browse..." Click="BrowseShortInput_Click" VerticalAlignment="Stretch" MinWidth="120" />
                            <TextBox x:Name="ShortOutputBox" Grid.Row="1" MinHeight="40" ToolTipService.ToolTip="Output .mp4 path" />
                            <Button Grid.Row="1" Grid.Column="1" Content="Browse..." Click="BrowseShortOutput_Click" VerticalAlignment="Stretch" MinWidth="120" />
                        </Grid>
                        <Grid ColumnSpacing="10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <StackPanel Grid.Column="0">
                                <TextBlock Text="Start (HH:MM:SS)" Style="{StaticResource FieldLabelStyle}" />
                                <TextBox x:Name="ShortStartBox" Text="00:00:00" />
                            </StackPanel>
                            <StackPanel Grid.Column="1">
                                <TextBlock Text="Duration (seconds)" Style="{StaticResource FieldLabelStyle}" />
                                <TextBox x:Name="ShortDurationBox" Text="30" />
                            </StackPanel>
                        </Grid>
                        <Button x:Name="PreviewShortSourceButton" Content="Preview source" Click="PreviewShortSource_Click" HorizontalAlignment="Left" />
                    </StackPanel>
                </Border>

                <Border Grid.Column="1" Padding="18" Style="{StaticResource CardBorderStyle}">
                    <StackPanel Spacing="12">
                        <TextBlock Text="Title card" Style="{StaticResource SectionTitleStyle}" />
                        <TextBox x:Name="ShortTitleBox" Header="Title" ToolTipService.ToolTip="Production title, e.g. Ami-Back V1.04A" />
                        <Grid ColumnSpacing="10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBox x:Name="ShortGroupBox" Grid.Column="0" Header="Group" />
                            <TextBox x:Name="ShortYearBox" Grid.Column="1" Header="Year" />
                            <TextBox x:Name="ShortTypeBox" Grid.Column="2" Header="Type" />
                        </Grid>
                        <TextBox x:Name="ShortHookBox" Header="Opening hook" />
                        <TextBox x:Name="ShortWebsiteBox" Header="Closing website text" />
                    </StackPanel>
                </Border>
            </Grid>

            <Border Grid.Row="1" Padding="18" Style="{StaticResource CardBorderStyle}">
                <StackPanel Spacing="12">
                    <TextBlock Text="Style and encoding" Style="{StaticResource SectionTitleStyle}" />
                    <Grid ColumnSpacing="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <ComboBox x:Name="ShortStyleBox" Grid.Column="0" Header="Style" SelectionChanged="ShortStyleBox_SelectionChanged" HorizontalAlignment="Stretch">
                            <ComboBoxItem Content="Brand (title card image)" Tag="Brand" />
                            <ComboBoxItem Content="Pixel" Tag="Pixel" />
                            <ComboBoxItem Content="Mirror" Tag="Mirror" />
                            <ComboBoxItem Content="Crop" Tag="Crop" />
                            <ComboBoxItem Content="Workbench" Tag="Workbench" />
                            <ComboBoxItem Content="Blur" Tag="Blur" />
                            <ComboBoxItem Content="CRT" Tag="Crt" />
                            <ComboBoxItem Content="Copper bars" Tag="Copper" />
                            <ComboBoxItem Content="Stars" Tag="Stars" />
                            <ComboBoxItem Content="Grid" Tag="Grid" />
                            <ComboBoxItem Content="Tiles" Tag="Tiles" />
                            <ComboBoxItem Content="Scanlines" Tag="Scan" />
                            <ComboBoxItem Content="Starfield" Tag="Starfield" />
                            <ComboBoxItem Content="Plasma" Tag="Plasma" />
                            <ComboBoxItem Content="Rasterbars" Tag="Rasterbars" />
                            <ComboBoxItem Content="VHS" Tag="Vhs" />
                            <ComboBoxItem Content="Monitor" Tag="Monitor" />
                            <ComboBoxItem Content="Split" Tag="Split" />
                            <ComboBoxItem Content="Spectrum (needs audio)" Tag="Spectrum" />
                        </ComboBox>
                        <ComboBox x:Name="ShortPresetBox" Grid.Column="1" Header="x264 preset" HorizontalAlignment="Stretch">
                            <ComboBoxItem Content="ultrafast" Tag="ultrafast" />
                            <ComboBoxItem Content="superfast" Tag="superfast" />
                            <ComboBoxItem Content="veryfast" Tag="veryfast" />
                            <ComboBoxItem Content="faster" Tag="faster" />
                            <ComboBoxItem Content="fast" Tag="fast" />
                            <ComboBoxItem Content="medium" Tag="medium" />
                            <ComboBoxItem Content="slow" Tag="slow" />
                            <ComboBoxItem Content="slower" Tag="slower" />
                            <ComboBoxItem Content="veryslow" Tag="veryslow" />
                        </ComboBox>
                    </Grid>
                    <Grid x:Name="ShortBackgroundRow" ColumnSpacing="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="ShortBackgroundBox" Grid.Column="0" Header="Background image (brand style)" />
                        <Button Grid.Column="1" Content="Browse..." Click="BrowseShortBackground_Click" VerticalAlignment="Bottom" MinWidth="120" />
                    </Grid>
                    <Grid ColumnSpacing="10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="120" />
                        </Grid.ColumnDefinitions>
                        <TextBox x:Name="ShortFontBox" Grid.Column="0" Header="Bold font file (.ttf/.otf)" />
                        <Button Grid.Column="1" Content="Browse..." Click="BrowseShortFont_Click" VerticalAlignment="Bottom" MinWidth="120" />
                        <TextBox x:Name="ShortCrfBox" Grid.Column="2" Header="CRF" />
                    </Grid>
                </StackPanel>
            </Border>
        </Grid>

        <Border Grid.Row="2" Padding="18" Style="{StaticResource CardBorderStyle}">
            <StackPanel Spacing="12">
                <TextBlock Text="Render progress" Style="{StaticResource SectionTitleStyle}" />
                <StackPanel Orientation="Horizontal" Spacing="6">
                    <TextBlock x:Name="StageText" Text="Ready" FontSize="15" FontWeight="SemiBold" />
                    <TextBlock x:Name="PercentText" Text="0%" FontSize="15" FontWeight="SemiBold" Foreground="{ThemeResource AccentBrush}" />
                </StackPanel>
                <ProgressBar x:Name="RenderProgressBar" Minimum="0" Maximum="100" Height="8" />
                <TextBlock x:Name="StatusText" Text="Select the recordings and start the render. The end card is optional." TextWrapping="WrapWholeWords" Style="{StaticResource MutedTextStyle}" />
            </StackPanel>
        </Border>

        <Border Grid.Row="3" Padding="18" Style="{StaticResource CardBorderStyle}">
            <StackPanel Spacing="12">
                <TextBlock Text="FFmpeg log" Style="{StaticResource SectionTitleStyle}" />
                <TextBox x:Name="LogBox"
                         IsReadOnly="True"
                         AcceptsReturn="True"
                         TextWrapping="NoWrap"
                         ScrollViewer.VerticalScrollBarVisibility="Auto"
                         ScrollViewer.HorizontalScrollBarVisibility="Auto"
                         FontFamily="Consolas"
                         FontSize="12"
                         VerticalAlignment="Stretch"
                         Height="260" />
            </StackPanel>
        </Border>

        <Grid Grid.Row="4">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <StackPanel Grid.Column="0" Orientation="Horizontal" Spacing="10">
                <Button x:Name="OpenOutputButton" Content="Open output folder" Click="OpenOutput_Click" IsEnabled="False" HorizontalAlignment="Left" />
                <Button x:Name="PreviewRenderedButton" Content="Preview render" Click="PreviewRendered_Click" IsEnabled="False" />
            </StackPanel>

            <StackPanel Grid.Column="1" Orientation="Horizontal" Spacing="10">
                <Button x:Name="OpenSettingsButton" Content="&#xE713;" FontFamily="Segoe Fluent Icons, Segoe MDL2 Assets"
                        Click="OpenSettings_Click" Style="{StaticResource IconButtonStyle}"
                        ToolTipService.ToolTip="Settings" AutomationProperties.Name="Settings" />
                <Button x:Name="CancelButton" Content="Cancel" Click="Cancel_Click" IsEnabled="False" />
                <Button x:Name="RenderButton" Content="Start render" Click="Render_Click" Style="{StaticResource PrimaryButtonStyle}" />
            </StackPanel>
        </Grid>

        <ContentDialog x:Name="SettingsDialog"
                       Title="Settings"
                       PrimaryButtonText="Done"
                       CloseButtonText="Close"
                       DefaultButton="Primary"
                       Background="{ThemeResource PanelBrush}"
                       Foreground="{ThemeResource TextBrush}"
                       BorderBrush="{ThemeResource HeroBorderBrush}"
                       BorderThickness="1.5"
                       CornerRadius="18"
                       PrimaryButtonStyle="{StaticResource DialogPrimaryButtonStyle}"
                       CloseButtonStyle="{StaticResource DialogCloseButtonStyle}">
            <ScrollViewer MaxHeight="560" VerticalScrollBarVisibility="Auto">
                <StackPanel Spacing="16">
                    <Border Padding="14" Background="{ThemeResource PanelAltBrush}" BorderBrush="{ThemeResource BorderBrush}" BorderThickness="1" CornerRadius="12">
                        <StackPanel Spacing="10">
                            <TextBlock Text="Appearance" Style="{StaticResource SectionTitleStyle}" />
                            <ComboBox x:Name="ThemeBox" Header="Theme" SelectionChanged="ThemeBox_SelectionChanged">
                                <ComboBoxItem Content="Dark" Tag="Dark" />
                                <ComboBoxItem Content="Light" Tag="Light" />
                            </ComboBox>
                        </StackPanel>
                    </Border>

                    <Border Padding="14" Background="{ThemeResource PanelAltBrush}" BorderBrush="{ThemeResource BorderBrush}" BorderThickness="1" CornerRadius="12">
                        <StackPanel Spacing="10">
                            <TextBlock Text="Timing and screenshots" Style="{StaticResource SectionTitleStyle}" />
                            <Grid ColumnSpacing="10" RowSpacing="10">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <TextBox x:Name="TrimBox" Grid.Row="0" Grid.Column="0" Header="Trim start (seconds)" />
                                <TextBox x:Name="FadeBox" Grid.Row="0" Grid.Column="1" Header="Fade (seconds)" />
                                <TextBox x:Name="HoldBox" Grid.Row="1" Grid.Column="0" Header="End-card hold (seconds)" />
                                <TextBox x:Name="IntervalBox" Grid.Row="1" Grid.Column="1" Header="Thumbnail interval (seconds)" />
                            </Grid>
                        </StackPanel>
                    </Border>

                    <Border Padding="14" Background="{ThemeResource PanelAltBrush}" BorderBrush="{ThemeResource BorderBrush}" BorderThickness="1" CornerRadius="12">
                        <StackPanel Spacing="10">
                            <TextBlock Text="Video encoding" Style="{StaticResource SectionTitleStyle}" />
                            <ComboBox x:Name="EncoderBox" Header="Encoder">
                                <ComboBoxItem Content="Auto (NVENC → CPU fallback)" Tag="Auto" />
                                <ComboBoxItem Content="NVIDIA NVENC" Tag="NvidiaNvenc" />
                                <ComboBoxItem Content="CPU libx264" Tag="CpuX264" />
                            </ComboBox>
                            <TextBlock Text="Output: 3840 × 2160 · 50 FPS" Style="{StaticResource MutedTextStyle}" />
                        </StackPanel>
                    </Border>

                    <Border Padding="14" Background="{ThemeResource PanelAltBrush}" BorderBrush="{ThemeResource BorderBrush}" BorderThickness="1" CornerRadius="12">
                        <StackPanel Spacing="10">
                            <TextBlock Text="Source files" Style="{StaticResource SectionTitleStyle}" />
                            <CheckBox x:Name="MoveSourcesBox"
                                      Content="Move source videos to destination\originals"
                                      IsChecked="True" />
                            <TextBlock Text="After a successful render, original recordings are moved into an originals subfolder of the output folder. Uncheck to leave them where they are."
                                       TextWrapping="WrapWholeWords"
                                       Style="{StaticResource MutedTextStyle}" />
                        </StackPanel>
                    </Border>

                    <Border Padding="14" Background="{ThemeResource PanelAltBrush}" BorderBrush="{ThemeResource BorderBrush}" BorderThickness="1" CornerRadius="12">
                        <StackPanel Spacing="10">
                            <TextBlock Text="FFmpeg tools" Style="{StaticResource SectionTitleStyle}" />
                            <TextBlock Text="Optional override. Leave blank to auto-detect FFmpeg and FFprobe from PATH, then use the embedded fallback." TextWrapping="WrapWholeWords" Style="{StaticResource MutedTextStyle}" />
                            <Grid ColumnSpacing="10" RowSpacing="10">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <TextBox x:Name="FfmpegPathBox" Grid.Row="0" Header="ffmpeg.exe path" />
                                <Button Grid.Row="0" Grid.Column="1" Content="Browse..." Click="BrowseFfmpeg_Click" VerticalAlignment="Bottom" />
                                <TextBox x:Name="FfprobePathBox" Grid.Row="1" Header="ffprobe.exe path" />
                                <Button Grid.Row="1" Grid.Column="1" Content="Browse..." Click="BrowseFfprobe_Click" VerticalAlignment="Bottom" />
                                <TextBox x:Name="PreviewPlayerPathBox" Grid.Row="2" Header="Preview player path (optional, e.g. mpv.exe)" />
                                <Button Grid.Row="2" Grid.Column="1" Content="Browse..." Click="BrowsePreviewPlayer_Click" VerticalAlignment="Bottom" />
                            </Grid>
                        </StackPanel>
                    </Border>
                </StackPanel>
            </ScrollViewer>
        </ContentDialog>
    </Grid>
</Page>
