// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart';
import 'package:syncrow_app/features/scene/model/scene_static_function.dart';
import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart';

class AlertDialogFunctionsOperationsBody extends StatefulWidget {
  AlertDialogFunctionsOperationsBody({
    super.key,
    this.functionValue,
    required this.taskItem,
    required this.isAutomation,
  });

  dynamic functionValue;
  final SceneStaticFunction taskItem;
  final bool isAutomation;

  @override
  State<AlertDialogFunctionsOperationsBody> createState() =>
      _AlertDialogFunctionsOperationsBodyState();
}

class _AlertDialogFunctionsOperationsBodyState
    extends State<AlertDialogFunctionsOperationsBody> {
  @override
  didChangeDependencies() {
    super.didChangeDependencies();
    if (widget.isAutomation) {
      final automationTempTasksList =
          context.read<CreateSceneBloc>().automationTempTasksList;

      if (automationTempTasksList.isNotEmpty) {
        for (var element in automationTempTasksList) {
          if (element.code == widget.taskItem.code) {
            groupValue = element.functionValue;
          } else {
            context.read<CreateSceneBloc>().add(RemoveFromSelectedValueById(
                code: widget.taskItem.code, isAutomation: widget.isAutomation));
          }
        }
      }

      if (widget.functionValue != null) {
        setState(() {
          groupValue = widget.functionValue;
        });
      }
    } else {
      final tempTaskList = context.read<CreateSceneBloc>().tempTasksList;

      if (tempTaskList.isNotEmpty) {
        for (var element in tempTaskList) {
          if (element.code == widget.taskItem.code) {
            groupValue = element.functionValue;
          } else {
            context
                .read<CreateSceneBloc>()
                .add(RemoveFromSelectedValueById(code: widget.taskItem.code));
          }
        }
      }

      if (widget.functionValue != null) {
        setState(() {
          groupValue = widget.functionValue;
        });
      }
    }
  }

  dynamic groupValue;

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ...widget.taskItem.operationalValues.map(
            (operation) => BlocBuilder<CreateSceneBloc, CreateSceneState>(
              builder: (context, state) {
                return SceneListTile(
                  iconsSize: 25,
                  minLeadingWidth: 15,
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  assetPath: operation.icon,
                  assetHeaderValue: operation.iconValue,
                  titleString: operation.description.toString(),
                  textAlign: TextAlign.start,
                  trailingWidget: Radio(
                    value: operation.value,
                    groupValue: (state is SelectedTaskValueState)
                        ? state.value
                        : groupValue,
                    onChanged: (value) {
                      setState(() {
                        groupValue = value;
                      });
                      context.read<CreateSceneBloc>().add(SelectedValueEvent(
                          value: value!,
                          code: widget.taskItem.code,
                          isAutomation: widget.isAutomation));
                    },
                  ),
                  onPressed: () {
                    setState(() {
                      groupValue = operation.value;
                    });
                    context.read<CreateSceneBloc>().add(SelectedValueEvent(
                        value: groupValue,
                        code: widget.taskItem.code,
                        isAutomation: widget.isAutomation));
                  },
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
