import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.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/features/profile/schedule/domain/entities/day_shedule.dart';
import 'package:krow/features/profile/schedule/presentation/widgets/shared_functions.dart';

class CalendarLegendWidget extends StatelessWidget {
  const CalendarLegendWidget({
    super.key,
    required this.selectedDates,
    required this.existedSlots,
  });

  final List<DateTime> selectedDates;
  final List<DaySchedule> existedSlots;

  @override
  Widget build(BuildContext context) {
    final selectedDatesOption = _getSelectedDatesOption();

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12),
      decoration: const BoxDecoration(
        color: AppColors.grayPrimaryFrame,
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(12),
          bottomRight: Radius.circular(12),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Divider(
            color: AppColors.grayTintStroke,
          ),
          const Gap(8),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                height: 16,
                width: 16,
                decoration: const BoxDecoration(
                  shape: BoxShape.circle,
                  color: AppColors.bgColorDark,
                ),
              ),
              const Gap(8),
               Text(
                'available'.tr(),
                style: AppTextStyles.bodyMediumReg,
              ),
              const Gap(32),
              Container(
                height: 16,
                width: 16,
                decoration: const BoxDecoration(
                  shape: BoxShape.circle,
                  color: AppColors.blackCaptionText,
                ),
              ),
              const Gap(8),
               Text(
                'not_available'.tr(),
                style: AppTextStyles.bodyMediumReg,
              ),
              const Spacer(),
              if (selectedDates.isEmpty)
                const SizedBox(
                  height: 34,
                )
              else
                Container(
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: AppColors.grayTintStroke,
                    ),
                    borderRadius: BorderRadius.circular(16),
                  ),
                  child: KwButton.secondary(
                    height: 32,
                    label: switch (selectedDatesOption) {
                      SelectedDatesOption.add => 'add'.tr(),
                      SelectedDatesOption.edit => 'edit'.tr(),
                      SelectedDatesOption.editAll => 'edit_all'.tr(),
                    },
                    onPressed: () => onEditButtonPress(
                      context,
                      selectedDatesOption: selectedDatesOption,
                      isWeeklySchedule: existedSlots.length == 1 &&
                          existedSlots.first.isWeekly,
                    ),
                    rightIcon: existedSlots.isNotEmpty
                        ? Assets.images.icons.edit
                        : Assets.images.icons.add,
                  ),
                ),
            ],
          ),
          const Gap(24),
        ],
      ),
    );
  }

  SelectedDatesOption _getSelectedDatesOption() {
    if (existedSlots.isEmpty) return SelectedDatesOption.add;

    return existedSlots.length > 1 || selectedDates.length > 1
        ? SelectedDatesOption.editAll
        : SelectedDatesOption.edit;
  }
}
