<Window x:Class="ImageCatalog_2.MainWindow"
        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"
        Title="Image Catalog - WPF" Height="490" Width="800"
        Background="{DynamicResource WindowBackgroundBrush}" Foreground="{DynamicResource ControlForegroundBrush}">
    <Window.Resources>
        <ResourceDictionary>
            <!-- Light theme resources -->
            <ResourceDictionary x:Key="LightTheme">
                <SolidColorBrush x:Key="WindowBackgroundBrush" Color="White" />
                <SolidColorBrush x:Key="ControlBackgroundBrush" Color="White" />
                <SolidColorBrush x:Key="ControlForegroundBrush" Color="Black" />
                <SolidColorBrush x:Key="BorderBrush" Color="#DDD" />
                <SolidColorBrush x:Key="AccentBrush" Color="#0078D7" />
                <SolidColorBrush x:Key="DataGridBackgroundBrush" Color="White" />
                <SolidColorBrush x:Key="DataGridForegroundBrush" Color="Black" />
            </ResourceDictionary>

            <!-- Dark theme resources -->
            <ResourceDictionary x:Key="DarkTheme">
                <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#1E1E1E" />
                <SolidColorBrush x:Key="ControlBackgroundBrush" Color="#252526" />
                <SolidColorBrush x:Key="ControlForegroundBrush" Color="#E6E6E6" />
                <SolidColorBrush x:Key="BorderBrush" Color="#3A3A3A" />
                <SolidColorBrush x:Key="AccentBrush" Color="#0A84FF" />
                <SolidColorBrush x:Key="DataGridBackgroundBrush" Color="#252526" />
                <SolidColorBrush x:Key="DataGridForegroundBrush" Color="#E6E6E6" />
            </ResourceDictionary>
        </ResourceDictionary>
    </Window.Resources>
    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*" />
            <!-- Make the live view/right side narrower -->
            <ColumnDefinition Width="0.8*" />
        </Grid.ColumnDefinitions>

        <!-- Left: Tabs -->
        <TabControl Grid.Column="0" Margin="0,0,10,0">
            <TabItem Header="Generale">
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Percorsi" FontWeight="Bold" />
                        <StackPanel Margin="0,6,0,0">
                            <!-- Source path row: textbox with pick and open buttons aligned to the end -->
                            <Grid Margin="0,0,0,6">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Sorgente:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                                <TextBox Text="{Binding SourcePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" />
                                <Button Content="Scegli..." Width="88" Margin="8,0,0,0" Command="{Binding SelectSourceFolderCommand}" Grid.Column="2" />
                                <Button Content="Apri" Width="56" Margin="8,0,0,0" Click="OpenSourceFolder_Click" Grid.Column="3" />
                            </Grid>

                            <!-- Destination path row -->
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Destinazione:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                                <TextBox Text="{Binding DestinationPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" />
                                <Button Content="Scegli..." Width="88" Margin="8,0,0,0" Command="{Binding SelectDestinationFolderCommand}" Grid.Column="2" />
                                <Button Content="Apri" Width="56" Margin="8,0,0,0" Click="OpenDestinationFolder_Click" Grid.Column="3" />
                            </Grid>
                        </StackPanel>

                        <TextBlock Text="Opzioni" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Vertical" Margin="0,6,0,0">
                            <CheckBox Content="Crea miniature" IsChecked="{Binding CreateThumbnails}" />
                            <CheckBox Content="Forza JPEG" IsChecked="{Binding ForceJpeg}" />
                            <CheckBox Content="Aggiorna sottodirectory" IsChecked="{Binding UpdateSubdirectories}" />
                            <CheckBox Content="Crea sottocartelle" IsChecked="{Binding CreateSubfolders}" />
                            <CheckBox Content="Sovrascrivi immagini" IsChecked="{Binding OverwriteImages}" />
                        </StackPanel>

                        <TextBlock Text="Elaborazione" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Threads:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding ThreadsCount, Mode=TwoWay}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Chunk:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding ChunkSize, Mode=TwoWay}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Divisione cartelle" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="File per cartella:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding FilesPerFolder}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Suffisso:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding FolderSuffix}" Width="120" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Numerazione" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <RadioButton Content="Progressiva" IsChecked="{Binding UseProgressiveNumbering}" GroupName="Num" />
                            <RadioButton Content="Per file" IsChecked="{Binding UseFileNumbering}" GroupName="Num" Margin="8,0,0,0" />
                            <TextBlock Text="Cifre:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding CounterDigits}" Width="40" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Libreria Immagini" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <RadioButton Content="System.Graphics" IsChecked="{Binding UseSystemGraphics}" GroupName="Lib" />
                            <RadioButton Content="ImageSharp" IsChecked="{Binding UseImageSharp}" GroupName="Lib" Margin="8,0,0,0" />
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <TabItem Header="Testo">
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Testo Orizzontale" FontWeight="Bold" />
                        <TextBox Text="{Binding HorizontalText, Mode=TwoWay}" />

                        <TextBlock Text="Testo Verticale" FontWeight="Bold" Margin="0,8,0,0" />
                        <TextBox Text="{Binding VerticalText, Mode=TwoWay}" />

                        <TextBlock Text="Font" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal">
                            <ComboBox ItemsSource="{Binding AvailableFonts}" SelectedItem="{Binding FontName}" Width="250" />
                            <TextBox Text="{Binding FontSize}" Width="60" Margin="8,0,0,0" />
                            <CheckBox Content="Grassetto" IsChecked="{Binding FontBold}" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Colore testo" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding TextColorRGB}" Width="120" />
                            <Button Content="Seleziona colore" Command="{Binding SelectColorCommand}" Margin="8,0,0,0" />
                            <Button Content="Seleziona trasparente" Command="{Binding SelectTransparentColorCommand}" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Dimensioni verticale" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Size:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding VerticalTextSize}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Margin:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding VerticalTextMargin}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Trasparenza testo:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding TextTransparency}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Margine testo:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding TextMargin}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <TabItem Header="Foto">
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Dimensioni foto grandi" FontWeight="Bold" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBox Text="{Binding PhotoBigWidth}" Width="80" />
                            <TextBox Text="{Binding PhotoBigHeight}" Width="80" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Opzioni foto" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Vertical" Margin="0,6,0,0">
                            <CheckBox Content="Mantieni dimensioni originali" IsChecked="{Binding KeepOriginalDimensions}" />
                            <CheckBox Content="Rotazione automatica" IsChecked="{Binding AutomaticRotation}" />
                            <CheckBox Content="Aggiungi tempo" IsChecked="{Binding AddTime}" />
                            <CheckBox Content="Aggiungi tempo gara" IsChecked="{Binding AddRaceTime}" />
                        </StackPanel>

                        <TextBlock Text="JPEG" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Qualità:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding JpegQuality}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Miniature Qualità:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding JpegQualityThumbnail}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <TabItem Header="Miniature">
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Miniature" FontWeight="Bold" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Prefisso:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding ThumbnailPrefix}" Width="120" Margin="8,0,0,0" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBox Text="{Binding ThumbnailWidth}" Width="80" />
                            <TextBox Text="{Binding ThumbnailHeight}" Width="80" Margin="8,0,0,0" />
                        </StackPanel>

                        <!-- New unified thumbnail mode selector (Italian labels) -->
                        <StackPanel Orientation="Vertical" Margin="0,8,0,0">
                            <TextBlock Text="Modalità miniature:" VerticalAlignment="Center" />
                            <ComboBox SelectedIndex="{Binding ThumbnailOptionIndex, Mode=TwoWay}" Width="220" Margin="0,6,0,0">
                                <ComboBoxItem>Nessuna</ComboBoxItem>
                                <ComboBoxItem>Aggiungi scritta</ComboBoxItem>
                                <ComboBoxItem>Nome file</ComboBoxItem>
                                <ComboBoxItem>Aggiungi orario</ComboBoxItem>
                                <ComboBoxItem>Nome+Orario</ComboBoxItem>
                                <ComboBoxItem>Tempo gara</ComboBoxItem>
                            </ComboBox>
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <TabItem Header="Logo">
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Logo" FontWeight="Bold" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <Button Content="Seleziona logo" Command="{Binding SelectLogoFileCommand}" />
                            <TextBlock Text="{Binding LogoFile}" Margin="8,0,0,0" VerticalAlignment="Center" Width="250" TextTrimming="CharacterEllipsis" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                            <Image Name="LogoPreview" Width="160" Height="160" Stretch="Uniform" VerticalAlignment="Center" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                            <TextBox Text="{Binding LogoWidth}" Width="80" />
                            <TextBox Text="{Binding LogoHeight}" Width="80" Margin="8,0,0,0" />
                        </StackPanel>
                        <CheckBox Content="Aggiungi logo" IsChecked="{Binding AddLogo}" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                            <TextBlock Text="Margine:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding LogoMargin}" Width="80" Margin="8,0,0,0" />
                            <TextBlock Text="Trasparenza:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding LogoTransparency}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                            <TextBlock Text="Posizione:" VerticalAlignment="Center" />
                            <ComboBox ItemsSource="{Binding HorizontalAlignments}" SelectedItem="{Binding LogoHorizontalPosition}" Width="120" Margin="8,0,0,0" />
                            <ComboBox ItemsSource="{Binding VerticalPositions}" SelectedItem="{Binding LogoVerticalPosition}" Width="120" Margin="8,0,0,0" />
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <TabItem Header="AI">
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="AI / OCR" FontWeight="Bold" />
                        <CheckBox Content="Estrai numeri dalle immagini" IsChecked="{Binding ExtractNumbers}" Margin="0,8,0,0" />

                        <TextBlock Text="Modelli" FontWeight="Bold" Margin="0,12,0,0" />
                        <Grid Margin="0,6,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Cartella modelli:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                            <TextBox Text="{Binding ModelsFolderPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" />
                            <Button Content="Scegli..." Width="88" Margin="8,0,0,0" Command="{Binding SelectModelsFolderCommand}" Grid.Column="2" />
                            <Button Content="Apri" Width="56" Margin="8,0,0,0" Click="OpenModelsFolder_Click" Grid.Column="3" />
                        </Grid>

                        <TextBlock Text="Output CSV" FontWeight="Bold" Margin="0,12,0,0" />
                        <Grid Margin="0,6,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Percorso CSV:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                            <TextBox Text="{Binding CsvOutputPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" />
                            <Button Content="Scegli..." Width="88" Margin="8,0,0,0" Command="{Binding SelectCsvOutputCommand}" Grid.Column="2" />
                            <Button Content="Apri" Width="56" Margin="8,0,0,0" Click="OpenCsvOutputFolder_Click" Grid.Column="3" />
                        </Grid>

                        <TextBlock Text="Anteprima risultati" FontWeight="Bold" Margin="0,12,0,0" />
                        <DataGrid ItemsSource="{Binding PreviewResults}" IsReadOnly="True" AutoGenerateColumns="False" Height="200" Margin="0,6,0,0">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Path" Binding="{Binding Path}" Width="*" />
                                <DataGridTextColumn Header="Text" Binding="{Binding Text}" Width="2*" />
                            </DataGrid.Columns>
                        </DataGrid>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>
        </TabControl>

        <!-- Right: Controls and live info -->
        <Border Grid.Column="1" BorderBrush="#DDD" BorderThickness="1" Padding="8" MaxWidth="280">
            <StackPanel>
                <!-- Buttons stacked vertically as requested -->
                <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
                    <Button Content="Carica" Width="120" Margin="0,0,0,8" Command="{Binding LoadSettingsCommand}" />
                    <Button Content="Salva" Width="120" Margin="0,0,0,8" Command="{Binding SaveSettingsCommand}" />
                    <Button Content="Avvia" Width="120" Height="36" Margin="0,6,0,8" Command="{Binding ProcessImagesCommand}" IsEnabled="{Binding UiEnabled}" />
                    <Button Content="Stop" Width="120" Height="36" Command="{Binding AsyncCancelOperationCommand}" />
                </StackPanel>

                <Separator Margin="0,12,0,12" />

                <TextBlock Text="Stato" FontWeight="Bold" />
                <TextBlock Text="{Binding ProcessingStatus}" TextWrapping="Wrap" />

                <TextBlock Text="Progresso" FontWeight="Bold" Margin="0,8,0,0" />
                <ProgressBar Minimum="0" Maximum="{Binding ProgressBarMaximum}" Value="{Binding ProgressBarValue}" Height="20" />
                <TextBlock Margin="0,6,0,0">
                    <Run Text="{Binding ProcessedImagesCount}" />
                    <Run Text=" / " />
                    <Run Text="{Binding TotalImagesCount}" />
                </TextBlock>

                <TextBlock Text="Velocità" FontWeight="Bold" Margin="0,8,0,0" />
                <TextBlock Text="{Binding SpeedCounter}" TextWrapping="Wrap" />
            </StackPanel>
        </Border>
    </Grid>
</Window>