import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.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/domain/bloc/create_event_bloc.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CreateEventBloc, CreateEventState>(
      builder: (context, state) {
        var allAddons = state.addons;
        var selectedAddons = state.entity.addons;
        if(context.read<CreateEventBloc>().state.addons.isEmpty) {
          return const SizedBox.shrink();
        }
        return Column(
          children: [
            const Divider(
              height: 0,
              color: AppColors.grayStroke,
            ),
            for (var addon in allAddons)
              _addonStrokeItem(
                title: addon.name ?? '',
                enabled: selectedAddons
                        ?.any((selected) => selected.id == addon.id) ??
                    false,
                onTap: () {
                  BlocProvider.of<CreateEventBloc>(context)
                      .add(CreateEventToggleAddon(addon));
                },
              ),
            const Gap(12),
            const Divider(
              height: 0,
              color: AppColors.grayStroke,
            ),
          ],
        );
      },
    );
  }

  _addonStrokeItem(
      {required String title,
      required bool enabled,
      required VoidCallback onTap}) {
    return Padding(
      padding: const EdgeInsets.only(top: 12),
      child: Row(
        children: [
          Expanded(
              child: Text(
            title,
            style: AppTextStyles.bodyMediumMed,
          )),
          CupertinoSwitch(
            value: enabled,
            onChanged: (_) {
              onTap();
            },
            activeTrackColor: AppColors.bgColorDark,
          )
        ],
      ),
    );
  }
}
