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/gen/assets.gen.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/kw_app_bar.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/features/profile/emergency_contacts/domain/bloc/emergency_contacts_bloc.dart';
import 'package:krow/features/profile/emergency_contacts/presentation/widgets/bottom_control_button.dart';
import 'package:krow/features/profile/emergency_contacts/presentation/widgets/contacts_list_sliver.dart';

@RoutePage()
class EmergencyContactsScreen extends StatelessWidget
    implements AutoRouteWrapper {
  const EmergencyContactsScreen({
    super.key,
    this.isInEditMode = true,
  });

  final bool isInEditMode;

  @override
  Widget wrappedRoute(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => EmergencyContactsBloc()
        ..add(InitializeEmergencyContactsEvent(isInEditMode)),
      child: this,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      extendBody: true,
      appBar: KwAppBar(
        titleText: 'emergency_contact'.tr(),
        centerTitle: true,
        showNotification: isInEditMode,
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        child: CustomScrollView(
          primary: false,
          slivers: [
            if (!isInEditMode)
              SliverList.list(
                children: [
                  const Gap(16),
                  Text(
                    'add_emergency_contacts'.tr(),
                    style: Theme.of(context).textTheme.titleLarge,
                  ),
                  const Gap(8),
                  RichText(
                    text: TextSpan(
                      text: 'provide_emergency_contact'.tr(),
                      style: AppTextStyles.bodyMediumReg
                          .copyWith(color: AppColors.blackGray),
                      children: [
                        TextSpan(
                          text: ' ${'must_have_one_contact'.tr()}',
                          style: AppTextStyles.bodyMediumSmb,
                        ),
                      ],
                    ),
                  ),
                  const Gap(8),
                ],
              ),
            const SliverToBoxAdapter(
              child: Gap(16),
            ),
            const ContactsListSliver(),
            SliverList.list(
              children: [
                KwButton.outlinedPrimary(
                  fit: KwButtonFit.shrinkWrap,
                  label: 'add_more'.tr(),
                  leftIcon: Assets.images.icons.add,
                  onPressed: () {
                    context.read<EmergencyContactsBloc>().add(
                          AddNewContactEvent(),
                        );
                  },
                ),
                const Gap(8),
                Text(
                  'add_additional_contact'.tr(),
                  style: Theme.of(context).textTheme.bodySmall,
                ),
                const Gap(120),
              ],
            ),
          ],
        ),
      ),
      bottomNavigationBar: SafeArea(
        top: false,
        bottom: true,
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: BottomControlButton(
            isInEditMode: isInEditMode,
            footerActionName:
                isInEditMode ? 'save_changes'.tr() : 'save_and_continue'.tr(),
          ),
        ),
      ),
    );
  }
}
