import 'dart:async';

import 'package:app_links/app_links.dart';
import 'package:auto_route/auto_route.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/scroll_layout_helper.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
import 'package:krow/features/profile/email_verification/domain/bloc/email_verification_bloc.dart';
import 'package:krow/features/profile/email_verification/presentation/widgets/bottom_control_button.dart';
import 'package:krow/features/profile/email_verification/presentation/widgets/verification_actions_widget.dart';

@RoutePage()
class EmailVerificationScreen extends StatefulWidget
    implements AutoRouteWrapper {
  const EmailVerificationScreen({
    super.key,
    required this.verifiedEmail,
    required this.userPhone,
  });

  final String verifiedEmail;
  final String userPhone;

  @override
  State<EmailVerificationScreen> createState() =>
      _EmailVerificationScreenState();

  @override
  Widget wrappedRoute(BuildContext context) {
    return BlocProvider<EmailVerificationBloc>(
      create: (context) => EmailVerificationBloc()
        ..add(SetVerifiedData(
          verifiedEmail: verifiedEmail,
          userPhone: userPhone,
        )),
      child: this,
    );
  }
}

class _EmailVerificationScreenState extends State<EmailVerificationScreen> {
  StreamSubscription<Uri>? _appLinkSubscription;

  @override
  void initState() {
    super.initState();

    final context = this.context;
    _appLinkSubscription = AppLinks().uriLinkStream.listen(
      (link) {
        if (!context.mounted) return;

        context
            .read<EmailVerificationBloc>()
            .add(IncomingVerificationLink(verificationLink: link));
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(
        titleText: 'email_verification'.tr(),
      ),
      body: ScrollLayoutHelper(
        padding: const EdgeInsets.all(16),
        upperWidget: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Gap(4),
             Text(
              'check_your_email'.tr(),
              style: AppTextStyles.headingH1,
              textAlign: TextAlign.start,
            ),
            const Gap(8),
            Text(
              'verification_link_sent'
                  .tr(args: [widget.verifiedEmail]),
              style: AppTextStyles.bodyMediumReg.copyWith(
                color: AppColors.blackGray,
              ),
              textAlign: TextAlign.start,
            ),
            const Gap(24),
            const VerificationActionsWidget(),
          ],
        ),
        lowerWidget: const BottomControlButton(),
      ),
    );
  }

  @override
  void dispose() {
    _appLinkSubscription?.cancel();
    super.dispose();
  }
}
