import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/routines/bloc/routine_bloc/routine_bloc.dart';
import 'package:syncrow_web/pages/routines/models/device_functions.dart';
import 'package:syncrow_web/pages/routines/widgets/dialog_footer.dart';
import 'package:syncrow_web/pages/routines/widgets/dialog_header.dart';

class DelayHelper {
  static Future<Map<String, dynamic>?> showDelayPickerDialog(
      BuildContext context, Map<String, dynamic> data) async {
    int hours = 0;
    int minutes = 0;

    return showDialog<Map<String, dynamic>?>(
      context: context,
      builder: (BuildContext context) {
        final routineBloc = context.read<RoutineBloc>();
        int totalSec = 0;

        final selectedFunctionData =
            routineBloc.state.selectedFunctions[data['uniqueCustomId']] ?? [];

        if (selectedFunctionData.isNotEmpty) {
          totalSec = selectedFunctionData[0].value;
          // Convert seconds to hours and minutes
          hours = totalSec ~/ 3600;
          minutes = (totalSec % 3600) ~/ 60;
        }
        return AlertDialog(
          contentPadding: EdgeInsets.zero,
          content: Container(
            width: 600,
            height: 300,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(20),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const DialogHeader('Select Delay Duration'),
                Expanded(
                  child: CupertinoTimerPicker(
                    mode: CupertinoTimerPickerMode.hm,
                    initialTimerDuration: Duration(hours: hours, minutes: minutes),
                    onTimerDurationChanged: (Duration newDuration) {
                      hours = newDuration.inHours;
                      minutes = newDuration.inMinutes % 60;
                    },
                  ),
                ),
                DialogFooter(
                  onCancel: () {
                    Navigator.of(context).pop();
                  },
                  onConfirm: () {
                    int totalSeconds = (hours * 3600) + (minutes * 60);
                    context.read<RoutineBloc>().add(AddFunctionToRoutine(
                          [
                            DeviceFunctionData(
                              entityId: 'delay',
                              functionCode: 'delay',
                              operationName: 'Delay',
                              value: totalSeconds,
                            )
                          ],
                          data['uniqueCustomId'],
                        ));

                    Navigator.pop(context, {
                      'deviceId': 'delay',
                      'value': totalSeconds,
                    });
                  },
                  isConfirmEnabled: true,
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
