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_popup_menu.dart';
import 'package:krow/features/shifts/domain/blocs/complete_dialog/shift_complete_dialog_bloc.dart';
import 'package:krow/features/shifts/domain/blocs/complete_dialog/shift_complete_dialog_event.dart';

const reasons = {
  'unpredictable_workflows': 'unpredictable_workflows',
  'poor_time_management': 'poor_time_management',
  'lack_of_coverage_or_short_staff': 'lack_of_coverage_or_short_staff',
  'no_break_area': 'no_break_area',
  'other': 'other',
};

class CompleteReasonInput extends StatelessWidget {
  final String? selectedReason;

  const CompleteReasonInput({super.key, required this.selectedReason});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: buildReasonInput(context, selectedReason),
    );
  }

  List<Widget> buildReasonInput(BuildContext context, String? selectedReason) {
    return [
      const Gap(24),
      Row(
        children: [
          const Gap(16),
          Text(
            'reason'.tr(),
            style:
                AppTextStyles.bodyTinyReg.copyWith(color: AppColors.blackGray),
          ),
        ],
      ),
      const Gap(4),
      KwPopupMenu(
          horizontalPadding: 40,
          fit: KwPopupMenuFit.expand,
          customButtonBuilder: (context, isOpened) {
            return _buildMenuButton(isOpened, selectedReason);
          },
          menuItems: [
            ...reasons.entries
                .map((e) => _buildMenuItem(context, e, selectedReason ?? ''))
          ])
    ];
  }

  Container _buildMenuButton(bool isOpened, String? selectedReason) {
    return Container(
      height: 48,
      padding: const EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: AppColors.grayWhite,
        borderRadius: BorderRadius.circular(24),
        border: Border.all(
            color: isOpened ? AppColors.bgColorDark : AppColors.grayTintStroke,
            width: 1),
      ),
      child: Row(
        children: [
          Expanded(
              child: Text(
            reasons[selectedReason]?.tr() ?? 'select_reason_from_list'.tr(),
            style: AppTextStyles.bodyMediumReg.copyWith(
                color: selectedReason == null
                    ? AppColors.blackGray
                    : AppColors.blackBlack),
          )),
        ],
      ),
    );
  }

  KwPopupMenuItem _buildMenuItem(BuildContext context,
      MapEntry<String, String> entry, String selectedReason) {
    return KwPopupMenuItem(
      title: entry.value.tr(),
      onTap: () {
        context.read<CompleteDialogBloc>().add(SelectReason(entry.key));
      },
      icon: Container(
        height: 16,
        width: 16,
        decoration: BoxDecoration(
          color: selectedReason != entry.key ? null : AppColors.bgColorDark,
          shape: BoxShape.circle,
          border: selectedReason == entry.key
              ? null
              : Border.all(color: AppColors.grayTintStroke, width: 1),
        ),
        child: selectedReason == entry.key
            ? Center(
                child: Assets.images.icons.check.svg(
                height: 10,
                width: 10,
                colorFilter: const ColorFilter.mode(
                    AppColors.grayWhite, BlendMode.srcIn),
              ))
            : null,
      ),
      textStyle: AppTextStyles.bodySmallMed,
    );
  }
}
