import 'package:flutter/material.dart';

import 'package:pshared/provider/account.dart';
import 'package:pshared/widgets/password/fields.dart';
import 'package:pshared/utils/snackbar.dart';

import 'package:pweb/models/password_field_type.dart';
import 'package:pweb/providers/password_form.dart';
import 'package:pweb/pages/settings/profile/account/password/form/error_text.dart';
import 'package:pweb/pages/settings/profile/account/password/form/submit_button.dart';
import 'package:pweb/utils/error/snackbar.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


class PasswordForm extends StatelessWidget {
  const PasswordForm({
    super.key,
    required this.formProvider,
    required this.accountProvider,
    required this.isBusy,
    required this.oldPasswordLabel,
    required this.newPasswordLabel,
    required this.confirmPasswordLabel,
    required this.savePassword,
    required this.successText,
    required this.errorText,
    required this.loc,
  });

  static const double _fieldWidth = 320;
  static const double _gapMedium = 12;
  static const double _gapSmall = 8;

  final PasswordFormProvider formProvider;
  final AccountProvider accountProvider;
  final bool isBusy;
  final String oldPasswordLabel;
  final String newPasswordLabel;
  final String confirmPasswordLabel;
  final String savePassword;
  final String successText;
  final String errorText;
  final AppLocalizations loc;

  @override
  Widget build(BuildContext context) {
    final isFormBusy = isBusy || formProvider.isSaving;

    return Column(
      children: [
        const SizedBox(height: _gapMedium),
        Form(
          key: formProvider.formKey,
          child: Column(
            children: [
              PasswordFields(
                oldPasswordController: formProvider.oldPasswordController,
                newPasswordController: formProvider.newPasswordController,
                confirmPasswordController: formProvider.confirmPasswordController,
                oldPasswordLabel: oldPasswordLabel,
                newPasswordLabel: newPasswordLabel,
                confirmPasswordLabel: confirmPasswordLabel,
                missingPasswordError: loc.errorPasswordMissing,
                passwordsDoNotMatchError: loc.passwordsDoNotMatch,
                fieldWidth: _fieldWidth,
                gapSmall: _gapSmall,
                isEnabled: !isFormBusy,
                showOldPassword:
                    formProvider.isPasswordVisible(PasswordFieldType.old),
                showNewPassword:
                    formProvider.isPasswordVisible(PasswordFieldType.newPassword),
                showConfirmPassword: formProvider
                    .isPasswordVisible(PasswordFieldType.confirmPassword),
                onToggleOldPassword: () =>
                    formProvider.togglePasswordVisibility(PasswordFieldType.old),
                onToggleNewPassword: () => formProvider
                    .togglePasswordVisibility(PasswordFieldType.newPassword),
                onToggleConfirmPassword: () => formProvider
                    .togglePasswordVisibility(PasswordFieldType.confirmPassword),
              ),
              const SizedBox(height: _gapMedium),
              PasswordSubmitButton(
                isBusy: isFormBusy,
                label: savePassword,
                onSubmit: () async {
                  try {
                    final success = await formProvider.submit(
                      accountProvider: accountProvider,
                      errorText: errorText,
                    );
                    if (!success) return;
                    if (!context.mounted) return;
                    notifyUser(context, successText);
                  } catch (e) {
                    if (!context.mounted) return;
                    await postNotifyUserOfErrorX(
                      context: context,
                      errorSituation: errorText,
                      exception: e,
                    );
                  }
                },
              ),
              PasswordErrorText(
                errorText: formProvider.errorText,
                gapSmall: _gapSmall,
              ),
            ],
          ),
        ),
      ],
    );
  }
}
