<Window x:Class="MarcusLaw.OutlookAddin.UI.Dialogs.SaveAttachmentsDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:conv="clr-namespace:MarcusLaw.OutlookAddin.UI.Converters"
        Title="שמור קבצים מצורפים ל-Klear"
        Height="560" Width="620"
        MinHeight="420" MinWidth="500"
        FlowDirection="RightToLeft"
        WindowStartupLocation="CenterOwner"
        ShowInTaskbar="False"
        ResizeMode="CanResize"
        Background="#F8FAFC"
        FontFamily="Segoe UI, David, Frank Ruehl, Arial">
    <Window.Resources>
        <conv:CaseStatusToHebrewConverter x:Key="CaseStatusHe" />
        <BooleanToVisibilityConverter x:Key="BoolToVis" />

        <SolidColorBrush x:Key="Primary"      Color="#2563EB" />
        <SolidColorBrush x:Key="PrimaryHover" Color="#1D4ED8" />
        <SolidColorBrush x:Key="Border"       Color="#E2E8F0" />
        <SolidColorBrush x:Key="Muted"        Color="#64748B" />
        <SolidColorBrush x:Key="CardBg"       Color="White" />
        <SolidColorBrush x:Key="HoverBg"      Color="#F1F5F9" />
        <SolidColorBrush x:Key="SelectedBg"   Color="#DBEAFE" />

        <Style x:Key="Card" TargetType="Border">
            <Setter Property="Background" Value="{StaticResource CardBg}" />
            <Setter Property="BorderBrush" Value="{StaticResource Border}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="CornerRadius" Value="6" />
        </Style>

        <Style x:Key="PrimaryButton" TargetType="Button">
            <Setter Property="Background" Value="{StaticResource Primary}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Padding" Value="22,8" />
            <Setter Property="FontWeight" Value="SemiBold" />
            <Setter Property="MinWidth" Value="110" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Bd" Background="{TemplateBinding Background}" CornerRadius="5" Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource PrimaryHover}" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Bd" Property="Background" Value="#94A3B8" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="SecondaryButton" TargetType="Button">
            <Setter Property="Background" Value="White" />
            <Setter Property="Foreground" Value="#0F172A" />
            <Setter Property="BorderBrush" Value="{StaticResource Border}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Padding" Value="20,8" />
            <Setter Property="MinWidth" Value="110" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Bd" Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="5" Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource HoverBg}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>

    <Grid Margin="16" x:Name="RootGrid">
        <Grid.Resources>
            <Style x:Key="CaseGrid" TargetType="DataGrid">
                <Setter Property="AutoGenerateColumns" Value="False" />
                <Setter Property="HeadersVisibility" Value="Column" />
                <Setter Property="GridLinesVisibility" Value="Horizontal" />
                <Setter Property="HorizontalGridLinesBrush" Value="#E2E8F0" />
                <Setter Property="RowBackground" Value="White" />
                <Setter Property="AlternatingRowBackground" Value="#F8FAFC" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="CanUserAddRows" Value="False" />
                <Setter Property="CanUserDeleteRows" Value="False" />
                <Setter Property="CanUserResizeRows" Value="False" />
                <Setter Property="CanUserReorderColumns" Value="False" />
                <Setter Property="SelectionMode" Value="Single" />
                <Setter Property="SelectionUnit" Value="FullRow" />
                <Setter Property="IsReadOnly" Value="True" />
                <Setter Property="RowHeight" Value="30" />
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!-- Search -->
        <Border Grid.Row="0" Style="{StaticResource Card}" Padding="10,8" Margin="0,0,0,8">
            <DockPanel LastChildFill="True">
                <TextBlock DockPanel.Dock="Left" Text="🔍" VerticalAlignment="Center" FontSize="16" Margin="0,0,8,0" />
                <TextBox x:Name="SearchBox"
                         Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
                         BorderThickness="0"
                         Background="Transparent"
                         FontSize="14"
                         VerticalAlignment="Center" />
            </DockPanel>
        </Border>

        <!-- Cases -->
        <Border Grid.Row="1" Style="{StaticResource Card}">
            <DataGrid ItemsSource="{Binding Cases}"
                      SelectedItem="{Binding SelectedCase}"
                      Style="{StaticResource CaseGrid}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="מס׳ תיק" Binding="{Binding Number}" Width="80" />
                    <DataGridTextColumn Header="שם" Binding="{Binding Name}" Width="*" />
                    <DataGridTextColumn Header="מס׳ בבית משפט" Binding="{Binding CourtCaseNumber}" Width="140" />
                    <DataGridTextColumn Header="סטטוס" Binding="{Binding Status, Converter={StaticResource CaseStatusHe}}" Width="110" />
                </DataGrid.Columns>
            </DataGrid>
        </Border>

        <!-- Folder -->
        <DockPanel Grid.Row="2" Margin="0,10,0,0" LastChildFill="True">
            <TextBlock DockPanel.Dock="Left" Text="תיקייה:" VerticalAlignment="Center"
                       Margin="0,0,8,0" Foreground="{StaticResource Muted}" FontSize="12" />
            <TextBlock DockPanel.Dock="Right" Text="טוען…" VerticalAlignment="Center"
                       Margin="8,0,0,0" Foreground="{StaticResource Muted}" FontSize="12"
                       Visibility="{Binding IsLoadingSubfolders, Converter={StaticResource BoolToVis}}" />
            <ComboBox ItemsSource="{Binding Subfolders}"
                      SelectedItem="{Binding SelectedSubfolder}"
                      Padding="6,4">
                <ComboBox.Style>
                    <Style TargetType="ComboBox">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsLoadingSubfolders}" Value="True">
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ComboBox.Style>
            </ComboBox>
        </DockPanel>

        <!-- Attachments header -->
        <TextBlock Grid.Row="3" Margin="0,12,0,4" Foreground="{StaticResource Muted}" FontSize="12">
            <Run Text="קבצים מצורפים (" /><Run Text="{Binding Attachments.Count, Mode=OneWay}" /><Run Text=")" />
        </TextBlock>

        <!-- Attachments list -->
        <Border Grid.Row="4" Style="{StaticResource Card}" MaxHeight="160">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
                <ItemsControl ItemsSource="{Binding Attachments}" Margin="6">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <DockPanel Margin="0,3" LastChildFill="True">
                                <CheckBox DockPanel.Dock="Right" IsChecked="{Binding IsSelected, Mode=TwoWay}"
                                          VerticalAlignment="Center" />
                                <TextBlock Text="{Binding SizeDisplay}"
                                           DockPanel.Dock="Left"
                                           Foreground="{StaticResource Muted}"
                                           FontSize="11"
                                           VerticalAlignment="Center"
                                           Margin="8,0,0,0" />
                                <TextBox Text="{Binding FileName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                         IsEnabled="{Binding IsSelected}"
                                         VerticalAlignment="Center"
                                         BorderThickness="1"
                                         BorderBrush="{StaticResource Border}"
                                         Padding="6,3"
                                         Margin="8,0,0,0"
                                         ToolTip="לחץ כדי לערוך את שם הקובץ לפני השמירה" />
                            </DockPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </Border>

        <!-- Action row -->
        <DockPanel Grid.Row="5" Margin="0,12,0,0" LastChildFill="True">
            <TextBlock DockPanel.Dock="Right"
                       Text="{Binding StatusMessage}"
                       VerticalAlignment="Center"
                       Margin="0,0,8,0"
                       Foreground="{StaticResource Muted}"
                       FontSize="12"
                       TextTrimming="CharacterEllipsis" />
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                <Button Command="{Binding SubmitCommand}"
                        IsDefault="True"
                        Content="שמור"
                        Style="{StaticResource PrimaryButton}"
                        Margin="0,0,8,0" />
                <Button Command="{Binding CancelCommand}"
                        IsCancel="True"
                        Content="ביטול"
                        Style="{StaticResource SecondaryButton}" />
            </StackPanel>
        </DockPanel>
    </Grid>
</Window>
