import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/features/create_event/domain/bloc/create_event_bloc.dart';
import 'package:krow/features/create_event/presentation/create_shift_details_section/create_shift_widget.dart';

class CreateShiftsList extends StatefulWidget {
  const CreateShiftsList({super.key});

  @override
  State<CreateShiftsList> createState() => _CreateShiftsListState();
}

class _CreateShiftsListState extends State<CreateShiftsList> {
  final Map<String, GlobalKey> shiftKeys = {};

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CreateEventBloc, CreateEventState>(
      builder: (context, state) {
        return SliverList.builder(
          itemCount: state.shifts.length,
          itemBuilder: (context, index) {
            final shiftId = state.shifts[index].id;
            if (shiftKeys[shiftId] == null) {
              shiftKeys[shiftId] = GlobalKey();
            }
            return BlocProvider.value(
              value: state.shifts[index].bloc,
              child: CreateShiftDetailsWidget(
                  key: shiftKeys[shiftId],
                  id: state.shifts[index].id,
                  expanded: state.shifts[index].bloc.state.isExpanded,
                  index: index),
            );
          },
        );
      },
    );
  }
}
