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/application/common/bool_extension.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/data/enums/state_status.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/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_loading_overlay.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_option_selector.dart';
import 'package:krow/features/profile/inclusive/domain/bloc/inclusive_info_bloc.dart';
import 'package:krow/features/profile/inclusive/presentation/widgets/accessibility_details_widget.dart';

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

  final bool isInEditMode;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(
        titleText: 'inclusive'.tr(),
        showNotification: false,
      ),
      body: ScrollLayoutHelper(
        padding: const EdgeInsets.all(16),
        upperWidget: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Gap(4),
             Text(
              'inclusive_information'.tr(),
              style: AppTextStyles.headingH1,
              textAlign: TextAlign.start,
            ),
            const Gap(8),
            Text(
              'providing_optional_information'.tr(),
              style: AppTextStyles.bodyMediumReg.copyWith(
                color: AppColors.blackGray,
              ),
              textAlign: TextAlign.start,
            ),
             Padding(
              padding: const EdgeInsets.only(bottom: 16, top: 24),
              child: Text(
                'specific_accommodations_question'.tr(),
                style: AppTextStyles.headingH3,
              ),
            ),
            BlocSelector<InclusiveInfoBloc, InclusiveInfoState, bool?>(
              selector: (state) => state.areAccommodationsRequired,
              builder: (context, areAccommodationsRequired) {
                return KwOptionSelector(
                  selectedIndex: areAccommodationsRequired?.toInt(),
                  selectedColor: AppColors.blackDarkBgBody,
                  itemBorder: const Border.fromBorderSide(
                    BorderSide(color: AppColors.grayStroke),
                  ),
                  items:  ['yes'.tr(), 'no'.tr()],
                  onChanged: (index) {
                    context
                        .read<InclusiveInfoBloc>()
                        .add(ToggleRequiresAccommodations(index));
                  },
                );
              },
            ),
            const Gap(16),
            const AccessibilityDetailsWidget(),
            const Gap(90),
          ],
        ),
        lowerWidget: BlocConsumer<InclusiveInfoBloc, InclusiveInfoState>(
          buildWhen: (previous, current) =>
              previous.status != current.status ||
              previous.isFilled != current.isFilled,
          listenWhen: (previous, current) => previous.status != current.status,
          listener: (context, state) {
            if (state.status == StateStatus.success) {
              if (isInEditMode) {
                Navigator.pop(context);
              } else {
                context.router.push(
                  AddressRoute(isInEditMode: false),
                );
              }
            }
          },
          builder: (context, state) {
            return KwLoadingOverlay(
              shouldShowLoading: state.status == StateStatus.loading,
              child: KwButton.primary(
                label: isInEditMode ? 'save_changes'.tr() : 'save_and_continue'.tr(),
                height: 52,
                disabled: !state.isFilled,
                onPressed: () {
                  context
                      .read<InclusiveInfoBloc>()
                      .add(SaveInclusiveInfoChanges());
                },
              ),
            );
          },
        ),
      ),
    );
  }
}
