import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/widgets/locale.dart';

import 'package:pweb/app/router/pages.dart';
import 'package:pweb/pages/errors/error.dart';
import 'package:pweb/pages/status/success.dart';
import 'package:pweb/pages/with_footer.dart';
import 'package:pweb/pages/verification/controller.dart';
import 'package:pweb/pages/verification/resend_dialog.dart';
import 'package:pweb/utils/snackbar.dart';
import 'package:pweb/utils/error/snackbar.dart';
import 'package:pweb/widgets/resend_link.dart';

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


class AccountVerificationContent extends StatefulWidget {
  const AccountVerificationContent();

  @override
  State<AccountVerificationContent> createState() =>
      AccountVerificationContentState();
}

class AccountVerificationContentState
    extends State<AccountVerificationContent> {
  Future<void> _resendVerificationEmail() async {
    final controller = context.read<AccountVerificationController>();
    if (controller.isResending) return;
    final locs = AppLocalizations.of(context)!;
    final email = await requestVerificationEmail(context, locs);
    if (!mounted || email == null) return;
    if (email.isEmpty) {
      notifyUser(context, locs.errorEmailMissing);
      return;
    }

    try {
      await controller.resendVerificationEmail(email);
      if (!mounted) return;
      await notifyUser(context, locs.signupConfirmationResent(email));
    } catch (e) {
      if (!mounted) return;
      await postNotifyUserOfErrorX(
        context: context,
        errorSituation: locs.signupConfirmationResendError,
        exception: e,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final locs = AppLocalizations.of(context)!;
    final controller = context.watch<AccountVerificationController>();
    final action = OutlinedButton.icon(
      onPressed: () => navigateAndReplace(context, Pages.login),
      icon: const Icon(Icons.login),
      label: Text(locs.login),
    );

    Widget content;
    if (controller.isLoading) {
      content = const Center(child: CircularProgressIndicator());
    } else if (controller.isAlreadyVerified) {
      content = StatusPageSuccess(
        successMessage: locs.accountAlreadyVerified,
        successDescription: locs.accountAlreadyVerifiedDescription,
        action: action,
      );
    } else if (controller.isSuccess) {
      content = StatusPageSuccess(
        successMessage: locs.accountVerified,
        successDescription: locs.accountVerifiedDescription,
        action: action,
      );
    } else {
      content = exceptionToErrorPage(
        context: context,
        title: locs.verificationFailed,
        errorMessage: locs.accountVerificationFailed,
        exception:
            controller.error ?? Exception(locs.accountVerificationFailed),
        action: controller.canResend
            ? ResendLink(
                label: locs.signupConfirmationResend,
                onPressed: _resendVerificationEmail,
                isDisabled: !controller.canResend || controller.isResending,
                isLoading: controller.isResending,
              )
            : null,
      );
    }

    return PageWithFooter(
      appBar: AppBar(
        title: Text(locs.verifyAccount),
        centerTitle: true,
        actions: [
          const LocaleChangerDropdown(
            availableLocales: AppLocalizations.supportedLocales,
          ),
        ],
      ),
      child: content,
    );
  }
}
