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/data/enums/state_status.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.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_loading_overlay.dart';
import 'package:krow/features/profile/schedule/domain/bloc/schedule_bloc.dart';
import 'package:krow/features/profile/schedule/presentation/widgets/schedule_calendar.dart';
import 'package:krow/features/profile/schedule/presentation/widgets/calendar_button_section_widget.dart';
import 'package:krow/features/profile/schedule/presentation/widgets/calendar_edit_slot.dart';
import 'package:krow/features/profile/schedule/presentation/widgets/calendar_footer_widget.dart';
import 'package:krow/features/profile/schedule/presentation/widgets/calendar_slot_list_widget.dart';

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

  final OverlayPortalController _controller = OverlayPortalController();

  bool _calendarBuildWhen(ScheduleState previous, ScheduleState current) {
    return previous.isInEditMode != current.isInEditMode ||
        previous.isScheduleAddMode != current.isScheduleAddMode ||
        previous.selectedDates != current.selectedDates ||
        previous.schedules != current.schedules;
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(titleText: 'schedule'.tr()),
      body: KwLoadingOverlay(
        controller: _controller,
        child: BlocListener<ScheduleBloc, ScheduleState>(
          listenWhen: (previous, current) => previous.status != current.status,
          listener: (context, state) {
            if (state.status == StateStatus.loading) {
              _controller.show();
            } else {
              _controller.hide();
            }
          },
          child: ListView(
            primary: false,
            padding: const EdgeInsets.symmetric(
              horizontal: 16,
              vertical: 16,
            ),
            children: [
              _buildPageTitle(),
              const Gap(12),
              Container(
                decoration: KwBoxDecorations.primaryLight8.copyWith(
                  borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(12),
                    topRight: Radius.circular(12),
                  ),
                ),
                child: BlocBuilder<ScheduleBloc, ScheduleState>(
                  buildWhen: _calendarBuildWhen,
                  builder: (context, state) {
                    return ScheduleCalendar(
                      disabled: state.isInEditMode || state.isScheduleAddMode,
                      selectedDates: state.selectedDates,
                      containsScheduleForDate: state.containsScheduleForDate,
                      onDateSelected: (dates) {
                        BlocProvider.of<ScheduleBloc>(context).add(
                          ScheduleEventSelectDates(dates: dates),
                        );
                      },
                    );
                  },
                ),
              ),
              BlocBuilder<ScheduleBloc, ScheduleState>(
                buildWhen: (previous, current) =>
                    previous.isInEditMode != current.isInEditMode ||
                    previous.selectedSchedules != current.selectedSchedules,
                builder: (context, state) {
                  if (state.isInEditMode) {
                    return const Column(
                      children: [
                        CalendarEditSlot(),
                        CalendarButtonSectionWidget()
                      ],
                    );
                  }
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      CalendarFooterWidget(
                        selectedDates: state.selectedDates,
                        existedSlots: state.selectedSchedules,
                      ),
                      CalendarSlotListWidget(
                        selectedSchedules: state.selectedSchedules,
                      ),
                    ],
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  RichText _buildPageTitle() {
    return RichText(
      text: TextSpan(
        children: [
          TextSpan(
            text: '${'set_your_availability'.tr()} ',
            style: AppTextStyles.bodySmallMed.copyWith(
              color: AppColors.blackBlack,
            ),
          ),
          TextSpan(
            text: 'mark_days_and_times'.tr(),
            style: AppTextStyles.bodySmallReg.copyWith(
              color: AppColors.blackGray,
            ),
          ),
        ],
      ),
    );
  }
}
