import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:kasy_kit/components/components.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/core/widgets/kasy_scroll_behavior.dart';
import 'package:kasy_kit/features/authentication/providers/models/email.dart';
import 'package:kasy_kit/features/authentication/providers/models/recover_state.dart';
import 'package:kasy_kit/features/authentication/providers/recover_provider.dart';
import 'package:kasy_kit/features/authentication/ui/widgets/recover_password_result.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

final _formKey = GlobalKey<FormState>();

class RecoverPasswordPage extends ConsumerWidget {
  const RecoverPasswordPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final state = ref.watch(recoverStateProvider);

    return PopScope(
      canPop: context.canPop(),
      onPopInvokedWithResult: (didPop, _) {
        if (!didPop) {
          return;
        }
        ref.invalidate(recoverStateProvider);
      },
      child: AnnotatedRegion<SystemUiOverlayStyle>(
          value: SystemUiOverlayStyle.dark,
          child: KasyOverlayScaffold(
            title: t.auth.recover.title,
            appBarStyle: KasyAppBarStyle.subpageSimple,
            onBack: () {
              if (context.canPop()) {
                context.pop();
              }
            },
            slivers: [
              switch (state) {
                RecoverStateData() => SliverToBoxAdapter(
                child: ScrollConfiguration(
                  behavior: const KasyKitScrollBehavior(),
                  child: Form(
                    key: _formKey,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        const SizedBox(height: KasySpacing.lg),
                        KasyTextField(
                          key: const Key('email_input'),
                          label: t.auth.recover.email_label,
                          contentType: KasyTextFieldContentType.email,
                          validator: (value) {
                            try {
                              state.email.validate();
                            } on EmailException catch (_) {
                              return t.auth.signin.email_invalid;
                            }
                            return null;
                          },
                          onChanged: (value) => ref
                              .read(recoverStateProvider.notifier)
                              .setEmail(value),
                        ),
                        const SizedBox(height: KasySpacing.lg),
                        KasyButton(
                          key: const Key('recover_button'),
                          label: t.auth.recover.submit,
                          onPressed: () {
                            if (!_formKey.currentState!.validate()) return;
                            ref.read(recoverStateProvider.notifier).send();
                          },
                          expand: true,
                        ),
                      ],
                    ),
                  ),
                ),
              ),
                RecoverStateSending() => SliverFillRemaining(
                  child: Center(
                    child: CircularProgressIndicator.adaptive(
                      valueColor: AlwaysStoppedAnimation<Color>(
                        context.colors.primary,
                      ),
                    ),
                  ),
                ),
                RecoverStateSent() => const SliverFillRemaining(
                  child: RecoverPasswordSent(),
                ),
              },
            ],
          ),
        ),
    );
  }
}
