<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="was.droidtransfer.features.messaging.ux.MessagingThreadContentPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:was.droidtransfer.features.messaging.ux"
    xmlns:converters="using:was.droidtransfer.features.messaging.converters"
    xmlns:commonConverters="using:was.droidtransfer.features.converters"
    xmlns:commonDialogs="using:was.droidtransfer.features.dialogs"
    xmlns:featurebase="using:was.droidtransfer.features"
    xmlns:data="using:was.droidtransfer.features.messaging.data"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DataContext="{d:DesignInstance Type=local:MessagingThreadContentPage}"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" ActualThemeChanged="Page_ActualThemeChanged">
    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Light">
                    <x:Boolean x:Key="IsLightTheme" >True</x:Boolean>
                </ResourceDictionary>
                <ResourceDictionary x:Key="Dark">
                    <x:Boolean x:Key="IsLightTheme" >False</x:Boolean>
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MessageContentTextDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentDebugDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentImageDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentImageCTLDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentVideoDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentVideoCTLDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentAudioDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentAudioCTLDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentContactDataTemplate.xaml"/>
                <ResourceDictionary Source="MessageContentNotSupportedDataTemplate.xaml" />
                <ResourceDictionary>
                    <converters:BoolToFontWeightConverter x:Key="BoolToFontWeightConverter"/>
                    <converters:DateFilterOptionsToTextConverter x:Key="DateFilterOptionsToTextConverter"/>
                    <converters:MessageDetailToHorizontalAlignmentConverter x:Key="MessageDetailToHorizontalAlignmentConverter"/>
                    <converters:MessageDetailToJoinedContentConverter x:Key="MessageDetailToJoinedContentConverter" />
                    <converters:MessageDetailToMessageHeaderTextConverter x:Key="MessageDetailToMessageHeaderTextConverter"/>
                    <converters:MessageDetailToPersonPictureVisibilityConverter x:Key="MessageDetailToPersonPictureVisibilityConverter"/>
                    <converters:MessageDetailToThicknessConverter x:Key="MessageDetailToThicknessConverter"/>
                    <converters:MessageDetailToVisibilityConverter x:Key="MessageDetailToVisibilityConverter"/>
                    <converters:MessageTextToFontSizeConverter x:Key="MessageTextToFontSizeConverter" />
                    <converters:MessagesToHasImagesVisibilityConverter x:Key="MessagesToHasImagesVisibilityConverter" />
                    <converters:MessagesToImageContentsConverter x:Key="MessagesToImageContentsConverter" />
                    <converters:MessageToBackgroundBrushConverter x:Name="messageToBackgroundBrushConverter" x:Key="MessageToBackgroundBrushConverter" />
                    <commonConverters:ByteArrayToBitmapImageConverter x:Key="ByteArrayToBitmapImageConverter"/>
                    <commonConverters:CollectionToHasItemsBoolConverter x:Key="CollectionToHasItemsBoolConverter" />
                    <commonConverters:ColorToSolidColorBrushConverter x:Key="ColorToSolidColorBrushConverter"/>
                    <commonConverters:ValueToVisibilityConverter x:Key="ValueToVisibilityConverter"/>
                    <local:MessageContentTemplateSelector x:Key="MessageContentTemplateSelector"
                        TextDataTemplate="{StaticResource MessageContentTextDataTemplate}"
                        ImageDataTemplate="{StaticResource MessageContentImageDataTemplate}" 
                        ImageClickToLoadDataTemplate="{StaticResource MessageContentImageCTLDataTemplate}"
                        AudioDataTemplate="{StaticResource MessageContentAudioDataTemplate}"
                        AudioClickToLoadDataTemplate="{StaticResource MessageContentAudioCTLDataTemplate}"
                        VideoDataTemplate="{StaticResource MessageContentVideoDataTemplate}"
                        VideoClickToLoadDataTemplate="{StaticResource MessageContentVideoCTLDataTemplate}"
                        ContactDataTemplate="{StaticResource MessageContentContactDataTemplate}"
                        DebugDataTemplate="{StaticResource MessageContentDebugDataTemplate}"
                        NotSupportedDataTemplate="{StaticResource MessageContentNotSupportedDataTemplate}" />
                </ResourceDictionary>
                <local:MessageContentTextDataTemplate />
                <local:MessageContentImageDataTemplate
                    OnRequestSaveImageFromMessageContent="_messageContentImageDataTemplate_OnRequestSaveImageFromMessageContent"
                    OnRequestPreviewImageInMessageContent="_messageContentImageDataTemplate_OnRequestPreviewImageInMessageContent"/>
                <local:MessageContentImageCTLDataTemplate
                    OnRequestSaveImageFromMessageContent="_messageContentImageDataTemplate_OnRequestSaveImageFromMessageContent"
                    OnRequestPreviewImageInMessageContent="_messageContentImageDataTemplate_OnRequestPreviewImageInMessageContent"/>
                <local:MessageContentVideoDataTemplate
                    OnRequestSaveVideoFromMessageContent="_messageContentVideoDataTemplate_OnRequestSaveVideoFromMessageContent"
                    OnRequestPreviewVideoInMessageContent="_messageContentVideoDataTemplate_OnRequestPreviewVideoInMessageContent"/>
                <local:MessageContentVideoCTLDataTemplate
                    OnRequestSaveVideoFromMessageContent="_messageContentVideoDataTemplate_OnRequestSaveVideoFromMessageContent"
                    OnRequestPreviewVideoInMessageContent="_messageContentVideoDataTemplate_OnRequestPreviewVideoInMessageContent"/>
                <local:MessageContentAudioDataTemplate
                    OnRequestSaveAudioFromMessageContent="_messageContentAudioDataTemplate_OnRequestSaveAudioFromMessageContent" />
                <local:MessageContentAudioCTLDataTemplate
                    OnRequestSaveAudioFromMessageContent="_messageContentAudioDataTemplate_OnRequestSaveAudioFromMessageContent" />
                <local:MessageContentContactDataTemplate
                    OnOpenContact="_messageContentContactDataTemplate_OnOpenContact"
                    OnRequestSaveContactFromMessageContent="_messageContentContactDataTemplate_OnRequestSaveContactFromMessageContent"/>
            </ResourceDictionary.MergedDictionaries>
            
        </ResourceDictionary>
    </Page.Resources>
    <Grid>
        <!--    <SelectorBar>
            <SelectorBarItem x:Uid="SelectorBarItemConversation" Text="XX Conversation" IsSelected="True">
                <FontIcon FontSize="16" Glyph="&#xE8BD;"/>
            </SelectorBarItem>
            <SelectorBarItem x:Uid="SelectorBarItemImages" Text="XX Images">
                <FontIcon FontSize="16" Glyph="&#xE91B;"/>
            </SelectorBarItem>
        </SelectorBar>-->
        <Pivot x:Name="pivot" Margin="0" SelectionChanged="_pivot_SelectionChanged" Background="{ThemeResource WindowCaptionBackground}" Visibility="{x:Bind IsMessageContentLoading, Converter={StaticResource ValueToVisibilityConverter}, ConverterParameter='invert', Mode=OneWay}">
            <PivotItem x:Uid="PivotItemConversation" Margin="0">
                <PivotItem.Header>
                    <StackPanel Orientation="Horizontal"  >
                        <FontIcon FontSize="16" Glyph="&#xE8BD;" Foreground="{ThemeResource SystemAccentColor}"/>
                        <TextBlock Text="Conversation" Margin="8,0,0,0" VerticalAlignment="Center"/>
                    </StackPanel>
                </PivotItem.Header>
                <Border Style="{ThemeResource FeatureContentPageBorder}" Padding="0">
                    <Grid x:Name="gridContent">
                        <ScrollViewer IsDeferredScrollingEnabled="True" x:Name="messageScrollViewer"  HorizontalScrollMode="Disabled" HorizontalAlignment="Stretch"  Padding="30,0">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Grid Margin="0,20,0,0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="auto"/>
                                    </Grid.ColumnDefinitions>
                                    <StackPanel Orientation="Horizontal" >
                                        <PersonPicture x:Name="PersonPictureConveration" DisplayName="{Binding Conversation.Title}" PreferSmallImage="True" Height="70" VerticalAlignment="Center" Margin="0,0,15,0" ProfilePicture="{Binding Conversation.PictureData, Converter={StaticResource ByteArrayToBitmapImageConverter}, Mode=OneWay}" />
                                        <StackPanel VerticalAlignment="Center">
                                            <TextBlock x:Name="textBlockConversationHeader" Text="{x:Bind ConversationTitle, Mode=OneWay}" FontWeight="Bold" FontSize="24" />
                                        </StackPanel>
                                    </StackPanel>
                                    <StackPanel Orientation="Horizontal" Grid.Column="1" x:Name="stackPanelDateFiltered" Visibility="Collapsed" HorizontalAlignment="Right" Margin="20,0,0,0">
                                        <StackPanel VerticalAlignment="Center" >
                                            <TextBlock x:Uid="TextBlockMessagesFilteredByDate" Text="XX Messages filtered by date" FontWeight="Black"/>
                                            <TextBlock Text="{Binding DateFilter, Converter={StaticResource DateFilterOptionsToTextConverter}}" HorizontalAlignment="Right"/>
                                        </StackPanel>
                                        <Button x:Uid="ButtonClearDateFilter" Style="{StaticResource AccentButtonStyle}" x:Name="buttonClearDateFilter" Content="XX Clear Filter"  Margin="10,0,0,0" Click="_buttonClearDateFilter_Click"/>
                                    </StackPanel>
                                </Grid>
                                <Grid Grid.Row="1" x:Name="gridNoMessages" Visibility="{x:Bind Messages, Mode=OneWay, Converter={StaticResource CollectionToHasItemsBoolConverter}, ConverterParameter='invert'}">
                                    <TextBlock x:Name="textBlockNoMessagesFound" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{ThemeResource HeaderTextBlockStyle}" FontSize="18" />
                                </Grid>
                                <ItemsRepeater x:Name="itemRepeater" Grid.Row="1" ItemsSource="{x:Bind Messages, Mode=OneWay}" Visibility="{x:Bind Messages, Converter={StaticResource CollectionToHasItemsBoolConverter}, Mode=OneWay}">
                                    <ItemsRepeater.Layout>
                                        <StackLayout Orientation="Vertical" Spacing="0" />
                                    </ItemsRepeater.Layout>
                                    <ItemsRepeater.ItemTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid HorizontalAlignment="{Binding Converter={StaticResource MessageDetailToHorizontalAlignmentConverter}}" Margin="0,0,0,10" >
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="auto"/>
                                                        <ColumnDefinition Width="*"/>
                                                    </Grid.ColumnDefinitions>
                                                    <PersonPicture DisplayName="{Binding Participant.Name, TargetNullValue='', FallbackValue=''}" PreferSmallImage="True" Height="36" Margin="0,30,7,0" VerticalAlignment="Top" Visibility="{Binding Converter={StaticResource MessageDetailToPersonPictureVisibilityConverter}}"
                                                                   ProfilePicture="{Binding Participant.PictureData, Converter={StaticResource ByteArrayToBitmapImageConverter}, Mode=OneWay}"/>
                                                    <StackPanel Grid.Column="1" Margin="0,10,0,0">
                                                        <TextBlock Text="{Binding Converter={StaticResource MessageDetailToMessageHeaderTextConverter}}" Style="{ThemeResource CaptionTextBlockStyle}" Visibility="{Binding Converter={StaticResource MessageDetailToVisibilityConverter}}" Margin="9,0,20,2"/>
                                                        <Border Grid.Column="1" d:DataContext="{d:DesignInstance Type=data:IMessageDetail}" Background="{Binding Converter={StaticResource MessageToBackgroundBrushConverter}, ConverterParameter={ThemeResource IsLightTheme}}" CornerRadius="6" ActualThemeChanged="Border_ActualThemeChanged">
                                                            <StackPanel VerticalAlignment="Center" Padding="{Binding Converter={StaticResource MessageDetailToThicknessConverter}}">
                                                                <TextBlock Text="{Binding MessageText}" TextWrapping="Wrap" FontWeight="{Binding IsDateSeparator, Converter={StaticResource BoolToFontWeightConverter}}" Visibility="{Binding Text, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource ValueToVisibilityConverter}}" FontSize="{Binding Text, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource MessageTextToFontSizeConverter}}"/>
                                                                <ListView x:Name="listViewMessageContentCategories" SelectionMode="None" ItemsSource="{Binding Converter={StaticResource MessageDetailToJoinedContentConverter}, Mode=OneWay}" ItemTemplateSelector="{StaticResource MessageContentTemplateSelector}" Visibility="{Binding ItemsSource, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource CollectionToHasItemsBoolConverter}}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.IsHorizontalRailEnabled="False"
                                                                          ScrollViewer.IsVerticalRailEnabled="False" ScrollViewer.VerticalScrollMode="Disabled" ScrollViewer.VerticalScrollBarVisibility="Hidden" Margin="0,0,0,-15">
                                                                    <ListView.ItemContainerStyle>
                                                                        <Style TargetType="ListViewItem">
                                                                            <Setter Property="Margin" Value="0,0,0,0" />
                                                                            <Setter Property="Padding" Value="0,0,0,10" />
                                                                            <Setter Property="BorderThickness" Value="0" />
                                                                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                                                            <Setter Property="VerticalContentAlignment" Value="Stretch" />
                                                                        </Style>
                                                                    </ListView.ItemContainerStyle>
                                                                    <ListView.Style>
                                                                        <Style TargetType="ListView">
                                                                            <Setter Property="BorderThickness" Value="0" />
                                                                            <Setter Property="Padding" Value="0" />
                                                                            <Setter Property="Margin" Value="0" />
                                                                        </Style>
                                                                    </ListView.Style>
                                                                </ListView>
                                                            </StackPanel>
                                                        </Border>
                                                    </StackPanel>
                                                </Grid>
                                            </Grid>
                                        </DataTemplate>
                                    </ItemsRepeater.ItemTemplate>
                                </ItemsRepeater>
                            </Grid>
                        </ScrollViewer>
                        <InfoBar x:Name="infoBarLimitingMessages" IsOpen="True" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="16" CloseButtonClick="_infoBarLimitingMessages_CloseButtonClick" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                            <InfoBar.ActionButton>
                                <Button x:Uid="ActionButtonLimitingMessages" Content="XX Show All" Click="_infoBarLimitingMessages_ActionButtonClick" />
                            </InfoBar.ActionButton>
                        </InfoBar>
                    </Grid>
                </Border>
            </PivotItem>
            <PivotItem x:Uid="PivotItemImages" Margin="0" x:Name="pivotItemImages">
                <PivotItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <FontIcon FontSize="16" Glyph="&#xE91B;" Foreground="{ThemeResource SystemAccentColor}"/>
                        <TextBlock Text="Images" Margin="8,0,0,0" VerticalAlignment="Center"/>
                    </StackPanel>
                </PivotItem.Header>
                <Border Style="{ThemeResource FeatureContentPageBorder}" Padding="0">
                    <Grid>
                        <GridView x:Name="gridViewImages" ContainerContentChanging="_gridViewImages_ContainerContentChanging" IsItemClickEnabled="True" DoubleTapped="_gridViewImages_DoubleTapped" ItemsSource="{x:Bind Messages, Converter={StaticResource MessagesToImageContentsConverter}, Mode=OneWay}" SelectionMode="Extended" PreviewKeyDown="_gridViewImages_KeyDown">
                            <GridView.ItemTemplate>
                                <DataTemplate x:DataType="data:IMessageContent">
                                    <Border CornerRadius="10">
                                        <local:MessageContentImageGridItem x:Name="imageGridItem" Width="190" Height="120"/>
                                    </Border>
                                </DataTemplate>
                            </GridView.ItemTemplate>
                            <GridView.ContextFlyout>
                                <MenuFlyout>
                                    <MenuFlyoutItem x:Uid="MenuFlyoutItemSaveToPC" Text="XX Save to PC" Click="_menuFlyoutItemSaveImage_Click">
                                        <MenuFlyoutItem.Icon>
                                            <FontIcon Glyph="&#xE78C;" />
                                        </MenuFlyoutItem.Icon>
                                    </MenuFlyoutItem>
                                    <MenuFlyoutItem x:Uid="MenuFlyoutItemView" Text="XX Preview" Click="_menuFlyoutItemViewImage_Click">
                                        <MenuFlyoutItem.Icon>
                                            <FontIcon Glyph="&#xE890;" />
                                        </MenuFlyoutItem.Icon>
                                    </MenuFlyoutItem>
                                </MenuFlyout>
                            </GridView.ContextFlyout>
                        </GridView>
                        <StackPanel Visibility="{x:Bind Messages, Converter={StaticResource MessagesToHasImagesVisibilityConverter}, ConverterParameter='invert', Mode=OneWay}" VerticalAlignment="Center">
                            <StackPanel Orientation="Vertical" VerticalAlignment="Center" Spacing="5">
                                <TextBlock TextAlignment="Center" Text="{x:Bind Conversation.Title, Mode=OneWay}" Opacity="0.7" Style="{ThemeResource BodyStrongTextBlockStyle}" />
                                <TextBlock x:Uid="TextBlockNoImagesInConversation" Text="XX No images in conversation..." Style="{ThemeResource SubheaderTextBlockStyle}" FontSize="22"  TextAlignment="Center" Visibility="{x:Bind DateFilter, Mode=OneWay, Converter={StaticResource ValueToVisibilityConverter}, ConverterParameter='invert'}" />
                            </StackPanel>
                            <StackPanel VerticalAlignment="Center" Visibility="{x:Bind DateFilter, Mode=OneWay, Converter={StaticResource ValueToVisibilityConverter}}">
                                <TextBlock x:Uid="TextBlockNoImagesInRange" Text="XX No images in date range..." Style="{ThemeResource SubheaderTextBlockStyle}" FontSize="22" TextAlignment="Center"  />
                                <Button x:Uid="ButtonClearImageDateFilter" Style="{StaticResource AccentButtonStyle}" Content="XX Clear Filter" Margin="0,10,0,0" HorizontalAlignment="Center" Click="_buttonClearDateFilter_Click"/>
                            </StackPanel>
                        </StackPanel>
                    </Grid>
                </Border>
            </PivotItem>
        </Pivot>
        <Grid Visibility="{x:Bind IsMessageContentLoading, Converter={StaticResource ValueToVisibilityConverter}, Mode=OneWay}">
            <StackPanel VerticalAlignment="Center" Spacing="20">
                <TextBlock x:Uid="TextBlockLoadingConversation" Text="XX Loading Conversation..." Style="{ThemeResource TitleTextBlockStyle}" TextAlignment="Center" />
                <ProgressRing />
            </StackPanel>
        </Grid>
        <!-- Dialogs and other display over content controls -->
        <commonDialogs:DateFilterContentDialog x:Name="dialogDateFilter" />
        <featurebase:ImagePreviewPage x:Name="imagePreviewPage" Visibility="Collapsed" Padding="30"/>
        <featurebase:ContactsContentPage x:Name="contactsContentsPage" Visibility="Collapsed" IsCloseButtonVisible="True" Background="{ThemeResource AppBarBackgroundThemeBrush}" />
    </Grid>
</Page>
