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_button.dart';
import 'package:krow/core/presentation/widgets/kw_time_slot.dart';
import 'package:krow/features/profile/schedule/domain/bloc/schedule_bloc.dart';
import 'package:krow/features/profile/schedule/domain/entities/schedule_slot.dart';

class CalendarEditSlot extends StatelessWidget {
  const CalendarEditSlot({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ScheduleBloc, ScheduleState>(
      buildWhen: (previous, current) {
        return previous.scheduleErrorText != current.scheduleErrorText ||
            previous.tempSchedules != current.tempSchedules;
      },
      builder: (context, state) {
        final daySchedule = state.tempSchedules.first;

        return Container(
          padding: const EdgeInsets.all(12),
          decoration: const BoxDecoration(
            color: AppColors.graySecondaryFrame,
            borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(12),
              bottomRight: Radius.circular(12),
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Text(
                'what_hours_available'.tr(),
                style: AppTextStyles.bodyMediumMed,
              ),
              const Divider(
                color: AppColors.grayTintStroke,
              ),
              AnimatedSize(
                duration: const Duration(milliseconds: 300),
                alignment: Alignment.topCenter,
                child: Column(
                  children: [
                    for (int i = 0; i < daySchedule.slots.length; i++)
                      _CalendarEditSlotItemWidget(
                        slot: daySchedule.slots[i],
                        slotIndex: i,
                      ),
                  ],
                ),
              ),
              if (state.scheduleErrorText.isNotEmpty)
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 4),
                  child: Text(
                    state.scheduleErrorText,
                    style: AppTextStyles.bodyTinyReg.copyWith(
                      color: AppColors.statusError,
                    ),
                  ),
                ),
              const Gap(16),
              KwButton.secondary(
                label: 'add_slot'.tr(),
                leftIcon: Assets.images.icons.add,
                onPressed: () {
                  BlocProvider.of<ScheduleBloc>(context).add(
                    const ScheduleEventAddSlot(),
                  );
                },
              )
            ],
          ),
        );
      },
    );
  }
}

class _CalendarEditSlotItemWidget extends StatelessWidget {
  const _CalendarEditSlotItemWidget({
    required this.slot,
    required this.slotIndex,
  });

  final ScheduleSlot slot;
  final int slotIndex;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const Gap(8),
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Expanded(
              child: KwTimeSlotInput(
                label: 'start_time'.tr(),
                initialValue: slot.startTime,
                onChange: (DateTime start) {
                  BlocProvider.of<ScheduleBloc>(context).add(
                    ScheduleEventEditSlot(
                      start: start,
                      slot: slot,
                      slotIndex: slotIndex,
                    ),
                  );
                },
              ),
            ),
            const Gap(8),
            Expanded(
              child: KwTimeSlotInput(
                label: 'end_time'.tr(),
                initialValue: slot.endTime,
                onChange: (DateTime end) {
                  BlocProvider.of<ScheduleBloc>(context).add(
                    ScheduleEventEditSlot(
                      end: end,
                      slot: slot,
                      slotIndex: slotIndex,
                    ),
                  );
                },
              ),
            ),
            const Gap(8),
            KwButton.secondary(
              onPressed: () {
                BlocProvider.of<ScheduleBloc>(context).add(
                  ScheduleEventRemoveSlot(
                    slot: slot,
                    slotIndex: slotIndex,
                  ),
                );
              },
              leftIcon: Assets.images.icons.x,
              fit: KwButtonFit.circular,
            )
          ],
        ),
      ],
    );
  }
}
