<?xml version="1.0" encoding="utf-8" ?>
<pages:BaseContentPage 
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Bit.App.Pages.HomePage"
    xmlns:pages="clr-namespace:Bit.App.Pages"
    xmlns:controls="clr-namespace:Bit.App.Controls"
    xmlns:u="clr-namespace:Bit.App.Utilities"
    x:DataType="pages:HomeViewModel"
    HideSoftInputOnTapped="True"
    x:Name="_page"
    Loaded="HomePage_Loaded"
    Title="{Binding PageTitle}">
    <ContentPage.BindingContext>
        <pages:HomeViewModel />
    </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"
            SemanticProperties.Description="{u:I18n Account}"
            AutomationId="AccountIconButton" />
        <ToolbarItem x:Name="_closeButton" Text="{u:I18n Close}" Command="{Binding CloseCommand}" Order="Primary" Priority="-1"/>
    </ContentPage.ToolbarItems>

    <ContentPage.Resources>
        <ResourceDictionary>
            <u:InverseBoolConverter x:Key="inverseBool" />
            <ScrollView x:Name="_mainLayout" x:Key="mainLayout">
                <StackLayout Spacing="30" Padding="20, 50, 20, 0">
                    <Image
                        x:Name="_logo"
                        Source="logo.png"
                        HeightRequest="60"
                        VerticalOptions="Center" />
                    <Label Text="{u:I18n LoginOrCreateNewAccount}"
                            StyleClass="text-lg"
                            HorizontalTextAlignment="Center"/>
                     <StackLayout
                        StyleClass="box-row">
                        <Label
                            Text="{u:I18n EmailAddress}"
                            StyleClass="box-label" />
                        <Entry
                            x:Name="_email"
                            Text="{Binding Email}"
                            Keyboard="Email"
                            StyleClass="box-value, no-keyboard-auto-help"
                            ReturnType="Go"
                            ReturnCommand="{Binding ContinueCommand}"
                            AutomationId="EmailAddressEntry">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Disabled">
                                        <VisualState.Setters>
                                            <Setter Property="TextColor" Value="{DynamicResource MutedColor}" />
                                        </VisualState.Setters>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Entry>
                        <StackLayout
                            Orientation="Horizontal"
                            Margin="0, 6, 0 ,0">
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer
                                    Command="{Binding ShowEnvironmentPickerCommand}" />
                            </StackLayout.GestureRecognizers>
                            <Label
                                Margin="{OnPlatform Android='0,0,6,1', iOS='0,0,6,0'}"
                                Text="{Binding RegionText}"
                                FontSize="13"
                                TextColor="{DynamicResource MutedColor}"
                                VerticalOptions="Center"
                                VerticalTextAlignment="Center"/>
                            <controls:IconLabel
                                Text="{Binding SelectedEnvironmentName}"
                                FontSize="13"
                                TextColor="{DynamicResource PrimaryColor}"
                                VerticalOptions="Center"
                                VerticalTextAlignment="Center"
                                AutomationId="RegionSelectorDropdown"/>
                        </StackLayout>
                        <StackLayout
                            Orientation="Horizontal"
                            Margin="0, 20, 0 ,0">
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer
                                    Command="{Binding RememberEmailCommand}" />
                            </StackLayout.GestureRecognizers>
                            <Label
                                Text="{u:I18n RememberMe}"
                                StyleClass="text-sm"
                                HorizontalOptions="FillAndExpand"
                                VerticalOptions="Center"
                                VerticalTextAlignment="Center"
                                />
                            <Switch
                                Scale="0.8"
                                IsToggled="{Binding RememberEmail}"
                                VerticalOptions="Center"
                                AutomationId="RememberMeSwitch"
                                />
                        </StackLayout>
                    </StackLayout>
                    <Button Text="{u:I18n Continue}"
                            StyleClass="btn-primary"
                            IsEnabled="{Binding CanContinue}"
                            Command="{Binding ContinueCommand}"
                            AutomationId="ContinueButton"
                            />
                            
                    <Label FormattedText="{Binding CreateAccountText}"
                           Margin="0, 10"
                            StyleClass="box-footer-label"
                            AutomationId="CreateAccountLabel">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding CreateAccountCommand}" />
                        </Label.GestureRecognizers>
                    </Label> 
                </StackLayout>
            </ScrollView>
        </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>

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