import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/blocs/non_bookable_spaces_bloc/non_bookaable_spaces_bloc.dart';

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

  @override
  State<WeekDaysCheckboxRow> createState() => _WeekDaysCheckboxRowState();
}

class _WeekDaysCheckboxRowState extends State<WeekDaysCheckboxRow> {
  final Map<String, bool> _daysChecked = {
    'Mon': false,
    'Tue': false,
    'Wed': false,
    'Thu': false,
    'Fri': false,
    'Sat': false,
    'Sun': false,
  };

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: _daysChecked.entries.map((entry) {
        return Expanded(
          child: Row(
            children: [
              Expanded(
                child: Checkbox(
                  value: entry.value,
                  onChanged: (newValue) {
                    setState(() {
                      _daysChecked[entry.key] = newValue ?? false;
                      final selectedDays = _daysChecked.entries
                          .where((e) => e.value)
                          .map((e) => e.key)
                          .toList();

                      for (var space in context
                          .read<NonBookableSpacesBloc>()
                          .selectedBookableSpaces) {
                        space.spaceConfig!.bookableDays = selectedDays;
                      }
                    });

                    context
                        .read<NonBookableSpacesBloc>()
                        .add(CheckConfigurValidityEvent());
                  },
                ),
              ),
              Expanded(
                  child: Text(
                entry.key,
                style: const TextStyle(fontSize: 10),
              )),
            ],
          ),
        );
      }).toList(),
    );
  }
}
