import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/provider/account.dart';

import 'package:pweb/pages/settings/profile/account/password/form/form.dart';
import 'package:pweb/providers/password_form.dart';

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


class AccountPasswordContent extends StatelessWidget {
  const AccountPasswordContent({
    required this.title,
    required this.successText,
    required this.errorText,
    required this.oldPasswordLabel,
    required this.newPasswordLabel,
    required this.confirmPasswordLabel,
    required this.savePassword,
    required this.loc,
  });

  final String title;
  final String successText;
  final String errorText;
  final String oldPasswordLabel;
  final String newPasswordLabel;
  final String confirmPasswordLabel;
  final String savePassword;
  final AppLocalizations loc;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Consumer2<AccountProvider, PasswordFormProvider>(
      builder: (context, accountProvider, formProvider, _) {
        final isBusy = accountProvider.isLoading || formProvider.isSaving;

        return Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            TextButton.icon(
              onPressed: isBusy ? null : formProvider.toggleExpanded,
              icon: Icon(Icons.lock_outline, color: theme.colorScheme.primary),
              label: Text(title, style: theme.textTheme.bodyMedium),
            ),
            if (formProvider.isExpanded)
              PasswordForm(
                formProvider: formProvider,
                accountProvider: accountProvider,
                isBusy: accountProvider.isLoading,
                oldPasswordLabel: oldPasswordLabel,
                newPasswordLabel: newPasswordLabel,
                confirmPasswordLabel: confirmPasswordLabel,
                savePassword: savePassword,
                successText: successText,
                errorText: errorText,
                loc: loc,
              ),
          ],
        );
      },
    );
  }
}
