import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_event.dart';
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_state.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

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

  @override
  Widget build(BuildContext context) {

    return BlocBuilder<SmartDoorBloc, SmartDoorState>(
        builder: (context, state) {
      final smartDoorBloc = BlocProvider.of<SmartDoorBloc>(context);
      return DefaultContainer(
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
          child: Column(children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    onTap: () {
                      smartDoorBloc.add(const SetStartEndTimeEvent(val: true));
                    },
                    child: BodyMedium(
                      text: 'Start',
                      fontColor: smartDoorBloc.isStartEndTime == false
                          ? Colors.black
                          : Colors.blue,
                      fontSize: 18,
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      smartDoorBloc.add(const SetStartEndTimeEvent(val: false));
                    },
                    child: BodyMedium(
                      text: 'End',
                      fontColor: smartDoorBloc.isStartEndTime
                          ? Colors.black
                          : Colors.blue,
                      fontSize: 18,
                    ),
                  )
                ],
              ),
            ),
            const Divider(
              color: ColorsManager.graysColor,
            ),
          smartDoorBloc.isStartEndTime
               ?  Container(
              height: 110,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.time,
                initialDateTime:smartDoorBloc.startTime,
                onDateTimeChanged: (startTime) {
                  smartDoorBloc.add(ChangeTimeEvent(val: startTime, isStartEndTime: true));
                },
              )
            ):SizedBox(
              height: 110,
              child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.time,
                initialDateTime:smartDoorBloc.endTime,
                onDateTimeChanged: (endTime) {
                  smartDoorBloc.add(ChangeTimeEvent(val: endTime, isStartEndTime: false));
                },
              )
            ),
            const Divider(
              color: ColorsManager.graysColor,
            ),
            const SizedBox(height: 20),
            SizedBox(
                height: MediaQuery.of(context).size.height *0.10,
                child: ListView(
                  scrollDirection: Axis.horizontal,
                  children: smartDoorBloc.days.map((day) {
                    bool isSelected = smartDoorBloc.selectedDays.contains(day['key']);
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: InkWell(
                        onTap: () {
                          smartDoorBloc.add(ToggleDaySelectionEvent(key:day['key']!));
                        },
                        child: Container(
                          width: 70,
                          padding: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
                          decoration: BoxDecoration(
                            border: Border.all(
                            color: isSelected?
                            Colors.black:ColorsManager.grayColor
                            ),
                            color: Colors.transparent,
                            borderRadius: BorderRadius.circular(55),
                          ),
                          child: Center(
                            child: Text(
                              day['day']!,
                              style: TextStyle(
                                fontSize: 18,
                                color: isSelected ? Colors.black : ColorsManager.grayColor,
                              ),
                            ),
                          ),
                        ),
                      ),
                    );
                  }).toList(),
                )
                )
          ]));
    });
  }
}
