<?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.TwoFactorPage"
    xmlns:pages="clr-namespace:Bit.App.Pages"
    xmlns:controls="clr-namespace:Bit.App.Controls"
    xmlns:u="clr-namespace:Bit.App.Utilities"
    x:DataType="pages:TwoFactorPageViewModel"
    HideSoftInputOnTapped="True"
    Unloaded="TwoFactorPage_OnUnloaded"
    Title="{Binding PageTitle}">

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

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="{u:I18n Cancel}" Clicked="Close_Clicked" Order="Primary" Priority="-1"
                     x:Name="_cancelItem" />
    </ContentPage.ToolbarItems>
            
    <ContentPage.Resources>
        <ResourceDictionary>
            <u:InverseBoolConverter x:Key="inverseBool" />
            <u:IsNullConverter x:Key="isNull" />
            <ToolbarItem 
                x:Name="_moreItem"
                x:Key="moreItem"
                IconImageSource="more_vert.png"
                Order="Primary"
                Command="{Binding MoreCommand}"
                SemanticProperties.Description="{u:I18n Options}" />
            <ToolbarItem Text="{u:I18n UseAnotherTwoStepMethod}" 
                Clicked="Methods_Clicked" 
                Order="Secondary"
                x:Name="_useAnotherTwoStepMethod" 
                x:Key="useAnotherTwoStepMethod" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ScrollView x:Name="_scrollView">
        <StackLayout Spacing="10" Padding="0, 0, 0, 10" VerticalOptions="FillAndExpand">
            <StackLayout Spacing="20" Padding="0" IsVisible="{Binding TotpMethod, Mode=OneWay}">
                <Label
                    Text="{Binding TotpInstruction, Mode=OneWay}"
                    Margin="10, 20, 10, 0"
                    HorizontalTextAlignment="Center" />
                <StackLayout StyleClass="box">
                    <StackLayout StyleClass="box-row, box-row-input">
                        <Label
                            Text="{u:I18n VerificationCode}"
                            StyleClass="box-label" />
                        <Entry
                            Text="{Binding Token}"
                            x:Name="_totpEntry"
                            Keyboard="Numeric"
                            StyleClass="box-value"
                            ReturnType="Go"
                            ReturnCommand="{Binding SubmitCommand}"
                            TextChanged="Token_TextChanged"/>
                    </StackLayout>
                    <StackLayout StyleClass="box-row, box-row-switch">
                        <Label
                            Text="{u:I18n RememberMe}"
                            StyleClass="box-label-regular"
                            HorizontalOptions="StartAndExpand" />
                        <Switch
                            IsToggled="{Binding Remember}"
                            StyleClass="box-value"
                            HorizontalOptions="End" />
                    </StackLayout>
                </StackLayout>
            </StackLayout>
            <StackLayout Spacing="20" Padding="0" IsVisible="{Binding YubikeyMethod, Mode=OneWay}">
                <Label
                    Text="{Binding YubikeyInstruction, Mode=OneWay}"
                    Margin="10, 20, 10, 0"
                    HorizontalTextAlignment="Center" />
                <Image
                    Source="yubikey.png"
                    Margin="10, 0"
                    WidthRequest="266"
                    HeightRequest="160"
                    HorizontalOptions="Center" />
                <StackLayout StyleClass="box">
                    <StackLayout StyleClass="box-row, box-row-input">
                        <Entry
                            x:Name="_yubikeyTokenEntry"
                            Text="{Binding Token}"
                            StyleClass="box-value"
                            IsPassword="True"
                            IsSpellCheckEnabled="False"
                            IsTextPredictionEnabled="False"
                            ReturnType="Go"
                            ReturnCommand="{Binding SubmitCommand}"
                            TextChanged="Token_TextChanged" />
                    </StackLayout>
                    <StackLayout StyleClass="box-row, box-row-switch">
                        <Label
                            Text="{u:I18n RememberMe}"
                            StyleClass="box-label-regular"
                            HorizontalOptions="StartAndExpand" />
                        <Switch
                            IsToggled="{Binding Remember}"
                            StyleClass="box-value"
                            HorizontalOptions="End" />
                    </StackLayout>
                </StackLayout>
            </StackLayout>
            <StackLayout Spacing="20" Padding="0" IsVisible="{Binding Fido2Method, Mode=OneWay}">
                <Label
                    Text="{u:I18n Fido2Instruction}"
                    Margin="10, 20, 10, 0"
                    HorizontalTextAlignment="Center" />
                <Image
                    Source="yubikey.png"
                    Margin="10, 0"
                    WidthRequest="266"
                    HeightRequest="160"
                    HorizontalOptions="Center" />
                <StackLayout StyleClass="box">
                    <StackLayout StyleClass="box-row, box-row-switch">
                        <Label
                            Text="{u:I18n RememberMe}"
                            StyleClass="box-label-regular"
                            HorizontalOptions="StartAndExpand" />
                        <Switch
                            IsToggled="{Binding Remember}"
                            StyleClass="box-value"
                            HorizontalOptions="End" />
                    </StackLayout>
                </StackLayout>
            </StackLayout>
            <StackLayout
                Spacing="0"
                Padding="0"
                IsVisible="{Binding DuoMethod, Mode=OneWay}"
                VerticalOptions="FillAndExpand">
                <Label
                    StyleClass="box"
                    Text="{Binding DuoFramelessLabel}"
                    HorizontalOptions="StartAndExpand" 
                    Margin="10,21"
                    IsVisible="{Binding IsDuoFrameless}"/>
                <controls:HybridWebView
                    x:Name="_duoWebView"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand"
                    HeightRequest="{Binding DuoWebViewHeight, Mode=OneWay}"
                    IsVisible="{Binding IsDuoFrameless, Converter={StaticResource inverseBool}}"/>
                <StackLayout
                    StyleClass="box"
                    VerticalOptions="End">
                    <StackLayout StyleClass="box-row, box-row-switch">
                        <Label
                            Text="{u:I18n RememberMe}"
                            StyleClass="box-label-regular"
                            HorizontalOptions="StartAndExpand" />
                        <Switch
                            IsToggled="{Binding Remember}"
                            StyleClass="box-value"
                            HorizontalOptions="End" />
                    </StackLayout>
                </StackLayout>
                <Button Text="{u:I18n LaunchDuo}"
                    Margin="10,21"
                    StyleClass="btn-primary"
                    Command="{Binding AuthenticateWithDuoFramelessCommand}"
                    AutomationId="DuoFramelessButton"
                    IsVisible="{Binding IsDuoFrameless}"/>
            </StackLayout>
            <StackLayout
                Spacing="0"
                Padding="0"
                IsVisible="{Binding SelectedProviderType, Mode=OneWay, Converter={StaticResource isNull}}">
                <Label
                    Text="{u:I18n NoTwoStepAvailable}"
                    Margin="10, 20, 10, 10"
                    HorizontalTextAlignment="Center" />
            </StackLayout>
            <Button Text="{u:I18n Continue}"
                    IsEnabled="{Binding EnableContinue}"
                    IsVisible="{Binding ShowContinue}"
                    Clicked="Continue_Clicked"
                    Margin="10, 0"
                    x:Name="_continue"></Button>
            <Button Text="{u:I18n SendVerificationCodeAgain}"
                    IsVisible="{Binding EmailMethod}"
                    Clicked="ResendEmail_Clicked"
                    Margin="10, 0"></Button>
            <Button Text="{u:I18n TryAgain}"
                    IsVisible="{Binding ShowTryAgain}"
                    Clicked="TryAgain_Clicked"
                    Margin="10, 0"></Button>
        </StackLayout>
    </ScrollView>

</pages:BaseContentPage>
