import 'package:auto_route/auto_route.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/application/di/injectable.dart';
import 'package:krow/core/application/routing/routes.gr.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/ui_kit/check_box.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/core/sevices/auth_state_service/auth_service.dart';
import 'package:krow/features/check_list/domain/bloc/check_list_bloc.dart';
import 'package:krow/features/check_list/domain/bloc/check_list_event.dart';
import 'package:krow/features/check_list/domain/bloc/check_list_state.dart';
import 'package:krow/features/check_list/presentation/widgets/check_list_display_widget.dart';

@RoutePage()
class CheckListScreen extends StatelessWidget implements AutoRouteWrapper {
  const CheckListScreen({super.key});

  @override
  Widget wrappedRoute(BuildContext context) {
    return BlocProvider(
      create: (_) => CheckListBloc()..add(const CheckListEventFetch()),
      child: this,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(),
      body: BlocListener<CheckListBloc, CheckListState>(
        listenWhen: (previous, current) =>
            previous.isSubmitted != current.isSubmitted,
        listener: (context, state) {
          if (state.isSubmitted) {
            context.router.push(const WaitingValidationRoute());
          }
        },
        child: SafeArea(
          top: false,
          child: ListView(
            primary: false,
            padding: const EdgeInsets.all(16),
            children: [
               Text(
                'finalize_your_profile'.tr(),
                style: AppTextStyles.headingH1,
              ),
              const SizedBox(height: 8),
              Text(
                'check_profile_verification'.tr(),
                style: AppTextStyles.bodyMediumReg
                    .copyWith(color: AppColors.blackGray),
              ),
              const SizedBox(height: 16),
              const CheckListDisplayWidget(),
              const Gap(36),
              const _AgreementWidget(),
              const Gap(36),
              const _SubmitButtonWidget(),
              const Gap(36),
              RichText(
                textAlign: TextAlign.center,
                text: TextSpan(
                  text: '${'not_you'.tr()} ',
                  style: AppTextStyles.bodyMediumReg
                      .copyWith(color: AppColors.blackGray),
                  children: [
                    TextSpan(
                      text: 'log_out'.tr(),
                      style: AppTextStyles.bodyMediumSmb.copyWith(
                        decoration: TextDecoration.underline,
                      ),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          getIt<AuthService>().logout();
                          context.router.replace(const AuthFlowRoute());
                        },
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class _AgreementWidget extends StatelessWidget {
  const _AgreementWidget();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        context.read<CheckListBloc>().add(const CheckListEventAgree());
      },
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          BlocSelector<CheckListBloc, CheckListState, bool>(
            selector: (state) => state.isAgree,
            builder: (context, isAgree) {
              return KWCheckBox(value: isAgree);
            },
          ),
          const Gap(8),
          Expanded(
            child: RichText(
              text: TextSpan(
                children: [
                   TextSpan(
                    text: '${'i_agree_to_the'.tr()} ',
                    style: AppTextStyles.bodyMediumMed,
                  ),
                  TextSpan(
                    text: 'terms_and_conditions'.tr(),
                    style: AppTextStyles.bodyMediumSmb.copyWith(
                      decoration: TextDecoration.underline,
                    ),
                    recognizer: TapGestureRecognizer()
                      ..onTap = () {
                        //TODO(Sleep): Handle Terms and Conditions tap
                      },
                  ),
                   TextSpan(
                    text: ' ${'and'.tr()} ',
                    style: AppTextStyles.bodyMediumMed,
                  ),
                  TextSpan(
                    text: 'privacy_policy'.tr(),
                    style: AppTextStyles.bodyMediumSmb.copyWith(
                      decoration: TextDecoration.underline,
                    ),
                    recognizer: TapGestureRecognizer()
                      ..onTap = () {
                        //TODO(Sleep): Handle Privacy Policy tap
                      },
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}

class _SubmitButtonWidget extends StatelessWidget {
  const _SubmitButtonWidget();

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CheckListBloc, CheckListState>(
        buildWhen: (previous, current) {
      return previous.isAgree != current.isAgree ||
          previous.hasErrors != current.hasErrors;
    }, builder: (context, state) {
      return KwButton.primary(
        disabled: !state.isAgree || state.hasErrors,
        label: '${'submit_profile_verification'.tr()} ',
        onPressed: () {
          context.read<CheckListBloc>().add(const CheckListEventSubmit());
        },
      );
    });
  }
}
