<?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"
    xmlns:xct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    x:Class="Bit.App.Pages.SendAddEditPage"
    xmlns:pages="clr-namespace:Bit.App.Pages"
    xmlns:u="clr-namespace:Bit.App.Utilities"
    xmlns:controls="clr-namespace:Bit.App.Controls"
    xmlns:behaviors="clr-namespace:Bit.App.Behaviors"
    xmlns:effects="clr-namespace:Bit.App.Effects"
    xmlns:core="clr-namespace:Bit.Core"
    x:DataType="pages:SendAddEditPageViewModel"
    HideSoftInputOnTapped="True"
    Unloaded="OnUnloaded"
    x:Name="_page"
    Title="{Binding PageTitle}">
    <ContentPage.BindingContext>
        <pages:SendAddEditPageViewModel />
    </ContentPage.BindingContext>

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="{u:I18n Save}" Clicked="Save_Clicked" Order="Primary" x:Name="_saveItem"/>
    </ContentPage.ToolbarItems>

    <ContentPage.Resources>
        <ResourceDictionary>
            <u:InverseBoolConverter x:Key="inverseBool" />
            <u:IsNullConverter x:Key="null" />
            <u:IsNotNullConverter x:Key="notNull" />
            <ToolbarItem Text="{u:I18n Cancel}" Clicked="Close_Clicked" Order="Primary" Priority="-1"
                         x:Key="closeItem" x:Name="_closeItem" />
            <ToolbarItem IconImageSource="more_vert.png" Clicked="More_Clicked" Order="Primary" x:Name="_moreItem"
                         x:Key="moreItem"
                         SemanticProperties.Description="{u:I18n Options}" />
            <ToolbarItem Text="{u:I18n RemovePassword}"
                         Clicked="RemovePassword_Clicked"
                         Order="Secondary"
                         IsDestructive="True"
                         x:Name="_removePassword"
                         x:Key="removePassword" />
            <ToolbarItem Text="{u:I18n CopyLink}"
                         Clicked="CopyLink_Clicked"
                         Order="Secondary"
                         IsDestructive="True"
                         x:Name="_copyLink"
                         x:Key="copyLink" />
            <ToolbarItem Text="{u:I18n ShareLink}"
                         Clicked="ShareLink_Clicked"
                         Order="Secondary"
                         IsDestructive="True"
                         x:Name="_shareLink"
                         x:Key="shareLink" />
            <ToolbarItem Text="{u:I18n Delete}"
                         Clicked="Delete_Clicked"
                         Order="Secondary"
                         IsDestructive="True"
                         x:Name="_deleteItem"
                         x:Key="deleteItem" />

            <ScrollView x:Key="scrollView" x:Name="_scrollView">
                <StackLayout Spacing="20">
                    <StackLayout StyleClass="box">
                        <Frame
                            IsVisible="{Binding SendEnabled, Converter={StaticResource inverseBool}}"
                            Padding="10"
                            Margin="0, 12, 0, 0"
                            HasShadow="False"
                            BackgroundColor="Transparent"
                            BorderColor="{DynamicResource PrimaryColor}">
                            <Label
                                Text="{u:I18n SendDisabledWarning}"
                                StyleClass="text-muted, text-sm, text-bold"
                                HorizontalTextAlignment="Center"
                                AutomationId="SendDisabledWarningMessageLabel" />
                        </Frame>
                        <Frame
                            IsVisible="{Binding SendOptionsPolicyInEffect}"
                            Padding="10"
                            Margin="0, 12, 0, 0"
                            HasShadow="False"
                            BackgroundColor="Transparent"
                            BorderColor="{DynamicResource PrimaryColor}">
                            <Label
                                Text="{u:I18n SendOptionsPolicyInEffect}"
                                StyleClass="text-muted, text-sm, text-bold"
                                HorizontalTextAlignment="Center"
                                AutomationId="SendOptionsPolicyInEffectLabel" />
                        </Frame>
                        <StackLayout StyleClass="box-row">
                            <Label
                                Text="{u:I18n Name}"
                                StyleClass="box-label" />
                            <Entry
                                x:Name="_nameEntry"
                                Text="{Binding Send.Name}"
                                IsEnabled="{Binding SendEnabled}"
                                StyleClass="box-value"
                                AutomationId="SendNameEntry" />
                            <Label
                                Text="{u:I18n NameInfo}"
                                StyleClass="box-footer-label"
                                Margin="0,5,0,0" />
                        </StackLayout>
                        <StackLayout
                            StyleClass="box-row"
                            IsVisible="{Binding ShowTypeButtons}">
                            <Label
                                Text="{u:I18n Type}"
                                StyleClass="box-label" />
                            <Grid
                                RowSpacing="0"
                                ColumnSpacing="0"
                                Margin="{Binding SegmentedButtonMargins}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Button
                                    Text="{u:I18n TypeFile}"
                                    StyleClass="segmented-button"
                                    IsEnabled="{Binding IsText}"
                                    HeightRequest="{Binding SegmentedButtonHeight}"
                                    FontSize="{Binding SegmentedButtonFontSize}"
                                    Clicked="FileType_Clicked"
                                    SemanticProperties.Description="{u:I18n File}"
                                    SemanticProperties.Hint="{Binding FileTypeAccessibilityLabel}"
                                    AutomationId="SendFileButton"
                                    Grid.Column="0">
                                </Button>
                                <Button
                                    Text="{u:I18n TypeText}"
                                    StyleClass="segmented-button"
                                    IsEnabled="{Binding IsFile}"
                                    HeightRequest="{Binding SegmentedButtonHeight}"
                                    FontSize="{Binding SegmentedButtonFontSize}"
                                    Clicked="TextType_Clicked"
                                    SemanticProperties.Description="{u:I18n Text}"
                                    SemanticProperties.Hint="{Binding TextTypeAccessibilityLabel}"
                                    AutomationId="SendTextButton"
                                    Grid.Column="1">
                                </Button>
                            </Grid>
                        </StackLayout>
                        <StackLayout
                            StyleClass="box-row"
                            IsVisible="{Binding IsFile}">
                            <Label
                                Text="{u:I18n TypeFile}"
                                StyleClass="box-label" />
                            <StackLayout 
                                IsVisible="{Binding EditMode}"
                                Orientation="Horizontal">
                                <Label
                                    Text="{Binding Send.File.FileName, Mode=OneWay}"
                                    StyleClass="box-value"
                                    VerticalTextAlignment="Center"
                                    HorizontalOptions="StartAndExpand"
                                    AutomationId="SendFileNameLabel" />
                                <Label
                                    Text="{Binding Send.File.SizeName, Mode=OneWay}"
                                    StyleClass="box-sub-label"
                                    HorizontalTextAlignment="End"
                                    VerticalTextAlignment="Center"
                                    AutomationId="SendFileSizeLabel" />
                            </StackLayout>
                            <StackLayout
                                IsVisible="{Binding EditMode, Converter={StaticResource inverseBool}}"
                                StyleClass="box-row">
                                <Label
                                    IsVisible="{Binding FileName, Converter={StaticResource null}}"
                                    Text="{u:I18n NoFileChosen}"
                                    LineBreakMode="CharacterWrap"
                                    StyleClass="text-sm, text-muted"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="Start"
                                    AutomationId="SendNoFileChosenLabel" />
                                <Label
                                    IsVisible="{Binding FileName, Converter={StaticResource notNull}}"
                                    Text="{Binding FileName}"
                                    LineBreakMode="CharacterWrap"
                                    StyleClass="text-sm, text-muted"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="Start"
                                    AutomationId="SendCurrentFileNameLabel" />
                                <Button
                                    Text="{u:I18n ChooseFile}"
                                    IsVisible="{Binding IsAddFromShare, Converter={StaticResource inverseBool}}"
                                    IsEnabled="{Binding SendEnabled}"
                                    StyleClass="box-button-row"
                                    Clicked="ChooseFile_Clicked"
                                    AutomationId="SendChooseFileButton" />
                                <Label
                                    Margin="0, 5, 0, 0"
                                    Text="{u:I18n MaxFileSize}"
                                    StyleClass="text-sm, text-muted"
                                    HorizontalOptions="FillAndExpand"
                                    HorizontalTextAlignment="Start" />
                            </StackLayout>
                            <Label
                                Text="{u:I18n TypeFileInfo}"
                                IsVisible="{Binding ShowTypeButtons}"
                                StyleClass="box-footer-label"
                                Margin="0,5,0,0" />
                        </StackLayout>
                        <StackLayout
                            StyleClass="box-row"
                            IsVisible="{Binding IsText}">
                            <Label
                                Text="{u:I18n TypeText}"
                                StyleClass="box-label" />
                            <Editor
                                x:Name="_textEditor"
                                AutoSize="TextChanges"
                                Text="{Binding Send.Text.Text}"
                                IsEnabled="{Binding SendEnabled}"
                                StyleClass="box-value"
                                Margin="{Binding EditorMargins}"
                                AutomationId="SendTextContentEntry"
                                effects:ScrollEnabledEffect.IsScrollEnabled="false" >
                                <Editor.Behaviors>
                                    <behaviors:EditorPreventAutoBottomScrollingOnFocusedBehavior ParentScrollView="{x:Reference _scrollView}" />
                                </Editor.Behaviors>
                                <Editor.Effects>
                                    <effects:ScrollEnabledEffect />
                                </Editor.Effects>
                            </Editor>
                            <BoxView
                                StyleClass="box-row-separator"
                                IsVisible="{Binding ShowEditorSeparators}" />
                            <Label
                                Text="{u:I18n TypeTextInfo}"
                                StyleClass="box-footer-label"
                                Margin="0,5,0,10" />
                            <StackLayout
                                StyleClass="box-row, box-row-switch"
                                Margin="0,10,0,0">
                                <Label
                                    Text="{u:I18n HideTextByDefault}"
                                    StyleClass="box-label-regular"
                                    VerticalOptions="Center"
                                    HorizontalOptions="StartAndExpand" />
                                <Switch
                                    IsToggled="{Binding Send.Text.Hidden}"
                                    IsEnabled="{Binding SendEnabled}"
                                    HorizontalOptions="End"
                                    Margin="10,0,0,0"
                                    AutomationId="SendHideTextByDefaultToggle" />
                            </StackLayout>
                        </StackLayout>
                        <StackLayout
                            Orientation="Horizontal"
                            Spacing="0"
                            SemanticProperties.Description="{Binding OptionsAccessilibityText}">
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer Command="{Binding ToggleOptionsCommand}" />
                            </StackLayout.GestureRecognizers>
                            <controls:CustomLabel
                                Text="{u:I18n Options}"
                                x:Name="_btnOptions"
                                StyleClass="box-row-button"
                                VerticalTextAlignment="Center"
                                TextColor="{DynamicResource PrimaryColor}"
                                Margin="0"
                                AutomationProperties.IsInAccessibleTree="False"
                                AutomationId="SendShowHideOptionsButton" />
                            <controls:IconButton
                                x:Name="_btnOptionsUp"
                                InputTransparent="True"
                                MinimumWidthRequest="25"
                                Text="{Binding Source={x:Static core:BitwardenIcons.ChevronUp}}"
                                StyleClass="box-row-button"
                                TextColor="{DynamicResource PrimaryColor}"
                                IsVisible="{Binding ShowOptions}" 
                                AutomationProperties.IsInAccessibleTree="False"
                                AutomationId="SendOptionsDisplayed" />
                            <controls:IconButton
                                x:Name="_btnOptionsDown"
                                InputTransparent="True"
                                MinimumWidthRequest="25"
                                Text="{Binding Source={x:Static core:BitwardenIcons.AngleDown}}"
                                StyleClass="box-row-button"
                                TextColor="{DynamicResource PrimaryColor}"
                                IsVisible="{Binding ShowOptions, Converter={StaticResource inverseBool}}" 
                                AutomationProperties.IsInAccessibleTree="False"
                                AutomationId="SendOptionsHidden" />
                        </StackLayout>
                        <StackLayout IsVisible="{Binding ShowOptions}">
                            <StackLayout
                                StyleClass="box-row"
                                Margin="0,10,0,0">
                                <Label
                                    Text="{u:I18n DeletionDate}"
                                    StyleClass="box-label" />
                                <Picker
                                    x:Name="_deletionDateTypePicker"
                                    IsVisible="{Binding EditMode, Converter={StaticResource inverseBool}}"
                                    ItemsSource="{Binding DeletionTypeOptions, Mode=OneTime}"
                                    SelectedIndex="{Binding DeletionDateTypeSelectedIndex}"
                                    IsEnabled="{Binding SendEnabled}"
                                    StyleClass="box-value"
                                    SemanticProperties.Description="{u:I18n DeletionTime}"
                                    AutomationId="SendDeletionOptionsPicker" />
                                <Grid
                                    IsVisible="{Binding ShowDeletionCustomPickers}"
                                    Margin="0,5,0,0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <controls:ExtendedDatePicker
                                        x:Name="_deletionExtendedDatePicker"
                                        NullableDate="{Binding DeletionDateTimeViewModel.Date, Mode=TwoWay}"
                                        Format="d"
                                        IsEnabled="{Binding SendEnabled}"
                                        SemanticProperties.Description="{u:I18n DeletionDate}"
                                        Grid.Column="0"
                                        AutomationId="SendCustomDeletionDatePicker" />
                                    <controls:ExtendedTimePicker
                                        NullableTime="{Binding DeletionDateTimeViewModel.Time, Mode=TwoWay}"
                                        Format="t"
                                        IsEnabled="{Binding SendEnabled}"
                                        SemanticProperties.Description="{u:I18n DeletionTime}"
                                        Grid.Column="1"
                                        AutomationId="SendCustomDeletionTimePicker" />
                                </Grid>
                                <Label
                                    Text="{u:I18n DeletionDateInfo}"
                                    StyleClass="box-footer-label"
                                    Margin="0,5,0,0" />
                            </StackLayout>
                            <StackLayout StyleClass="box-row" Margin="0,5,0,0">
                                <Label
                                    Text="{u:I18n ExpirationDate}"
                                    StyleClass="box-label" />
                                <Picker
                                    x:Name="_expirationDateTypePicker"
                                    IsVisible="{Binding EditMode, Converter={StaticResource inverseBool}}"
                                    ItemsSource="{Binding ExpirationTypeOptions, Mode=OneTime}"
                                    SelectedIndex="{Binding ExpirationDateTypeSelectedIndex}"
                                    IsEnabled="{Binding SendEnabled}"
                                    StyleClass="box-value"
                                    SemanticProperties.Description="{u:I18n ExpirationTime}"
                                    AutomationId="SendExpirationOptionsPicker" />
                                <Grid
                                    IsVisible="{Binding ShowExpirationCustomPickers}"
                                    Margin="0,5,0,0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <controls:ExtendedDatePicker
                                        x:Name="_expirationExtendedDatePicker"
                                        NullableDate="{Binding ExpirationDateTimeViewModel.Date, Mode=TwoWay}"
                                        PlaceHolder="mm/dd/yyyy"
                                        Format="d"
                                        IsEnabled="{Binding SendEnabled}"
                                        SemanticProperties.Description="{u:I18n ExpirationDate}"
                                        Grid.Column="0"
                                        AutomationId="SendCustomExpirationDatePicker" />
                                    <controls:ExtendedTimePicker
                                        NullableTime="{Binding ExpirationDateTimeViewModel.Time, Mode=TwoWay}"
                                        PlaceHolder="--:-- --"
                                        Format="t"
                                        IsEnabled="{Binding SendEnabled}"
                                        SemanticProperties.Description="{u:I18n ExpirationTime}"
                                        Grid.Column="1"
                                        AutomationId="SendCustomExpirationTimePicker" />
                                </Grid>
                                <StackLayout
                                    Orientation="Horizontal"
                                    Margin="0,5,0,0">
                                    <Label
                                        Text="{u:I18n ExpirationDateInfo}"
                                        StyleClass="box-footer-label"
                                        HorizontalOptions="StartAndExpand" />
                                    <Button
                                        Text="{u:I18n Clear}"
                                        IsVisible="{Binding EditMode}"
                                        WidthRequest="110"
                                        HeightRequest="{Binding SegmentedButtonHeight}"
                                        FontSize="{Binding SegmentedButtonFontSize}"
                                        IsEnabled="{Binding SendEnabled}"
                                        StyleClass="box-row-button"
                                        Clicked="ClearExpirationDate_Clicked"
                                        AutomationId="SendClearExpirationDateButton" />
                                </StackLayout>
                            </StackLayout>
                            <StackLayout
                                StyleClass="box-row"
                                Margin="0,5,0,0">
                                <Label
                                    Text="{u:I18n MaximumAccessCount}"
                                    StyleClass="box-label" />
                                <StackLayout
                                    StyleClass="box-row"
                                    Orientation="Horizontal">
                                    <Entry
                                        Text="{Binding MaxAccessCount}"
                                        IsEnabled="{Binding SendEnabled}"
                                        StyleClass="box-value"
                                        Keyboard="Numeric"
                                        MaxLength="9"
                                        TextChanged="OnMaxAccessCountTextChanged"
                                        HorizontalOptions="FillAndExpand"
                                        AutomationId="SendMaxAccessCountEntry" />
                                    <controls:ExtendedStepper
                                        x:Name="_maxAccessCountStepper"
                                        Value="{Binding MaxAccessCount, TargetNullValue=0}"
                                        Maximum="999999999"
                                        IsEnabled="{Binding SendEnabled}"
                                        Margin="10,0,0,0"
                                        AutomationId="SendMaxAccessCountStepper" />
                                </StackLayout>
                                <Label
                                    Text="{u:I18n MaximumAccessCountInfo}"
                                    StyleClass="box-footer-label" />
                                <StackLayout
                                    IsVisible="{Binding EditMode}"
                                    StyleClass="box-row"
                                    Orientation="Horizontal">
                                    <Label
                                        Text="{u:I18n CurrentAccessCount}"
                                        StyleClass="box-footer-label"
                                        VerticalTextAlignment="Center" />
                                    <Label
                                        Text=": "
                                        StyleClass="box-footer-label"
                                        VerticalTextAlignment="Center" />
                                    <Label
                                        Text="{Binding Send.AccessCount, Mode=OneWay}"
                                        StyleClass="box-label"
                                        VerticalTextAlignment="Center"
                                        AutomationId="SendCurrentAccessCountLabel" />
                                </StackLayout>
                            </StackLayout>
                            <StackLayout
                                StyleClass="box-row"
                                Margin="0,5,0,0">
                                <Label
                                    Text="{u:I18n NewPassword}"
                                    StyleClass="box-label" />
                                <StackLayout Orientation="Horizontal">
                                    <Entry
                                        Text="{Binding NewPassword}"
                                        IsPassword="{Binding ShowPassword, Converter={StaticResource inverseBool}}"
                                        IsEnabled="{Binding SendEnabled}"
                                        StyleClass="box-value"
                                        IsSpellCheckEnabled="False"
                                        IsTextPredictionEnabled="False"
                                        HorizontalOptions="FillAndExpand"
                                        AutomationId="SendNewPasswordEntry" />
                                    <controls:IconButton
                                        IsEnabled="{Binding SendEnabled}"
                                        StyleClass="box-row-button, box-row-button-platform"
                                        Text="{Binding ShowPasswordIcon}"
                                        Command="{Binding TogglePasswordCommand}"
                                        Margin="10,0,0,0"
                                        SemanticProperties.Description="{u:I18n ToggleVisibility}"
                                        SemanticProperties.Hint="{Binding PasswordVisibilityAccessibilityText}"
                                        AutomationId="SendShowHidePasswordButton" />
                                </StackLayout>
                                <Label
                                    Text="{u:I18n PasswordInfo}"
                                    StyleClass="box-footer-label"
                                    Margin="0,5,0,0" />
                            </StackLayout>
                            <StackLayout
                                StyleClass="box-row"
                                Margin="0,5,0,0">
                                <Label
                                    Text="{u:I18n Notes}"
                                    StyleClass="box-label" />
                                <Editor
                                    AutoSize="TextChanges"
                                    Text="{Binding Send.Notes}"
                                    IsEnabled="{Binding SendEnabled}"
                                    StyleClass="box-value"
                                    Margin="{Binding EditorMargins}"
                                    effects:ScrollEnabledEffect.IsScrollEnabled="false"
                                    AutomationId="SendNotesEntry">
                                    <Editor.Behaviors>
                                        <behaviors:EditorPreventAutoBottomScrollingOnFocusedBehavior ParentScrollView="{x:Reference _scrollView}" />
                                    </Editor.Behaviors>
                                    <Editor.Effects>
                                        <effects:ScrollEnabledEffect />
                                    </Editor.Effects>
                                </Editor>
                                <BoxView
                                    StyleClass="box-row-separator"
                                    IsVisible="{Binding ShowEditorSeparators}" />
                                <Label
                                    Text="{u:I18n NotesInfo}"
                                    StyleClass="box-footer-label"
                                    Margin="0,5,0,0" />
                            </StackLayout>
                            <StackLayout
                                StyleClass="box-row, box-row-switch"
                                Margin="0,5,0,0">
                                <Label
                                    Text="{u:I18n HideEmail}"
                                    StyleClass="box-label-regular"
                                    VerticalOptions="Center"
                                    HorizontalOptions="StartAndExpand" />
                                <Switch
                                    IsToggled="{Binding Send.HideEmail}"
                                    IsEnabled="{Binding DisableHideEmailControl, Converter={StaticResource inverseBool}}"
                                    HorizontalOptions="End"
                                    Margin="10,0,0,0"
                                    AutomationId="SendHideEmailSwitch" />
                            </StackLayout>
                            <StackLayout
                                StyleClass="box-row, box-row-switch"
                                Margin="0,5,0,0">
                                <Label
                                    Text="{u:I18n DisableSend}"
                                    StyleClass="box-label-regular"
                                    VerticalOptions="Center"
                                    HorizontalOptions="StartAndExpand" />
                                <Switch
                                    IsToggled="{Binding Send.Disabled}"
                                    IsEnabled="{Binding SendEnabled}"
                                    HorizontalOptions="End"
                                    Margin="10,0,0,0"
                                    AutomationId="SendDeactivateSwitch" />
                            </StackLayout>
                        </StackLayout>

                    </StackLayout>
                </StackLayout>
            </ScrollView>

        </ResourceDictionary>
    </ContentPage.Resources>

</pages:BaseContentPage>
