<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    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"
    xmlns:viewModels="using:ModernKeePass.ViewModels"
    xmlns:converters="using:ModernKeePass.Converters"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity" 
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:actions="using:ModernKeePass.Actions"
    xmlns:controls="using:ModernKeePass.Controls"
    xmlns:templateSelectors="using:ModernKeePass.TemplateSelectors"
    x:Name="PageRoot"
    x:Class="ModernKeePass.Pages.GroupDetailPage"
    mc:Ignorable="d" >
    <Page.Resources>
        <converters:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
        <converters:BooleanToFontStyleConverter x:Key="BooleanToFontStyleConverter"/>
        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <converters:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter"/>
        <converters:NullToBooleanConverter x:Key="NullToBooleanConverter"/>
    </Page.Resources>
    <Page.DataContext>
        <viewModels:GroupVm />
    </Page.DataContext>
    <Page.BottomAppBar>
        <CommandBar x:Name="CommandBar" VerticalAlignment="Center">
            <CommandBar.SecondaryCommands>
                <AppBarButton Icon="Home" x:Uid="AppBarHome">
                    <interactivity:Interaction.Behaviors>
                        <core:EventTriggerBehavior EventName="Click">
                            <core:NavigateToPageAction TargetPage="ModernKeePass.Pages.MainPage" />
                        </core:EventTriggerBehavior>
                    </interactivity:Interaction.Behaviors>
                </AppBarButton>
                <AppBarButton Icon="Save" x:Uid="AppBarSave">
                    <interactivity:Interaction.Behaviors>
                        <core:EventTriggerBehavior EventName="Click">
                            <core:CallMethodAction TargetObject="{Binding}" MethodName="Save"/>
                            <core:ChangePropertyAction TargetObject="{Binding ElementName=CommandBar}" PropertyName="IsOpen" Value="False" />
                        </core:EventTriggerBehavior>
                    </interactivity:Interaction.Behaviors>
                </AppBarButton>
                <AppBarButton Icon="Setting" x:Uid="AppBarSettings">
                    <interactivity:Interaction.Behaviors>
                        <core:EventTriggerBehavior EventName="Click">
                            <core:NavigateToPageAction TargetPage="ModernKeePass.Pages.SettingsPage" />
                        </core:EventTriggerBehavior>
                    </interactivity:Interaction.Behaviors>
                </AppBarButton>
            </CommandBar.SecondaryCommands>
            <AppBarButton Icon="Sort" x:Uid="AppBarSort" Visibility="{Binding IsEditMode, Converter={StaticResource BooleanToVisibilityConverter}}">
                <Button.Flyout>
                    <MenuFlyout>
                        <MenuFlyoutItem x:Uid="AppBarSortEntries">
                            <interactivity:Interaction.Behaviors>
                                <core:EventTriggerBehavior EventName="Click">
                                    <core:CallMethodAction MethodName="SortEntries" TargetObject="{Binding}" />
                                </core:EventTriggerBehavior>
                            </interactivity:Interaction.Behaviors>
                        </MenuFlyoutItem>
                        <MenuFlyoutItem x:Uid="AppBarSortGroups">
                            <interactivity:Interaction.Behaviors>
                                <core:EventTriggerBehavior EventName="Click">
                                    <core:CallMethodAction MethodName="SortGroups" TargetObject="{Binding}" />
                                </core:EventTriggerBehavior>
                            </interactivity:Interaction.Behaviors>
                        </MenuFlyoutItem>
                    </MenuFlyout>
                </Button.Flyout>
            </AppBarButton>
            <AppBarToggleButton Icon="Edit" x:Uid="AppBarEdit" IsChecked="{Binding IsEditMode, Mode=TwoWay}">
                <interactivity:Interaction.Behaviors>
                    <core:EventTriggerBehavior EventName="Click">
                        <core:ChangePropertyAction TargetObject="{Binding ElementName=CommandBar}" PropertyName="IsOpen" Value="False" />
                    </core:EventTriggerBehavior>
                </interactivity:Interaction.Behaviors>
            </AppBarToggleButton>
            <AppBarButton Icon="Undo" x:Uid="AppBarRestore" Visibility="{Binding ShowRestore, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding PreviousGroup, Converter={StaticResource NullToBooleanConverter}}" Click="RestoreButton_Click">
                <interactivity:Interaction.Behaviors>
                    <core:EventTriggerBehavior EventName="Click">
                        <core:CallMethodAction MethodName="UndoDelete" TargetObject="{Binding}" />
                    </core:EventTriggerBehavior>
                </interactivity:Interaction.Behaviors>
            </AppBarButton>
            <AppBarButton Icon="Delete" x:Uid="AppBarDelete" IsEnabled="{Binding IsNotRoot}" Click="DeleteButton_Click" />
        </CommandBar>
    </Page.BottomAppBar>
    <Grid>
        <Grid.Resources>
            <CollectionViewSource
                x:Name="GroupsViewSource"
                Source="{Binding Groups}" />
            <CollectionViewSource
                x:Name="EntriesViewSource"
                Source="{Binding EntriesFiltered}" />
            <CollectionViewSource
                x:Name="EntriesZoomedOutViewSource"
                Source="{Binding EntriesZoomedOut}" IsSourceGrouped="True"/>
        </Grid.Resources>
        <Grid.Background>
            <StaticResource ResourceKey="ApplicationPageBackgroundThemeBrush"/>
        </Grid.Background>
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50" x:Name="LeftListViewColumn" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <ListView 
                Grid.Column="0"
                x:Name="LeftListView"
                Margin="0,0,0,-10"
                SelectionChanged="groups_SelectionChanged"
                IsSwipeEnabled="false"
                IsSynchronizedWithCurrentItem="False"
                DataContext="{Binding DataContext, ElementName=PageRoot}" 
                RequestedTheme="Dark"
                Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
                Foreground="{ThemeResource DefaultTextForegroundThemeBrush}">
                <ListView.Resources>
                    <DataTemplate x:Name="GroupOtherItem">
                        <StackPanel Orientation="Horizontal">
                            <SymbolIcon Symbol="{Binding IconSymbol}" Margin="8,0,0,0">
                                <ToolTipService.ToolTip>
                                    <ToolTip Content="{Binding Name}" />
                                </ToolTipService.ToolTip>
                             </SymbolIcon>
                            <TextBlock Text="{Binding Name}" x:Name="GroupTextBlock" TextWrapping="NoWrap" VerticalAlignment="Center" Margin="30,0,20,0" FontStyle="{Binding IsSelected, Converter={StaticResource BooleanToFontStyleConverter}}" />
                        </StackPanel>
                    </DataTemplate>
                    <DataTemplate x:Name="GroupFirstItem">
                        <StackPanel Orientation="Horizontal">
                            <SymbolIcon Symbol="{Binding IconSymbol}" Margin="8,0,0,0">
                                <ToolTipService.ToolTip>
                                    <ToolTip x:Uid="GroupNewItemTooltip" />
                                </ToolTipService.ToolTip>
                            </SymbolIcon>
                            <TextBlock x:Name="GroupTextBlock" x:Uid="GroupNewItemTextBox" FontWeight="SemiBold" TextWrapping="NoWrap" VerticalAlignment="Center" Margin="30,0,20,0" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.Resources>
                <ListView.ItemsSource>
                    <Binding Source="{StaticResource GroupsViewSource}"/>
                </ListView.ItemsSource>
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="VerticalContentAlignment" Value="Stretch" />
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="Margin" Value="0"/>
                        <Setter Property="AllowDrop" Value="True"/>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.HeaderTemplate>
                    <DataTemplate>
                        <ToggleButton Style="{StaticResource HamburgerToggleButton}">
                            <interactivity:Interaction.Behaviors>
                                <core:EventTriggerBehavior EventName="Checked">
                                    <core:ChangePropertyAction PropertyName="Width" Value="Auto" TargetObject="{Binding ElementName=LeftListViewColumn}"/>
                                </core:EventTriggerBehavior>
                                <core:EventTriggerBehavior EventName="Unchecked">
                                    <core:ChangePropertyAction PropertyName="Width" Value="50" TargetObject="{Binding ElementName=LeftListViewColumn}"/>
                                </core:EventTriggerBehavior>
                            </interactivity:Interaction.Behaviors>
                        </ToggleButton>
                    </DataTemplate>
                </ListView.HeaderTemplate>
                <ListView.ItemTemplateSelector>
                    <templateSelectors:FirstItemDataTemplateSelector 
                        FirstItem="{StaticResource GroupFirstItem}" 
                        OtherItem="{StaticResource GroupOtherItem}" />
                </ListView.ItemTemplateSelector>
            </ListView>
            <!-- Horizontal scrolling grid -->
            <HyperlinkButton Grid.Column="1" VerticalAlignment="Top" Margin="40,0,0,0" Click="CreateEntry_ButtonClick" Visibility="{Binding IsSelected, Converter={StaticResource InverseBooleanToVisibilityConverter}}" >
                <StackPanel Orientation="Horizontal">
                    <SymbolIcon Symbol="Add"></SymbolIcon>
                    <TextBlock x:Uid="GroupCreateEntry" VerticalAlignment="Center" Margin="10,0,0,0" />
                </StackPanel>
            </HyperlinkButton>
            <SemanticZoom Grid.Column="1" ViewChangeStarted="SemanticZoom_ViewChangeStarted" Margin="0,45,0,0">
                <SemanticZoom.ZoomedInView>
                    <GridView
                        x:Name="GridView"
                        AutomationProperties.AutomationId="ItemGridView"
                        AutomationProperties.Name="Entries"
                        TabIndex="1"
                        SelectionChanged="entries_SelectionChanged"
                        IsSynchronizedWithCurrentItem="False" 
                        BorderBrush="{StaticResource ListViewItemSelectedBackgroundThemeBrush}"
                        AllowDrop="True"
                        CanReorderItems="True" 
                        CanDragItems="True"
                        DragItemsStarting="GridView_DragItemsStarting">
                        <interactivity:Interaction.Behaviors>
                            <core:DataTriggerBehavior Binding="{Binding IsEditMode}" Value="False">
                                <actions:SetupFocusAction TargetObject="{Binding ElementName=GridView}" />
                            </core:DataTriggerBehavior>
                        </interactivity:Interaction.Behaviors>
                        <GridView.ItemTemplate>
                            <DataTemplate>
                                <Grid Height="110" Width="480" x:Name="EntryGrid" Background="{Binding BackgroundColor, ConverterParameter={StaticResource AppBarBackgroundThemeBrush}, Converter={StaticResource ColorToBrushConverter}}">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <SymbolIcon Grid.Column="0" Symbol="{Binding IconSymbol}" Width="100" Height="100" RenderTransformOrigin="0.5,0.5" >
                                        <SymbolIcon.RenderTransform>
                                            <CompositeTransform ScaleX="2" TranslateX="0" TranslateY="0" ScaleY="2"/>
                                        </SymbolIcon.RenderTransform>
                                    </SymbolIcon>
                                    <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,10,0,0" >
                                        <TextBlock x:Name="NameTextBlock" Text="{Binding Name}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" Foreground="{Binding ForegroundColor, ConverterParameter={StaticResource TextBoxForegroundThemeBrush}, Converter={StaticResource ColorToBrushConverter}}"/>
                                        <TextBlock Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap"/>
                                        <TextBlock Text="{Binding UserName}" Style="{StaticResource BodyTextBlockStyle}" MaxHeight="60" />
                                        <TextBlock Text="{Binding Url}" Style="{StaticResource BodyTextBlockStyle}" MaxHeight="60" />
                                    </StackPanel>
                                </Grid>
                            </DataTemplate>
                        </GridView.ItemTemplate>
                        <GridView.ItemsSource>
                            <Binding Source="{StaticResource EntriesViewSource}"/>
                        </GridView.ItemsSource>
                        <GridView.DataContext>
                            <viewModels:EntryVm/>
                        </GridView.DataContext>
                        <GridView.ItemContainerStyle>
                            <Style TargetType = "GridViewItem" >
                                <Setter Property = "Template" >
                                    <Setter.Value>
                                        <ControlTemplate TargetType = "GridViewItem" >
                                            <GridViewItemPresenter
                                                SelectionCheckMarkVisualEnabled="True"
                                                CheckHintBrush="{ThemeResource ListViewItemCheckHintThemeBrush}"
                                                CheckSelectingBrush="{ThemeResource ListViewItemCheckSelectingThemeBrush}"
                                                CheckBrush="{ThemeResource ListViewItemCheckThemeBrush}"
                                                DragBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
                                                DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
                                                FocusBorderBrush="{ThemeResource ListViewItemFocusBorderThemeBrush}"
                                                PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
                                                PointerOverBackground="{ThemeResource ListViewItemPointerOverBackgroundThemeBrush}"
                                                SelectedBorderThickness="{ThemeResource GridViewItemCompactSelectedBorderThemeThickness}"
                                                SelectedBackground="{ThemeResource ListViewItemSelectedBackgroundThemeBrush}"
                                                SelectedForeground="{ThemeResource ListViewItemSelectedForegroundThemeBrush}"
                                                SelectedPointerOverBackground="{ThemeResource ListViewItemSelectedPointerOverBackgroundThemeBrush}"
                                                SelectedPointerOverBorderBrush="{ThemeResource ListViewItemSelectedPointerOverBorderThemeBrush}"
                                                DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                                                DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
                                                ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
                                                PointerOverBackgroundMargin="1"
                                                ContentMargin="4" />
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GridView.ItemContainerStyle>
                    </GridView>
                </SemanticZoom.ZoomedInView>

                <SemanticZoom.ZoomedOutView>
                    <ListView
                        ItemsSource="{Binding Source={StaticResource EntriesZoomedOutViewSource}}"
                        IsSwipeEnabled="false" 
                        SelectionChanged="groups_SelectionChanged"
                        IsSynchronizedWithCurrentItem="False">
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="{Binding Name}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                        <ListView.GroupStyle>
                            <GroupStyle HidesIfEmpty="True">
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <Grid Background="LightGray" Margin="0,0,10,0" HorizontalAlignment="Left">
                                            <TextBlock Text="{Binding Key}" Width="40" Foreground="Black" Margin="30" Style="{StaticResource HeaderTextBlockStyle}"/>
                                        </Grid>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>
                            </GroupStyle>
                        </ListView.GroupStyle>
                    </ListView>
                </SemanticZoom.ZoomedOutView>
            </SemanticZoom>
        </Grid>
        <!-- Back button and page title -->
        <Grid Grid.Row="0" Background="{ThemeResource AppBarBackgroundThemeBrush}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="400"/>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" 
                    Command="{Binding NavigationHelper.GoBackCommand, ElementName=PageRoot}"
                    Height="50"
                    Width="50"
                    AutomationProperties.Name="Back"
                    AutomationProperties.AutomationId="BackButton"
                    AutomationProperties.ItemType="Navigation Button"
                    Style="{StaticResource NoBorderButtonStyle}">
                <SymbolIcon Symbol="Back" />
            </Button>
            <StackPanel Grid.Column="1" >
                <TextBox
                    x:Name="TitleTextBox"
                    Text="{Binding Name, Mode=TwoWay}" 
                    Foreground="{ThemeResource DefaultTextForegroundThemeBrush}"
                    Background="Transparent"
                    IsHitTestVisible="{Binding IsEditMode}" 
                    BorderThickness="0" 
                    FontSize="20"
                    FontWeight="SemiBold"
                    TextWrapping="NoWrap" 
                    VerticalAlignment="Center" 
                    x:Uid="GroupTitle">
                    <interactivity:Interaction.Behaviors>
                        <core:DataTriggerBehavior Binding="{Binding IsEditMode}" Value="True">
                            <actions:SetupFocusAction TargetObject="{Binding ElementName=TitleTextBox}" />
                        </core:DataTriggerBehavior>
                    </interactivity:Interaction.Behaviors>
                </TextBox>
                <TextBlock FontSize="12" Text="{Binding Path}" />
            </StackPanel>
            <controls:TextBoxWithButton Grid.Column="2" x:Uid="GroupSearch" ButtonSymbol="&#xE11A;" Text="{Binding Filter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="18" VerticalContentAlignment="Center" Style="{StaticResource TextBoxWithButtonStyle}" />
        </Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="DragDropGroup">
                <VisualState x:Name="Dragging">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="GridView" Storyboard.TargetProperty="BorderThickness">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Dropped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="GridView" Storyboard.TargetProperty="BorderThickness">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</Page>