<UserControl x:Class="MarcusLaw.OutlookAddin.UI.Views.ReadingPaneView"
             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"
             FlowDirection="RightToLeft"
             Background="White"
             FontFamily="Segoe UI"
             FontSize="12">
    <UserControl.Resources>
        <conv:CaseStatusToHebrewConverter x:Key="HebrewStatus" />
        <Style TargetType="TextBlock">
            <Setter Property="TextWrapping" Value="Wrap" />
        </Style>
        <Style x:Key="LabelStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="Gray" />
            <Setter Property="FontSize" Value="10" />
            <Setter Property="Margin" Value="0,4,0,2" />
        </Style>
        <Style x:Key="LinkButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Foreground" Value="#0066CC" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="HorizontalAlignment" Value="Right" />
        </Style>
    </UserControl.Resources>

    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Text="פרטי תיק" FontWeight="Bold" FontSize="14" Margin="0,0,0,8" />

        <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
            <Grid>
                <!-- Loading -->
                <StackPanel Visibility="{Binding LoadingVisibility}" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <ProgressBar IsIndeterminate="True" Width="120" Height="6" Margin="0,0,0,8" />
                    <TextBlock Text="טוען…" Foreground="Gray" HorizontalAlignment="Center" />
                </StackPanel>

                <!-- Empty / no selection -->
                <TextBlock Visibility="{Binding EmptyVisibility}"
                           Text="סמן מייל כדי לראות את פרטי התיק"
                           Foreground="Gray" HorizontalAlignment="Center" VerticalAlignment="Center"
                           TextAlignment="Center" />

                <!-- No match -->
                <StackPanel Visibility="{Binding NoMatchVisibility}">
                    <TextBlock Text="לא נמצא איש קשר ל-" Foreground="Gray" />
                    <TextBlock Text="{Binding SenderEmail}" FontWeight="Bold" />
                    <Button Content="חפש ידנית ב-Klear"
                            Command="{Binding OpenManualSearchCommand}"
                            Style="{StaticResource LinkButtonStyle}"
                            Margin="0,12,0,0" />
                </StackPanel>

                <!-- Ambiguous -->
                <StackPanel Visibility="{Binding AmbiguousVisibility}">
                    <TextBlock Text="{Binding AmbiguousMessage}" Foreground="DarkOrange" />
                    <TextBlock Style="{StaticResource LabelStyle}" Text="בחר איש קשר:" Margin="0,8,0,2" />
                    <ItemsControl ItemsSource="{Binding Candidates}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Border BorderBrush="#EEE" BorderThickness="0,0,0,1" Padding="0,4">
                                    <Button Command="{Binding DataContext.SelectCandidateCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                            CommandParameter="{Binding}"
                                            Background="Transparent"
                                            BorderThickness="0"
                                            Padding="0"
                                            Cursor="Hand"
                                            HorizontalContentAlignment="Right"
                                            HorizontalAlignment="Stretch">
                                        <StackPanel HorizontalAlignment="Stretch">
                                            <TextBlock Text="{Binding Name}" FontWeight="SemiBold" Foreground="#0066CC" />
                                            <TextBlock Text="{Binding AccountName}" Foreground="DimGray" FontSize="11" />
                                            <TextBlock Text="{Binding EmailAddress}" Foreground="Gray" FontSize="10" />
                                        </StackPanel>
                                    </Button>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                    <Button Content="פתח חיפוש ידני"
                            Command="{Binding OpenManualSearchCommand}"
                            Style="{StaticResource LinkButtonStyle}"
                            Margin="0,12,0,0" />
                </StackPanel>

                <!-- Match -->
                <StackPanel Visibility="{Binding MatchVisibility}">
                    <Button Content="→ חזור לרשימת אנשי קשר"
                            Command="{Binding BackToCandidatesCommand}"
                            Style="{StaticResource LinkButtonStyle}"
                            HorizontalAlignment="Right"
                            Margin="0,0,0,4"
                            Visibility="{Binding BackToCandidatesVisibility}" />
                    <TextBlock Style="{StaticResource LabelStyle}" Text="איש קשר" />
                    <TextBlock FontSize="14" FontWeight="Bold">
                        <Hyperlink Command="{Binding OpenInEspoCrmCommand}"
                                   TextDecorations="{x:Null}"
                                   Foreground="#0066CC">
                            <Run Text="{Binding Match.Contact.Name, Mode=OneWay}" />
                        </Hyperlink>
                    </TextBlock>
                    <TextBlock Text="{Binding Match.Contact.EmailAddress}" Foreground="Gray" />

                    <TextBlock Style="{StaticResource LabelStyle}" Text="לקוח/חברה" Visibility="{Binding AccountVisibility}" />
                    <TextBlock Text="{Binding Match.Account.Name}" Visibility="{Binding AccountVisibility}" />

                    <TextBlock Style="{StaticResource LabelStyle}" Text="תיקים מקושרים" Margin="0,12,0,2" />
                    <ItemsControl ItemsSource="{Binding RecentCases}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Border BorderBrush="#EEE" BorderThickness="0,0,0,1" Padding="0,4">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <StackPanel Grid.Column="0">
                                            <TextBlock>
                                                <Hyperlink Command="{Binding DataContext.OpenCaseInCrmCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                                           CommandParameter="{Binding}"
                                                           TextDecorations="{x:Null}"
                                                           Foreground="#0066CC">
                                                    <Run Text="["/><Run Text="{Binding Number, Mode=OneWay}"/><Run Text="] " />
                                                    <Run Text="{Binding Name, Mode=OneWay}" FontWeight="SemiBold" />
                                                </Hyperlink>
                                            </TextBlock>
                                            <TextBlock Text="{Binding Status, Converter={StaticResource HebrewStatus}}"
                                                       Foreground="Gray" FontSize="10" />
                                        </StackPanel>
                                        <Button Grid.Column="1"
                                                Content="תייק"
                                                Command="{Binding DataContext.FileToCaseCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                                CommandParameter="{Binding}"
                                                Padding="8,2"
                                                Margin="6,0,0,0"
                                                VerticalAlignment="Top"
                                                ToolTip="תייק את המייל הנוכחי לתיק הזה" />
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

                    <TextBlock Text="(אין תיקים פתוחים)" Foreground="Gray" FontStyle="Italic"
                               Visibility="{Binding NoCasesVisibility}" Margin="0,4,0,0" />
                </StackPanel>

                <!-- Error -->
                <TextBlock Visibility="{Binding ErrorVisibility}"
                           Text="{Binding ErrorMessage}"
                           Foreground="Firebrick" TextWrapping="Wrap" />
            </Grid>
        </ScrollViewer>

        <StackPanel Grid.Row="2" Margin="0,8,0,0" Visibility="{Binding ActionsVisibility}">
            <Button Content="תייק לתיק הזה"
                    Command="{Binding FileToThisCommand}"
                    Padding="8,4" Margin="0,0,0,4"
                    HorizontalAlignment="Stretch"
                    Visibility="{Binding SingleCaseFileVisibility}" />
            <TextBlock Text="יש כמה תיקים מקושרים — בחר תיק מהרשימה למעלה."
                       Foreground="Gray" FontStyle="Italic" FontSize="10"
                       Margin="0,0,0,4"
                       Visibility="{Binding MultiCaseHintVisibility}" />
            <Button Content="תייק למקום אחר…"
                    Command="{Binding FileElsewhereCommand}"
                    Padding="8,4" Margin="0,0,0,4"
                    HorizontalAlignment="Stretch" />
            <Button Content="פתח ב-Klear"
                    Command="{Binding OpenInEspoCrmCommand}"
                    Padding="8,4"
                    HorizontalAlignment="Stretch" />
        </StackPanel>
    </Grid>
</UserControl>
