<?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.SettingsPage"
             xmlns:pages="clr-namespace:Bit.App.Pages"
             xmlns:controls="clr-namespace:Bit.App.Controls"
             xmlns:u="clr-namespace:Bit.App.Utilities"
             x:DataType="pages:SettingsPageViewModel"
             Title="{Binding PageTitle}">
    <ContentPage.BindingContext>
        <pages:SettingsPageViewModel />
    </ContentPage.BindingContext>

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

            <DataTemplate
                x:Key="regularTemplate"
                x:DataType="pages:SettingsPageListItem">
                <controls:ExtendedStackLayout Orientation="Horizontal"
                             StyleClass="list-row, list-row-platform">
                    <Frame
                        IsVisible="{Binding UseFrame}"
                        Padding="10"
                        HasShadow="False"
                        BackgroundColor="Transparent"
                        BorderColor="{DynamicResource PrimaryColor}">
                        <Label
                            Text="{Binding Name, Mode=OneWay}"
                            StyleClass="text-muted, text-sm, text-bold"
                            HorizontalTextAlignment="Center" />
                    </Frame>
                    <Label IsVisible="{Binding UseFrame, Converter={StaticResource inverseBool}}"
                           Text="{Binding Name, Mode=OneWay}"
                           LineBreakMode="{Binding LineBreakMode}"
                           HorizontalOptions="StartAndExpand"
                           VerticalOptions="CenterAndExpand"
                           StyleClass="list-title"/>
                    <Label Text="{Binding SubLabel, Mode=OneWay}"
                           IsVisible="{Binding ShowSubLabel}"
                           HorizontalOptions="End"
                           HorizontalTextAlignment="End"
                           VerticalOptions="CenterAndExpand"
                           TextColor="{Binding SubLabelColor}"
                           StyleClass="list-sub" />
                </controls:ExtendedStackLayout>
            </DataTemplate>
            <DataTemplate
                x:Key="timePickerTemplate"
                x:DataType="pages:SettingsPageListItem">
                <controls:ExtendedStackLayout Orientation="Horizontal"
                             StyleClass="list-row, list-row-platform">
                    <Frame
                        IsVisible="{Binding UseFrame}"
                        Padding="10"
                        HasShadow="False"
                        BackgroundColor="Transparent"
                        BorderColor="{DynamicResource PrimaryColor}">
                        <Label
                            Text="{Binding Name, Mode=OneWay}"
                            StyleClass="text-muted, text-sm, text-bold"
                            HorizontalTextAlignment="Center" />
                    </Frame>
                    <Label IsVisible="{Binding UseFrame, Converter={StaticResource inverseBool}}"
                           Text="{Binding Name, Mode=OneWay}"
                           LineBreakMode="{Binding LineBreakMode}"
                           HorizontalOptions="StartAndExpand"
                           VerticalOptions="CenterAndExpand"
                           StyleClass="list-title"/>
                    <TimePicker Time="{Binding Time}" Format="HH:mm"
                                PropertyChanged="OnTimePickerPropertyChanged"
                                HorizontalOptions="End"
                                VerticalOptions="Center"
                                FontSize="Small"
                                TextColor="{Binding SubLabelColor}"
                                StyleClass="list-sub" Margin="-5"/>
                    <controls:ExtendedStackLayout.GestureRecognizers>
			            <TapGestureRecognizer Tapped="ActivateTimePicker"/>
		            </controls:ExtendedStackLayout.GestureRecognizers>
                </controls:ExtendedStackLayout>
	        </DataTemplate>

            <DataTemplate
                x:Key="headerTemplate"
                x:DataType="pages:SettingsPageHeaderListItem">
                <StackLayout
                    Padding="0" Spacing="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" />
                    </StackLayout>
                    <BoxView StyleClass="list-section-separator-bottom, list-section-separator-bottom-platform" />
                </StackLayout>
	        </DataTemplate>

            <pages:SettingsPageListItemSelector
                x:Key="listItemDataTemplateSelector"
                HeaderTemplate="{StaticResource headerTemplate}"
                RegularTemplate="{StaticResource regularTemplate}"
                TimePickerTemplate="{StaticResource timePickerTemplate}" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <controls:ExtendedCollectionView
        ItemsSource="{Binding GroupedItems}"
        VerticalOptions="FillAndExpand"
        ItemTemplate="{StaticResource listItemDataTemplateSelector}"
        SelectionMode="Single"
        SelectionChanged="RowSelected"
        StyleClass="list, list-platform"
        ExtraDataForLogging="Settings Page" />

</pages:BaseContentPage>
