import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/common/date_time_widget.dart';
import 'package:syncrow_web/pages/visitor_password/bloc/visitor_password_bloc.dart';
import 'package:syncrow_web/pages/visitor_password/bloc/visitor_password_event.dart';
import 'package:syncrow_web/pages/visitor_password/bloc/visitor_password_state.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';

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

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return BlocBuilder<VisitorPasswordBloc, VisitorPasswordState>(
        builder: (context, state) {
      final visitorBloc = BlocProvider.of<VisitorPasswordBloc>(context);
      return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          // Wrap the Row in a SingleChildScrollView to handle overflow
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: visitorBloc.days.map((day) {
                return Container(
                  width: 70, // Adjust width as needed
                  margin: EdgeInsets.all(5),
                  child: CheckboxListTile(
                    contentPadding: EdgeInsets.zero,
                    title: Text(
                      day['day']!,
                      style: TextStyle(
                        fontSize: 10,
                        color: visitorBloc.selectedDays.contains(day['key'])
                            ? Colors.black
                            : ColorsManager.blackColor,
                      ),
                    ),
                    value: visitorBloc.selectedDays.contains(day['key']),
                    onChanged: (bool? value) {
                      if (value != null) {
                        visitorBloc
                            .add(ToggleDaySelectionEvent(key: day['key']!));
                      }
                    },
                  ),
                );
              }).toList(),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: DateTimeWebWidget(
              icon: Assets.timeIcon,
              isRequired: false,
              title: '',
              size: size,
              endTime: () {
                visitorBloc
                    .add(SelectTimeEvent(context: context, isEffective: false));
                Future.delayed(const Duration(milliseconds: 500), () {
                  visitorBloc.add(ChangeTimeEvent(
                      val: visitorBloc.endTime, isStartEndTime: true));
                });
              },
              startTime: () {
                Future.delayed(const Duration(milliseconds: 500), () {
                  visitorBloc.add(ChangeTimeEvent(
                      val: visitorBloc.endTime, isStartEndTime: true));
                });
                visitorBloc
                    .add(SelectTimeEvent(context: context, isEffective: true));
              },
              firstString: visitorBloc.effectiveTime,
              secondString: visitorBloc.expirationTime,
            ),
          ),
          const SizedBox(height: 20),
        ],
      );
    });
  }
}
