import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:syncrow_app/features/devices/view/widgets/garage_door/garagedialog.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 CreateGarageSchedule extends StatefulWidget {
  final List<Map<String, String>> days;
  final void Function(List<String> selectedDays)?
      selectDays; 
  final Function(DateTime) onDateTimeChanged;
  final void Function(bool isOn)? onToggleChanged; 

  const CreateGarageSchedule({
    Key? key,
    required this.days,
    required this.selectDays,
    required this.onDateTimeChanged,
    this.onToggleChanged, 
  }) : super(key: key);

  @override
  _CreateGarageScheduleState createState() => _CreateGarageScheduleState();
}

class _CreateGarageScheduleState extends State<CreateGarageSchedule> {
  List<String> selectedDays = []; 
  bool isOn = true; 
  String selectedControlOption = 'Open';

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 5),
      child: Column(
        children: <Widget>[
          const Divider(
            color: ColorsManager.greyColor,
          ),
          SizedBox(
            height: 110,
            child: CupertinoDatePicker(
              mode: CupertinoDatePickerMode.time,
              initialDateTime: DateTime.now(),
              onDateTimeChanged: widget.onDateTimeChanged,
            ),
          ),
          const Divider(
            color: ColorsManager.greyColor,
          ),
          const SizedBox(height: 20),
          SizedBox(
            height: MediaQuery.of(context).size.height * 0.08,
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: widget.days.map((day) {
                bool isSelected = selectedDays.contains(day['day']);
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        if (isSelected) {
                          selectedDays.remove(day['day']);
                        } else {
                          selectedDays.add(day['day']!); 
                        }
                        if (widget.selectDays != null) {
                          widget.selectDays!(selectedDays);
                        }
                      });
                    },
                    child: Container(
                      width: 50,
                      padding: const EdgeInsets.symmetric(
                          vertical: 8, horizontal: 8),
                      decoration: BoxDecoration(
                        border: Border.all(
                          color: isSelected
                              ? Colors.black
                              : ColorsManager.grayColor,
                        ),
                        color: Colors.transparent,
                        borderRadius: BorderRadius.circular(30),
                      ),
                      child: Center(
                        child: Text(
                          day['day']!,
                          style: TextStyle(
                            fontSize: 13,
                            color: isSelected
                                ? Colors.black
                                : ColorsManager.grayColor,
                          ),
                        ),
                      ),
                    ),
                  ),
                );
              }).toList(),
            ),
          ),
          const SizedBox(height: 20),
          DefaultContainer(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 50,
                  child: ListTile(
                    contentPadding: EdgeInsets.zero,
                    leading: const BodyMedium(
                      text: 'Notification',
                      fontWeight: FontWeight.normal,
                    ),
                    trailing: Transform.scale(
                        scale: .8,
                        child: CupertinoSwitch(
                          value: true,
                          onChanged: (value) {
                          },
                          applyTheme: true,
                        )),
                  ),
                ),
                const Divider(
                  color: ColorsManager.graysColor,
                ),
                SizedBox(
                  height: 50,
                  child: InkWell(
                    onTap: () {
                      showDialog(
                        context: context,
                        builder: (context) {
                          return GarageDialog(
                            label2: 'Close',
                            initialSelectedLabel: selectedControlOption,
                            cancelTab: () {
                              Navigator.of(context).pop();
                            },
                            confirmTab: () {
                              setState(() {});
                              if (selectedControlOption == "Open") {
                                widget.onToggleChanged!(true);
                              } else if (selectedControlOption == 'Close') {
                                widget.onToggleChanged!(false);
                              }

                              Navigator.of(context).pop();
                            },
                            title: 'Control',
                            label1: 'Open',
                            onTapLabel1: (selected) {
                              setState(() {
                                selectedControlOption = 'Open';
                              });
                            },
                            onTapLabel2: (selected) {
                              setState(() {
                                selectedControlOption = 'Close';
                              });
                            },
                          );
                        },
                      );
                    },
                    child: ListTile(
                        contentPadding: EdgeInsets.zero,
                        leading: const BodyMedium(
                          text: 'Control',
                          fontWeight: FontWeight.normal,
                        ),
                        trailing: SizedBox(
                          width: 70,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              BodyMedium(
                                fontColor: ColorsManager.textGray,
                                text: selectedControlOption,
                                fontWeight: FontWeight.normal,
                              ),
                              const Icon(
                                Icons.arrow_forward_ios,
                                color: ColorsManager.textGray,
                                size: 17,
                              )
                            ],
                          ),
                        )),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
