import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.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_header.dart';
import 'package:syncrow_web/pages/routines/widgets/dialog_footer.dart';
import 'package:syncrow_web/utils/constants/assets.dart';

class AutomationDialog extends StatefulWidget {
  final String automationName;
  final String automationId;
  final String uniqueCustomId;
  final String? passedAutomationActionExecutor;

  const AutomationDialog({
    super.key,
    required this.automationName,
    required this.automationId,
    required this.uniqueCustomId,
    this.passedAutomationActionExecutor,
  });

  @override
  State<AutomationDialog> createState() => _AutomationDialogState();
}

class _AutomationDialogState extends State<AutomationDialog> {
  String? selectedAutomationActionExecutor;

  @override
  void initState() {
    super.initState();
    List<DeviceFunctionData>? functions =
        context.read<RoutineBloc>().state.selectedFunctions[widget.uniqueCustomId];
    for (DeviceFunctionData data in functions ?? []) {
      if (data.entityId == widget.automationId) {
        selectedAutomationActionExecutor = data.value;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Container(
        width: 400,
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            DialogHeader(widget.automationName),
            const SizedBox(height: 16),
            ListTile(
              leading: SvgPicture.asset(Assets.acPower, width: 24, height: 24),
              title: const Text('Enable'),
              trailing: Radio<String?>(
                  value: 'rule_enable',
                  groupValue: selectedAutomationActionExecutor,
                  onChanged: (String? value) {
                    setState(() {
                      selectedAutomationActionExecutor = 'rule_enable';
                    });
                  }),
            ),
            ListTile(
              leading: SvgPicture.asset(Assets.acPowerOff, width: 24, height: 24),
              title: const Text('Disable'),
              trailing: Radio<String?>(
                value: 'rule_disable',
                groupValue: selectedAutomationActionExecutor,
                onChanged: (String? value) {
                  setState(() {
                    selectedAutomationActionExecutor = 'rule_disable';
                  });
                },
              ),
            ),
            const SizedBox(height: 16),
            DialogFooter(
              onConfirm: () {
                if (selectedAutomationActionExecutor != null) {
                  context.read<RoutineBloc>().add(
                        AddFunctionToRoutine(
                          [
                            DeviceFunctionData(
                              entityId: widget.automationId,
                              functionCode: 'automation',
                              value: selectedAutomationActionExecutor,
                              operationName: 'Automation',
                            ),
                          ],
                          widget.uniqueCustomId,
                        ),
                      );
                }
                Navigator.of(context).pop(true);
              },
              onCancel: () => Navigator.of(context).pop(),
              isConfirmEnabled: selectedAutomationActionExecutor != null,
              dialogWidth: 400,
            ),
          ],
        ),
      ),
    );
  }
}
