<UserControl x:Class="SharepointToolbox.Views.Tabs.StorageView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:loc="clr-namespace:SharepointToolbox.Localization"
             xmlns:conv="clr-namespace:SharepointToolbox.Views.Converters"
             xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
             xmlns:common="clr-namespace:SharepointToolbox.Views.Common">
    <DockPanel LastChildFill="True">
        <!-- Options panel -->
        <ScrollViewer DockPanel.Dock="Left" Width="240" VerticalScrollBarVisibility="Auto"
                      HorizontalScrollBarVisibility="Disabled" Margin="8,8,4,8">
            <StackPanel>

                <!-- Scan options group -->
                <GroupBox Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[grp.scan.opts]}"
                          Margin="0,0,0,8">
                    <StackPanel Margin="4">
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.per.lib]}"
                                  IsChecked="{Binding PerLibrary}" Margin="0,2" />
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.subsites]}"
                                  IsChecked="{Binding IncludeSubsites}" Margin="0,2" />
                        <StackPanel Orientation="Horizontal" Margin="0,4,0,0">
                            <Label Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[lbl.folder.depth]}"
                                   VerticalAlignment="Center" Padding="0,0,4,0" />
                            <TextBox Text="{Binding FolderDepth, UpdateSourceTrigger=PropertyChanged}"
                                     Width="40" Height="22" VerticalAlignment="Center"
                                     IsEnabled="{Binding IsMaxDepth, Converter={StaticResource InverseBoolConverter}}" />
                        </StackPanel>
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.max.depth]}"
                                  IsChecked="{Binding IsMaxDepth}" Margin="0,2" />
                        <TextBlock Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.note]}"
                                   TextWrapping="Wrap" FontSize="11" Foreground="{DynamicResource TextMutedBrush}"
                                   Margin="0,6,0,0" />
                    </StackPanel>
                </GroupBox>

                <!-- Scan sources group: control what the scan captures -->
                <GroupBox Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[grp.scan.sources]}"
                          Margin="0,0,0,8">
                    <StackPanel Margin="4">
                        <StackPanel Orientation="Horizontal" Margin="0,2">
                            <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.scan.hidden]}"
                                      IsChecked="{Binding ScanHiddenLibraries}" VerticalAlignment="Center" />
                            <common:InfoButton Margin="6,0,0,0"
                                               Title="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[help.storage.hidden.title]}"
                                               Body="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[help.storage.hidden.body]}" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,2">
                            <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.scan.preservation]}"
                                      IsChecked="{Binding ScanPreservationHold}" VerticalAlignment="Center" />
                            <common:InfoButton Margin="6,0,0,0"
                                               Title="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[help.storage.preservation.title]}"
                                               Body="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[help.storage.preservation.body]}" />
                        </StackPanel>
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.scan.attachments]}"
                                  IsChecked="{Binding ScanListAttachments}" Margin="0,2" />
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.scan.recyclebin]}"
                                  IsChecked="{Binding ScanRecycleBin}" Margin="0,2" />
                    </StackPanel>
                </GroupBox>

                <!-- Report filter group: control what appears in DataGrid + exports -->
                <GroupBox Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[grp.report.filter]}"
                          Margin="0,0,0,8">
                    <StackPanel Margin="4">
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.show.libraries]}"
                                  IsChecked="{Binding ShowLibraries}" Margin="0,2" />
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.show.hidden]}"
                                  IsChecked="{Binding ShowHiddenLibraries}" Margin="0,2" />
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.show.preservation]}"
                                  IsChecked="{Binding ShowPreservationHold}" Margin="0,2" />
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.show.attachments]}"
                                  IsChecked="{Binding ShowListAttachments}" Margin="0,2" />
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.show.recyclebin]}"
                                  IsChecked="{Binding ShowRecycleBin}" Margin="0,2" />
                        <CheckBox Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.show.subsites]}"
                                  IsChecked="{Binding ShowSubsites}" Margin="0,2" />
                        <Separator Margin="0,4" />
                        <CheckBox IsChecked="{Binding CombineRecycleBinStages}" Margin="0,2">
                            <TextBlock Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[chk.combine.recyclebin]}"
                                       TextWrapping="Wrap" MaxWidth="195" />
                        </CheckBox>
                    </StackPanel>
                </GroupBox>

                <!-- Action buttons -->
                <Button Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[btn.gen.storage]}"
                        Command="{Binding RunCommand}"
                        Height="28" Margin="0,0,0,4" />
                <Button Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[btn.cancel]}"
                        Command="{Binding CancelCommand}"
                        Height="28" Margin="0,0,0,8" />

                <!-- Export group -->
                <GroupBox Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[grp.export.fmt]}"
                          Margin="0,0,0,8">
                    <StackPanel Margin="4">
                        <Label Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[export.split.label]}" Margin="0,0,0,0" Padding="0,2" />
                        <ComboBox SelectedIndex="{Binding SplitModeIndex}" Height="24" Margin="0,0,0,4">
                            <ComboBoxItem Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[export.split.single]}" />
                            <ComboBoxItem Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[export.split.bySite]}" />
                        </ComboBox>
                        <Label Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[export.html.layout.label]}" Padding="0,2" />
                        <ComboBox SelectedIndex="{Binding HtmlLayoutIndex}" Height="24" Margin="0,0,0,6">
                            <ComboBoxItem Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[export.html.layout.separate]}" />
                            <ComboBoxItem Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[export.html.layout.tabbed]}" />
                        </ComboBox>
                        <Button Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.rad.csv]}"
                                Command="{Binding ExportCsvCommand}"
                                Height="26" Margin="0,2" />
                        <Button Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.rad.html]}"
                                Command="{Binding ExportHtmlCommand}"
                                Height="26" Margin="0,2" />
                    </StackPanel>
                </GroupBox>

                <!-- Chart view toggle -->
                <GroupBox Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.chart.toggle]}"
                          Margin="0,0,0,8">
                    <StackPanel Margin="4">
                        <RadioButton Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.chart.donut]}"
                                     IsChecked="{Binding IsDonutChart}" Margin="0,2" />
                        <RadioButton Content="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.chart.bar]}"
                                     IsChecked="{Binding IsDonutChart, Converter={StaticResource InverseBoolConverter}}"
                                     Margin="0,2" />
                    </StackPanel>
                </GroupBox>

                <!-- Status -->
                <TextBlock Text="{Binding StatusMessage}" TextWrapping="Wrap"
                           FontSize="11" Foreground="{DynamicResource TextMutedBrush}" Margin="0,4" />
            </StackPanel>
        </ScrollViewer>

        <!-- Right content area: Summary + DataGrid on top, Chart on bottom -->
        <Grid Margin="4,8,8,8">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" MinHeight="150" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="300" MinHeight="200" />
            </Grid.RowDefinitions>

            <!-- Summary bar -->
            <Border Grid.Row="0" Background="{DynamicResource AccentSoftBrush}" CornerRadius="4" Padding="12,8" Margin="0,0,0,6">
                <Border.Style>
                    <Style TargetType="Border">
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding HasResults}" Value="True">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <StackPanel Orientation="Horizontal">
                    <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                        <TextBlock Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[storage.lbl.total_size_colon]}" FontWeight="SemiBold" />
                        <TextBlock Text="{Binding SummaryTotalSize, Converter={StaticResource BytesConverter}, Mode=OneWay}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                        <TextBlock Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[storage.lbl.version_size_colon]}" FontWeight="SemiBold" />
                        <TextBlock Text="{Binding SummaryVersionSize, Converter={StaticResource BytesConverter}, Mode=OneWay}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                        <TextBlock Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[storage.lbl.recyclebin_colon]}" FontWeight="SemiBold" />
                        <TextBlock Text="{Binding SummaryRecycleBinSize, Converter={StaticResource BytesConverter}, Mode=OneWay}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="0,0,24,0">
                        <TextBlock Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[storage.lbl.files_colon]}" FontWeight="SemiBold" />
                        <TextBlock Text="{Binding SummaryFileCount, StringFormat=N0, Mode=OneWay}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[storage.lbl.spo_reported_colon]}" FontWeight="SemiBold" />
                        <TextBlock Text="{Binding SpoReportedTotalSize, Converter={StaticResource BytesConverter}, Mode=OneWay}" />
                    </StackPanel>
                </StackPanel>
            </Border>

            <!-- Results DataGrid -->
            <DataGrid x:Name="ResultsGrid"
                      Grid.Row="1"
                      ItemsSource="{Binding Results}"
                      IsReadOnly="True"
                      AutoGenerateColumns="False"
                      VirtualizingPanel.IsVirtualizing="True"
                      VirtualizingPanel.VirtualizationMode="Recycling">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.col.library]}"
                                            Width="*" MinWidth="160">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}"
                                           Margin="{Binding IndentLevel, Converter={StaticResource IndentConverter}}"
                                           VerticalAlignment="Center" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.col.site]}"
                                        Binding="{Binding SiteTitle}" Width="140" />
                    <DataGridTextColumn Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.col.kind]}"
                                        Binding="{Binding Kind, Converter={StaticResource StorageKindConverter}}"
                                        Width="120" />
                    <DataGridTextColumn Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.col.files]}"
                                        Binding="{Binding TotalFileCount, StringFormat=N0}"
                                        Width="70" ElementStyle="{StaticResource RightAlignStyle}" />
                    <DataGridTextColumn Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.col.size]}"
                                        Binding="{Binding TotalSizeBytes, Converter={StaticResource BytesConverter}}"
                                        Width="100" ElementStyle="{StaticResource RightAlignStyle}" />
                    <DataGridTextColumn Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.col.versions]}"
                                        Binding="{Binding VersionSizeBytes, Converter={StaticResource BytesConverter}}"
                                        Width="110" ElementStyle="{StaticResource RightAlignStyle}" />
                    <DataGridTextColumn Header="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.col.lastmod]}"
                                        Binding="{Binding LastModified, StringFormat=yyyy-MM-dd}"
                                        Width="110" />
                </DataGrid.Columns>
            </DataGrid>

            <!-- Splitter between DataGrid and Chart -->
            <GridSplitter Grid.Row="2" Height="5" HorizontalAlignment="Stretch"
                          Background="{DynamicResource BorderSoftBrush}" ResizeDirection="Rows" />

            <!-- Chart panel -->
            <Border Grid.Row="3" BorderBrush="{DynamicResource BorderSoftBrush}" BorderThickness="1" CornerRadius="4"
                    Padding="8" Background="{DynamicResource SurfaceBrush}">
                <Grid>
                    <!-- Chart title -->
                    <TextBlock Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.chart.title]}"
                               FontWeight="SemiBold" FontSize="14" VerticalAlignment="Top"
                               HorizontalAlignment="Left" Margin="4,0,0,0" />

                    <!-- No data placeholder -->
                    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                               Foreground="{DynamicResource TextMutedBrush}" FontSize="12"
                               Text="{Binding Source={x:Static loc:TranslationSource.Instance}, Path=[stor.chart.nodata]}">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding HasChartData}" Value="False">
                                        <Setter Property="Visibility" Value="Visible" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>

                    <!-- Pie/Donut chart wrapper (visible when IsDonutChart=true AND HasChartData=true) -->
                    <Grid Margin="4,24,4,4">
                        <Grid.Style>
                            <Style TargetType="Grid">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <MultiDataTrigger>
                                        <MultiDataTrigger.Conditions>
                                            <Condition Binding="{Binding IsDonutChart}" Value="True" />
                                            <Condition Binding="{Binding HasChartData}" Value="True" />
                                        </MultiDataTrigger.Conditions>
                                        <Setter Property="Visibility" Value="Visible" />
                                    </MultiDataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Style>
                        <lvc:PieChart x:Name="StoragePieChart"
                                      Series="{Binding PieChartSeries}"
                                      LegendPosition="Right"
                                      LegendTextPaint="{Binding LegendTextPaint}"
                                      LegendBackgroundPaint="{Binding LegendBackgroundPaint}"
                                      TooltipTextPaint="{Binding TooltipTextPaint}"
                                      TooltipBackgroundPaint="{Binding TooltipBackgroundPaint}" />
                    </Grid>

                    <!-- Bar chart wrapper (visible when IsDonutChart=false AND HasChartData=true) -->
                    <Grid Margin="4,24,4,4">
                        <Grid.Style>
                            <Style TargetType="Grid">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <MultiDataTrigger>
                                        <MultiDataTrigger.Conditions>
                                            <Condition Binding="{Binding IsDonutChart}" Value="False" />
                                            <Condition Binding="{Binding HasChartData}" Value="True" />
                                        </MultiDataTrigger.Conditions>
                                        <Setter Property="Visibility" Value="Visible" />
                                    </MultiDataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Style>
                        <lvc:CartesianChart Series="{Binding BarChartSeries}"
                                            XAxes="{Binding BarXAxes}"
                                            YAxes="{Binding BarYAxes}"
                                            LegendPosition="Hidden"
                                            TooltipTextPaint="{Binding TooltipTextPaint}"
                                            TooltipBackgroundPaint="{Binding TooltipBackgroundPaint}" />
                    </Grid>
                </Grid>
            </Border>
        </Grid>
    </DockPanel>
</UserControl>
