<Window 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"
        x:Class="ImageCatalog_2.AvaloniaMainWindow"
        mc:Ignorable="d"
        Title="Image Catalog - Avalonia" Height="540" Width="800">

    <Grid RowDefinitions="*,Auto">

        <Grid Grid.Row="0" Margin="10">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="3*" />
                <ColumnDefinition Width="0.8*" />
            </Grid.ColumnDefinitions>

            <!-- Left: Tabs -->
            <TabControl Grid.Column="0" Margin="0,0,10,0">

                <!-- Tab 1: Generale -->
                <TabItem Header="Generale">
                    <ScrollViewer>
                        <StackPanel Margin="8">
                            <TextBlock Text="Percorsi" FontWeight="Bold" />
                            <StackPanel Margin="0,6,0,0">
                                <!-- Source -->
                                <Grid Margin="0,0,0,6" ColumnDefinitions="Auto,*,Auto,Auto">
                                    <TextBlock Text="Sorgente:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                                    <TextBox Text="{Binding SourcePath, Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" />
                                    <Button Width="88" Margin="8,0,0,0" Command="{Binding SelectSourceFolderCommand}" Grid.Column="2" Content="Scegli..." />
                                    <Button Width="56" Margin="8,0,0,0" Grid.Column="3" Click="OpenSourceFolder_Click" Content="Apri" />
                                </Grid>
                                <!-- Destination -->
                                <Grid ColumnDefinitions="Auto,*,Auto,Auto">
                                    <TextBlock Text="Destinazione:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                                    <TextBox Text="{Binding DestinationPath, Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" />
                                    <Button Width="88" Margin="8,0,0,0" Command="{Binding SelectDestinationFolderCommand}" Grid.Column="2" Content="Scegli..." />
                                    <Button Width="56" Margin="8,0,0,0" Grid.Column="3" Click="OpenDestinationFolder_Click" Content="Apri" />
                                </Grid>
                            </StackPanel>

                            <TextBlock Text="Opzioni" FontWeight="Bold" Margin="0,12,0,0" />
                            <StackPanel Margin="0,6,0,0">
                                <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>

                <!-- Tab 2: Testo -->
                <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}" AcceptsReturn="True"
                                     TextWrapping="Wrap" MinHeight="80" />

                            <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" VerticalAlignment="Center" />
                            </StackPanel>

                            <TextBlock Text="Colore testo (hex)" 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" />
                            </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>

                            <TextBlock Text="Tempo Gara" FontWeight="Bold" Margin="0,12,0,0" />
                            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                                <CheckBox Content="Aggiungi Orario" IsChecked="{Binding AddTime}" />
                                <CheckBox Content="Aggiungi tempo gara" IsChecked="{Binding AddRaceTime}" Margin="12,0,0,0" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                                <TextBlock Text="Partenza:" VerticalAlignment="Center" />
                                <CalendarDatePicker SelectedDate="{Binding RaceStartDate}"
                                                    IsEnabled="{Binding AddRaceTime}"
                                                    Margin="8,0,0,0" Width="200" />
                                <TextBox Text="{Binding TimeLabel}" Width="220" Margin="12,0,0,0" />
                            </StackPanel>
                        </StackPanel>
                    </ScrollViewer>
                </TabItem>

                <!-- Tab 3: Foto -->
                <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 Margin="0,6,0,0">
                                <CheckBox Content="Mantieni dimensioni originali" IsChecked="{Binding KeepOriginalDimensions}" />
                                <CheckBox Content="Rotazione automatica" IsChecked="{Binding AutomaticRotation}" />
                            </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>

                <!-- Tab 4: Miniature -->
                <TabItem Header="Miniature">
                    <ScrollViewer>
                        <StackPanel Margin="8">
                            <TextBlock Text="Miniature" FontWeight="Bold" />
                            <CheckBox Content="Crea miniature" IsChecked="{Binding CreateThumbnails}" Margin="0,6,0,0" />
                            <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>

                            <StackPanel 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>

                <!-- Tab 5: Logo -->
                <TabItem Header="Logo">
                    <ScrollViewer>
                        <StackPanel Margin="8">
                            <TextBlock Text="Logo" FontWeight="Bold" />
                            <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                                <Button Command="{Binding SelectLogoFileCommand}" Content="Seleziona logo" />
                                <TextBlock Text="{Binding LogoFile}" Margin="8,0,0,0" VerticalAlignment="Center"
                                           Width="250" TextTrimming="CharacterEllipsis" />
                            </StackPanel>
                            <StackPanel Margin="0,8,0,0">
                                <Image Name="LogoPreview" Width="160" Height="160" Stretch="Uniform" />
                            </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" />
                                <Button Command="{Binding SelectTransparentColorCommand}" Margin="8,0,0,0"
                                        Content="Seleziona trasparente" />
                            </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>

                <!-- Tab 6: AI -->
                <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" ColumnDefinitions="Auto,*,Auto,Auto">
                                <TextBlock Text="Cartella modelli:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                                <TextBox Text="{Binding ModelsFolderPath, Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" />
                                <Button Width="88" Margin="8,0,0,0" Command="{Binding SelectModelsFolderCommand}"
                                        Grid.Column="2" Content="Scegli..." />
                                <Button Width="56" Margin="8,0,0,0" Grid.Column="3"
                                        Click="OpenModelsFolder_Click" Content="Apri" />
                            </Grid>

                            <TextBlock Text="Output CSV" FontWeight="Bold" Margin="0,12,0,0" />
                            <Grid Margin="0,6,0,0" ColumnDefinitions="Auto,*,Auto,Auto">
                                <TextBlock Text="Percorso CSV:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                                <TextBox Text="{Binding CsvOutputPath, Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center" />
                                <Button Width="88" Margin="8,0,0,0" Command="{Binding SelectCsvOutputCommand}"
                                        Grid.Column="2" Content="Scegli..." />
                                <Button Width="56" Margin="8,0,0,0" Grid.Column="3"
                                        Click="OpenCsvOutputFolder_Click" Content="Apri" />
                            </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 -->
            <StackPanel Grid.Column="1">
                <StackPanel HorizontalAlignment="Right" Margin="0,0,0,12">
                    <Button Width="28" Height="28" Click="ToggleTheme_Click" ToolTip.Tip="Cambia tema"
                            HorizontalAlignment="Right" Padding="2" Content="🌙" />
                </StackPanel>
                <Border BorderBrush="#DDD" BorderThickness="1" Padding="8" MaxWidth="280">
                    <StackPanel>
                        <StackPanel>
                            <Button Width="120" Margin="0,0,0,8" Command="{Binding LoadSettingsCommand}" Content="Carica" />
                            <Button Width="120" Margin="0,0,0,8" Command="{Binding SaveSettingsCommand}" Content="Salva" />
                            <Button Width="120" Height="36" Margin="0,6,0,8"
                                    Command="{Binding ProcessImagesCommand}"
                                    IsEnabled="{Binding UiEnabled}" Content="Avvia" />
                            <Button Width="120" Height="36"
                                    Command="{Binding AsyncCancelOperationCommand}" Content="Stop" />
                        </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" />

                        <TextBlock Text="{Binding AppVersion}" Margin="0,8,0,0" Opacity="0.6" />
                    </StackPanel>
                </Border>
            </StackPanel>

        </Grid>
    </Grid>
</Window>
