import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/features/create_event/presentation/create_role_section/create_role_widget.dart';
import 'package:krow/features/create_event/presentation/create_shift_details_section/bloc/create_shift_details_bloc.dart';

class CreateRolesList extends StatefulWidget {
  final String id;

  const CreateRolesList({super.key, required this.id});

  @override
  State<CreateRolesList> createState() => _CreateRolesListState();
}

class _CreateRolesListState extends State<CreateRolesList> {
  final Map<String, GlobalKey> roleKeys = {};

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CreateShiftDetailsBloc, CreateShiftDetailsState>(
      builder: (context, state) {
        return ListView.builder(
          padding: const EdgeInsets.all(0),
          shrinkWrap: true,
          primary: false,
          itemCount: state.roles.length,
          itemBuilder: (context, index) {
            final roleId = state.roles[index].id;
            if (roleKeys[roleId] == null) {
              roleKeys[roleId] = GlobalKey();
            }
            return BlocProvider.value(
              value: state.roles[index].bloc,
              child: CreateRoleDetailsWidget(
                  key: roleKeys[roleId], id: state.roles[index].id),
            );
          },
        );
      },
    );
  }
}
