import 'package:flutter/material.dart';

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

import 'package:pweb/controllers/auth/password_form.dart';
import 'package:pweb/models/state/control_state.dart';
import 'package:pweb/models/state/visibility.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/widgets/password/ui_controller.dart';
import 'package:pweb/widgets/password/verify.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 PasswordFormController 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;
    final controlState = isFormBusy
        ? ControlState.disabled
        : ControlState.enabled;

    return Column(
      children: [
        const SizedBox(height: _gapMedium),
        Form(
          key: formProvider.formKey,
          child: Column(
            children: [
              PasswordField(
                controller: formProvider.oldPasswordController,
                labelText: oldPasswordLabel,
                fieldWidth: _fieldWidth,
                isEnabled: controlState == ControlState.enabled,
                obscureText:
                    formProvider.oldPasswordVisibility !=
                    VisibilityState.visible,
                onToggleVisibility: formProvider.toggleOldPasswordVisibility,
                validator: (value) => (value == null || value.isEmpty)
                    ? loc.errorPasswordMissing
                    : null,
              ),
              const SizedBox(height: _gapSmall),
              SizedBox(
                width: _fieldWidth,
                child: PasswordUiController(
                  controller: formProvider.newPasswordController,
                  labelText: newPasswordLabel,
                  state: controlState,
                ),
              ),
              const SizedBox(height: _gapSmall),
              SizedBox(
                width: _fieldWidth,
                child: VerifyPasswordField(
                  controller: formProvider.confirmPasswordController,
                  externalPasswordController:
                      formProvider.newPasswordController,
                  labelText: confirmPasswordLabel,
                  state: controlState,
                ),
              ),
              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,
              ),
            ],
          ),
        ),
      ],
    );
  }
}
