<?xml version="1.0" encoding="utf-8" ?>
<pages:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Bit.App.Pages.GroupingsPage"
    xmlns:pages="clr-namespace:Bit.App.Pages"
    xmlns:u="clr-namespace:Bit.App.Utilities"
    xmlns:effects="clr-namespace:Bit.App.Effects"
    xmlns:controls="clr-namespace:Bit.App.Controls"
    xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
    xmlns:core="clr-namespace:Bit.Core;assembly=BitwardenCore"
    x:DataType="pages:GroupingsPageViewModel"
    Title="{Binding PageTitle}"
    x:Name="_page">

    <ContentPage.BindingContext>
        <pages:GroupingsPageViewModel />
    </ContentPage.BindingContext>

    <ContentPage.ToolbarItems>
        <controls:ExtendedToolbarItem
            x:Name="_accountAvatar"
            IconImageSource="{Binding AvatarImageSource}"
            Command="{Binding Source={x:Reference _accountListOverlay}, Path=ToggleVisibililtyCommand}"
            Order="Primary"
            Priority="-1"
            UseOriginalImage="True"
            AutomationProperties.IsInAccessibleTree="True"
            AutomationProperties.Name="{u:I18n Account}" />
        <ToolbarItem Icon="search.png" Clicked="Search_Clicked"
                     AutomationProperties.IsInAccessibleTree="True"
                     AutomationProperties.Name="{u:I18n Search}" />
    </ContentPage.ToolbarItems>

    <ContentPage.Resources>
        <ResourceDictionary>
            <u:InverseBoolConverter x:Key="inverseBool" />

            <ToolbarItem x:Name="_syncItem" x:Key="syncItem" Text="{u:I18n Sync}"
                         Clicked="Sync_Clicked" Order="Secondary" />
            <ToolbarItem x:Name="_lockItem" x:Key="lockItem" Text="{u:I18n Lock}"
                         Clicked="Lock_Clicked" Order="Secondary" />
            <ToolbarItem x:Name="_exitItem" x:Key="exitItem" Text="{u:I18n Exit}"
                         Clicked="Exit_Clicked" Order="Secondary" />
            <ToolbarItem x:Name="_addItem" x:Key="addItem" Icon="plus.png"
                         Clicked="AddButton_Clicked" Order="Primary"
                         AutomationProperties.IsInAccessibleTree="True"
                         AutomationProperties.Name="{u:I18n AddItem}" />

            <DataTemplate x:Key="cipherTemplate"
                          x:DataType="pages:GroupingsPageListItem">
                <controls:CipherViewCell
                    Cipher="{Binding Cipher}"
                    ButtonCommand="{Binding BindingContext.CipherOptionsCommand, Source={x:Reference _page}}"
                    WebsiteIconsEnabled="{Binding BindingContext.WebsiteIconsEnabled, Source={x:Reference _page}}" />
            </DataTemplate>

            <DataTemplate x:Key="authenticatorTemplate"
                          x:DataType="pages:GroupingsPageTOTPListItem">
                <controls:AuthenticatorViewCell 
                    Cipher="{Binding Cipher}"
                    WebsiteIconsEnabled="{Binding BindingContext.WebsiteIconsEnabled, Source={x:Reference _page}}"
                    TotpSec="{Binding TotpSec}"/>
            </DataTemplate>

            <DataTemplate x:Key="groupTemplate"
                          x:DataType="pages:GroupingsPageListItem">
                <controls:ExtendedStackLayout Orientation="Horizontal" 
                                                StyleClass="list-row, list-row-platform">
                    <controls:IconLabel Text="{Binding Icon, Mode=OneWay}"
                                        HorizontalOptions="Start"
                                        VerticalOptions="Center"
                                        StyleClass="list-icon, list-icon-platform"
                                        ShouldUpdateFontSizeDynamicallyForAccesibility="True">
                        <controls:IconLabel.Effects>
                            <effects:FixedSizeEffect />
                        </controls:IconLabel.Effects>
                    </controls:IconLabel>
                    <Label Text="{Binding Name, Mode=OneWay}"
                            LineBreakMode="TailTruncation"
                            HorizontalOptions="FillAndExpand"
                            VerticalOptions="CenterAndExpand"
                            StyleClass="list-title"/>
                    <Label Text="{Binding ItemCount, Mode=OneWay}"
                            HorizontalOptions="End"
                            VerticalOptions="CenterAndExpand"
                            HorizontalTextAlignment="End"
                            StyleClass="list-sub"/>
                </controls:ExtendedStackLayout>
            </DataTemplate>

            <DataTemplate
                x:Key="headerTemplate"
                x:DataType="pages:GroupingsPageHeaderListItem">
                <StackLayout
                    Spacing="0"
                    Padding="0"
                    VerticalOptions="FillAndExpand"
                    StyleClass="list-row-header-container, list-row-header-container-platform">
                    <BoxView
                        StyleClass="list-section-separator-top, list-section-separator-top-platform" />
                    <StackLayout StyleClass="list-row-header, list-row-header-platform">
                        <Label
                            Text="{Binding Title}"
                            StyleClass="list-header, list-header-platform" />
                        <Label
                            Text="{Binding ItemCount}"
                            StyleClass="list-header-sub" />
                    </StackLayout>
                    <BoxView StyleClass="list-section-separator-bottom, list-section-separator-bottom-platform" />
                </StackLayout>
	        </DataTemplate>

            <pages:GroupingsPageListItemSelector x:Key="listItemDataTemplateSelector"
                                                 HeaderTemplate="{StaticResource headerTemplate}"
                                                 CipherTemplate="{StaticResource cipherTemplate}"
                                                 AuthenticatorTemplate="{StaticResource authenticatorTemplate}"
                                                 GroupTemplate="{StaticResource groupTemplate}" />

            <StackLayout x:Key="mainLayout" x:Name="_mainLayout">
                <StackLayout
                    IsVisible="{Binding ShowVaultFilter}"
                    Orientation="Horizontal"
                    HorizontalOptions="FillAndExpand"
                    Margin="0,5,0,0">
                    <Label
                        Text="{Binding VaultFilterDescription}"
                        LineBreakMode="TailTruncation"
                        Margin="10,0"
                        StyleClass="text-md, text-muted"
                        VerticalOptions="Center"
                        HorizontalOptions="StartAndExpand"
                        AutomationProperties.IsInAccessibleTree="True"
                        AutomationProperties.Name="{u:I18n Filter}" />
                    <controls:MiButton
                        Text="{Binding Source={x:Static core:BitwardenIcons.ViewCellMenu}}"
                        StyleClass="list-row-button-text, list-row-button-platform"
                        Command="{Binding VaultFilterCommand}"
                        VerticalOptions="Center"
                        HorizontalOptions="End"
                        AutomationProperties.IsInAccessibleTree="True"
                        AutomationProperties.Name="{u:I18n Filter}" />
                </StackLayout>
                <StackLayout
                    VerticalOptions="CenterAndExpand"
                    Padding="20, 0"
                    Spacing="20"
                    IsVisible="{Binding ShowNoData}">
                    <Label
                        Text="{Binding NoDataText}"
                        HorizontalTextAlignment="Center"></Label>
                    <Button
                        Text="{u:I18n AddAnItem}"
                        Clicked="AddButton_Clicked"
                        IsVisible="{Binding ShowAddCipherButton}"></Button>
                </StackLayout>

                <RefreshView
                    IsVisible="{Binding ShowList}"
                    IsRefreshing="{Binding Refreshing}"
                    Command="{Binding RefreshCommand}">
                    <controls:ExtendedCollectionView
                        ItemsSource="{Binding GroupedItems}"
                        VerticalOptions="FillAndExpand"
                        ItemTemplate="{StaticResource listItemDataTemplateSelector}"
                        SelectionMode="Single"
                        SelectionChanged="RowSelected"
                        StyleClass="list, list-platform"
                        ExtraDataForLogging="Groupings Page" />
                </RefreshView>
            </StackLayout>
        </ResourceDictionary>
    </ContentPage.Resources>

    <AbsoluteLayout
        x:Name="_absLayout"
        VerticalOptions="FillAndExpand"
        HorizontalOptions="FillAndExpand">
        <ContentView
            x:Name="_mainContent"
            AbsoluteLayout.LayoutFlags="All"
            AbsoluteLayout.LayoutBounds="0, 0, 1, 1">
        </ContentView>
        
        <!-- Android FAB -->
        <Button
            x:Name="_fab"
            Image="plus.png"
            Clicked="AddButton_Clicked"
            Style="{StaticResource btn-fab}"
            AbsoluteLayout.LayoutFlags="PositionProportional"
            AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize"
            AutomationProperties.IsInAccessibleTree="True"
            AutomationProperties.Name="{u:I18n AddItem}">
            <Button.Effects>
                <effects:FabShadowEffect />
            </Button.Effects>
        </Button>

        <controls:AccountSwitchingOverlayView
            x:Name="_accountListOverlay"
            AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
            AbsoluteLayout.LayoutFlags="All"
            MainPage="{Binding Source={x:Reference _page}}"
            MainFab="{Binding Source={x:Reference _fab}, Path=.}"
            BindingContext="{Binding AccountSwitchingOverlayViewModel}"/>
    </AbsoluteLayout>

</pages:BaseContentPage>
