import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/app/router/pages.dart';
import 'package:pweb/pages/login/app_bar.dart';
import 'package:pweb/pages/signup/confirmation/card/card.dart';
import 'package:pweb/providers/signup_confirmation.dart';
import 'package:pweb/pages/with_footer.dart';


class SignUpConfirmationPage extends StatefulWidget {
  final String? email;
  final String? password;

  const SignUpConfirmationPage({
    super.key,
    this.email,
    this.password,
  });

  @override
  State<SignUpConfirmationPage> createState() => _SignUpConfirmationPageState();
}

class _SignUpConfirmationPageState extends State<SignUpConfirmationPage> {
  late final SignupConfirmationProvider _provider;

  @override
  void initState() {
    super.initState();
    _provider = SignupConfirmationProvider(
      accountProvider: context.read<AccountProvider>(),
    )..addListener(_handleAuthorizationStatus);
    WidgetsBinding.instance.addPostFrameCallback((_) => _startPolling());
  }

  @override
  void dispose() {
    _provider.removeListener(_handleAuthorizationStatus);
    _provider.dispose();
    super.dispose();
  }

  void _startPolling() {
    if (!mounted) return;
    final email = widget.email?.trim();
    final password = widget.password;
    if (email == null || email.isEmpty || password == null || password.isEmpty) {
      return;
    }
    _provider.startPolling(
      email: email,
      password: password,
      locale: Localizations.localeOf(context).toLanguageTag(),
    );
  }

  void _handleAuthorizationStatus() {
    if (!_provider.isAuthorized || !mounted) return;
    navigateAndReplace(context, Pages.login);
  }

  @override
  Widget build(BuildContext context) {
    final email = widget.email?.trim();
    final width = MediaQuery.of(context).size.width;
    final isWide = width >= 980;

    return PageWithFooter(
      appBar: const LoginAppBar(),
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 32),
        child: Center(
          child: ConstrainedBox(
            constraints: BoxConstraints(maxWidth: isWide ? 980 : 720),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Card(
                  elevation: 0,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(22),
                    side: BorderSide(
                      color: Theme.of(context).dividerColor.withValues(alpha: 0.6),
                    ),
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(28),
                    child: 
                      SignupConfirmationCard(
                        email: email,
                        isEmbedded: true,
                      ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
