import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/data/models/event/business_member_model.dart';
import 'package:krow/core/presentation/gen/assets.gen.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/features/create_event/presentation/create_shift_details_section/bloc/create_shift_details_bloc.dart';
import 'package:krow/features/create_event/presentation/create_shift_details_section/widgets/select_contact_popup.dart';

class ShiftContactsWidget extends StatelessWidget {
  final List<BusinessMemberModel> allContacts;
  final List<BusinessMemberModel> selectedContacts;

  const ShiftContactsWidget(
      {super.key, required this.allContacts, required this.selectedContacts});

  @override
  Widget build(BuildContext context) {

    if (selectedContacts.isEmpty) {
      return buildSelectContactPopup(context);
    }
    return Container(
        padding: const EdgeInsets.only(top: 12, left: 12, right: 12),
        margin: const EdgeInsets.only(top: 12),
        decoration: KwBoxDecorations.primaryLight12,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Shift Contact',
                style: AppTextStyles.bodyTinyReg
                    .copyWith(color: AppColors.blackGray)),
            for (var item in selectedContacts)
              _buildContactItem(context, item, selectedContacts),
            buildSelectContactPopup(context),
          ],
        ));
  }

  Widget buildSelectContactPopup(BuildContext context) {
    return SelectContactPopup(
      key: ObjectKey(selectedContacts),
      contacts: allContacts
          .where((element) => !selectedContacts.contains(element))
          .toList(),
      onSelect: (contact) {
        BlocProvider.of<CreateShiftDetailsBloc>(context)
            .add(CreateShiftSelectContactEvent(contact));
      },
      child: Container(
        margin: const EdgeInsets.only(top: 12, bottom: 12),
        height: 20,
        child: Row(
          children: [
            Assets.images.icons.profileAdd.svg(),
            const Gap(8),
            Text(
              'Shift Contact',
              style: AppTextStyles.bodyMediumMed
                  .copyWith(color: AppColors.primaryBlue),
            ),
          ],
        ),
      ),
    );
  }

  Container _buildContactItem(BuildContext context, BusinessMemberModel item,
      List<BusinessMemberModel> selectedContacts) {
    return Container(
      margin: const EdgeInsets.only(top: 12),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            height: 36,
            width: 36,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(18), color: Colors.grey),
          ),
          const Gap(8),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '${item.firstName} ${item.lastName}',
                style: AppTextStyles.bodyMediumMed,
              ),
              const Gap(2),
              Text(
                item.authInfo?.phone ?? '',
                style: AppTextStyles.bodySmallReg
                    .copyWith(color: AppColors.blackGray),
              ),
            ],
          ),
          const Spacer(),
          SelectContactPopup(
            key: ObjectKey(selectedContacts),
            contacts: allContacts
                .where((element) => !selectedContacts.contains(element))
                .toList(),
            onSelect: (contact) {
              BlocProvider.of<CreateShiftDetailsBloc>(context)
                  .add(CreateShiftRemoveContactEvent(item));

              BlocProvider.of<CreateShiftDetailsBloc>(context)
                  .add(CreateShiftSelectContactEvent(contact));
            },
            child: Container(
              height: 34,
              padding: const EdgeInsets.symmetric(horizontal: 12),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: AppColors.grayWhite,
                  borderRadius: BorderRadius.circular(17),
                  border: Border.all(color: AppColors.grayTintStroke)),
              child: Row(
                children: [
                  Center(
                    child: Assets.images.icons.edit.svg(
                      width: 16,
                      height: 16,
                      colorFilter: const ColorFilter.mode(
                          AppColors.blackBlack, BlendMode.srcIn),
                    ),
                  ),
                  const Gap(4),
                  const Text(
                    'Edit',
                    style: AppTextStyles.bodyMediumMed,
                  )
                ],
              ),
            ),
          ),
          const Gap(4),
          GestureDetector(
            onTap: () {
              BlocProvider.of<CreateShiftDetailsBloc>(context)
                  .add(CreateShiftRemoveContactEvent(item));
            },
            child: Container(
              height: 34,
              width: 34,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: AppColors.grayWhite,
                  borderRadius: BorderRadius.circular(17),
                  border: Border.all(color: AppColors.grayTintStroke)),
              child: Center(
                child: Assets.images.icons.delete.svg(
                  width: 16,
                  height: 16,
                  colorFilter: const ColorFilter.mode(
                      AppColors.statusError, BlendMode.srcIn),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
