import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/app/router/pages.dart';
import 'package:pweb/pages/login/buttons.dart';
import 'package:pweb/pages/login/header.dart';
import 'package:pweb/widgets/constrained_form.dart';
import 'package:pweb/widgets/password/hint/short.dart';
import 'package:pweb/widgets/password/password.dart';
import 'package:pweb/widgets/username.dart';
import 'package:pweb/widgets/vspacer.dart';
import 'package:pweb/controllers/auth/email.dart';
import 'package:pweb/utils/error/snackbar.dart';

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


class LoginForm extends StatefulWidget {
  final String? initialEmail;

  const LoginForm({super.key, this.initialEmail});

  @override
  State<LoginForm> createState() => _LoginFormState();
}

class _LoginFormState extends State<LoginForm> {
  final EmailFieldController _emailController = EmailFieldController();
  final TextEditingController _passwordController = TextEditingController();
  final _formKey = GlobalKey<FormState>();

  // ValueNotifiers for validation state
  final ValueNotifier<bool> _isPasswordAcceptable = ValueNotifier<bool>(false);

  @override
  void initState() {
    super.initState();
    final initialEmail = widget.initialEmail?.trim();
    if (initialEmail != null && initialEmail.isNotEmpty) {
      _emailController.setText(initialEmail);
    }
  }

  Future<String?> _login(BuildContext context, VoidCallback onLogin, void Function(Object e) onError) async {
    final provider = Provider.of<AccountProvider>(context, listen: false);

    try {
      final outcome = await provider.login(
        email: _emailController.text,
        password: _passwordController.text,
        locale: context.read<LocaleProvider>().locale.languageCode,
      );
      if (outcome.isPending) {
        // TODO: fix context usage
        navigateAndReplace(context, Pages.sfactor);
      } else {
        onLogin();
      }
      return 'ok';
    } catch (e) {
      onError(provider.error ?? e);
    }
    return null;
  }

  @override
  void dispose() {
    _emailController.dispose();
    _passwordController.dispose();
    _isPasswordAcceptable.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Align(
    alignment: Alignment.center,
    child: ConstrainedBox(
      constraints: BoxConstraints(maxWidth: 400, maxHeight: 300),
      child: Card(
      child: ConstrainedForm(
        formKey: _formKey,
        children: [
          const LoginHeader(),
          const VSpacer(multiplier: 1.5),
          UsernameField(
            controller: _emailController,
          ),
          VSpacer(),
          defaulRulesPasswordField(
            context,
            controller: _passwordController,
            validationRuleBuilder: (rules, value) => shortValidation(context, rules, value),
            onValid: (isValid) => _isPasswordAcceptable.value = isValid,
          ),
          VSpacer(multiplier: 2.0),
          ValueListenableBuilder<bool>(
            valueListenable: _emailController.isValid,
            builder: (context, isUsernameValid, child) => ValueListenableBuilder<bool>(
              valueListenable: _isPasswordAcceptable,
              builder: (context, isPasswordValid, child) => ButtonsRow(
                onSignUp: () => navigateAndReplace(context, Pages.signup),
                login: () => _login(
                  context, 
                  () => navigateAndReplace(context, Pages.dashboard),
                  (e) => postNotifyUserOfErrorX(
                    context: context, 
                    errorSituation: AppLocalizations.of(context)!.errorLogin, 
                    exception: e,
                  ),
                ),
                isEnabled: isUsernameValid && isPasswordValid,
              ),
            ),
          ),
        ],
      ),
    )));
  }
