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/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/check_box.dart';
import 'package:krow/core/presentation/widgets/ui_kit/check_box_card.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/features/profile/working_area/domain/bloc/working_area_bloc.dart';

@RoutePage()
class WorkingAreaScreen extends StatelessWidget implements AutoRouteWrapper {
  final bool isInEditMode;

  const WorkingAreaScreen({super.key, this.isInEditMode = true});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(
        showNotification: isInEditMode,
        titleText: 'working_area'.tr(),
      ),
      body: BlocConsumer<WorkingAreaBloc, WorkingAreaState>(
        listenWhen: (previous, current) => previous.status != current.status,
        listener: (context, state) {
          if (state.status == StateStatus.success) {
            if (isInEditMode) {
              Navigator.pop(context);
            } else {
              context.router.push(
                RoleRoute(isInEditMode: false),
              );
            }
          }
        },
        builder: (context, state) {
          return KwLoadingOverlay(
            shouldShowLoading: state.status == StateStatus.loading,
            child: ScrollLayoutHelper(
              padding: isInEditMode? const EdgeInsets.symmetric(horizontal: 16):const EdgeInsets.all(16),
              upperWidget: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  if (isInEditMode)
                    Text(
                      'select_nearby'.tr(),
                      style: AppTextStyles.bodySmallReg
                          .copyWith(color: AppColors.blackGray),
                    ),
                  if (!isInEditMode) ...[
                    const Gap(20),
                     Text(
                      'where_u_can_work'.tr(),
                      style: AppTextStyles.headingH1,
                    ),
                    Text(
                      'define_the_zone'.tr(),
                      style: AppTextStyles.bodyMediumReg
                          .copyWith(color: AppColors.blackGray),
                    ),
                  ],
                  const Gap(24),
                  ...state.areas.map((item) => CheckBoxCard(
                        checkBoxStyle: CheckBoxStyle.black,
                        padding: const EdgeInsets.only(bottom: 8.0),
                        isChecked: item.selected,
                        title: item.name,
                        onTap: () {
                          context.read<WorkingAreaBloc>().add(CheckAreaEvent(
                              id: item.id, selected: !item.selected));
                        },
                      )),
                  const Gap(24),
                ],
              ),
              lowerWidget: KwButton.primary(
                  disabled: state.areas.every((element) => !element.selected),
                  label: isInEditMode ? 'save_changes'.tr() : 'save_and_continue'.tr(),
                  onPressed: () {
                    context
                        .read<WorkingAreaBloc>()
                        .add(SubmitWorkingAreaEvent());
                  }),
            ),
          );
        },
      ),
    );
  }

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