<?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.ScanPage"
    xmlns:pages="clr-namespace:Bit.App.Pages"
    xmlns:u="clr-namespace:Bit.App.Utilities"
    xmlns:controls="clr-namespace:Bit.App.Controls"
    xmlns:skia="clr-namespace:SkiaSharp.Views.Maui.Controls;assembly=SkiaSharp.Views.Maui.Controls"
    xmlns:core="clr-namespace:Bit.Core" 
    xmlns:maui="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
    HideSoftInputOnTapped="True"
    x:Name="_page"
    Title="{Binding ScanQrPageTitle}">

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

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

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="{u:I18n Cancel}" Clicked="Close_Clicked" Order="Primary" Priority="-1" />
    </ContentPage.ToolbarItems>

    <Grid
        VerticalOptions="FillAndExpand"
        HorizontalOptions="FillAndExpand">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <maui:CameraView x:Name="_cameraView"
                         AutomationId="zxingScannerView"
                         HorizontalOptions="Fill"
                         VerticalOptions="Fill"
                         ControlBarcodeResultDuplicate="True"
                         BarCodeDetectionEnabled="True"
                         BarcodeDetected="CameraViewOnBarcodeDetected"
                         BarCodeOptions="{Binding BarCodeOptions}" 
                         Cameras="{Binding Cameras, Mode=OneWayToSource}" 
                         Camera="{Binding Camera}" 
                         AutoStartPreview="{Binding AutoStartPreview}" 
                         NumCamerasDetected="{Binding NumCameras, Mode=OneWayToSource}"
                         WidthRequest="{OnPlatform Android=150}"
                         HeightRequest="{OnPlatform Android=150}"
                         Scale="{OnPlatform Android=4}"
                         Grid.Column="0"
                         Grid.Row="0"
                         Grid.RowSpan="3" />
        <StackLayout
            VerticalOptions="Center"
            HorizontalOptions="FillAndExpand"
            IsVisible="{Binding ShowScanner}"
            Grid.Column="0"
            Grid.Row="0"
            Grid.RowSpan="2"
            Margin="30,0">

            <skia:SKCanvasView
                x:Name="SkCanvasView" 
                Margin="0,50,0,0"
                WidthRequest="250"
                HeightRequest="250"
                IsVisible="{Binding ShowScanner}"
                VerticalOptions="Center" 
                HorizontalOptions="Center"
                PaintSurface="OnCanvasViewPaintSurface"/>

            <controls:IconButton 
                x:Name="_checkIcon"
                IsVisible="{Binding ShowScanner}"
                StyleClass="box-row-button, box-row-button-platform"
                Text="{Binding Source={x:Static core:BitwardenIcons.CheckCircle}}"
                HorizontalOptions="Center"
                VerticalOptions="Start"
                FontSize="Title"
                TextColor="Transparent"/>
        </StackLayout>
        <BoxView
            Grid.Column="0"
            Grid.Row="0"
            Grid.RowSpan="2"
            IsVisible="{Binding ShowScanner, Converter={StaticResource inverseBool}}"
            BackgroundColor="{DynamicResource BackgroundColor}"/>
        <StackLayout
                VerticalOptions="Center"
                HorizontalOptions="FillAndExpand"
                IsVisible="{Binding ShowScanner, Converter={StaticResource inverseBool}}"
                Grid.Column="0"
                Grid.Row="0"
                Grid.RowSpan="2"
                Margin="30,0">
            <Label
                    Text="{u:I18n EnterKeyManually}"
                    FontSize="Title" />
            <Label
                    Text="{u:I18n AuthenticatorKeyScanner}"
                    StyleClass="box-label" />
            <controls:MonoEntry
                    x:Name="_authenticationKeyEntry"
                    Text="{Binding TotpAuthenticationKey}"
                    IsSpellCheckEnabled="False"
                    IsTextPredictionEnabled="False"
                    StyleClass="box-value" />
            <Button 
                    Text="{u:I18n AddTotp}" 
                    StyleClass="box-button-row"
                    Clicked="AddAuthenticationKey_OnClicked"/>
        </StackLayout>
        <BoxView
                Grid.Column="0"
                Grid.Row="2"
                VerticalOptions="Fill"
                HorizontalOptions="FillAndExpand"
                BackgroundColor="Black"
                Opacity="0.7" />
        <StackLayout
                VerticalOptions="Start"
                HorizontalOptions="Center"
                Grid.Column="0"
                Grid.Row="2">
            <Label
                    Text="{Binding CameraInstructionTop}"
                    AutomationId="zxingDefaultOverlay_TopTextLabel"
                    Margin="30,15,30,0"
                    HorizontalOptions="Center"
                    HorizontalTextAlignment="Center"
                    StyleClass="text-sm"
                    TextColor="White" />
        </StackLayout>
        <Label
                FormattedText="{Binding ToggleScanModeLabel}"
                Grid.Column="0"
                Grid.Row="2"
                Margin="0,15"
                StyleClass="text-sm"
                FontAttributes="Bold"
                VerticalOptions="End"
                HorizontalOptions="Center" >
            <Label.GestureRecognizers>
                <TapGestureRecognizer Tapped="ToggleScanMode_OnTapped" />
            </Label.GestureRecognizers>
        </Label>
    </Grid>
</pages:BaseContentPage>