import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/routines/bloc/functions_bloc/functions_bloc_bloc.dart';
import 'package:syncrow_web/pages/routines/models/ceiling_presence_sensor_functions.dart';
import 'package:syncrow_web/pages/routines/models/device_functions.dart';
import 'package:syncrow_web/pages/routines/widgets/routine_dialog_selection_list_tile.dart';

class CpsDialogValueSelector extends StatelessWidget {
  const CpsDialogValueSelector({
    required this.operations,
    required this.selectedFunction,
    required this.selectedFunctionData,
    required this.cpsFunctions,
    required this.device,
    required this.operationName,
    super.key,
  });

  final List<CpsOperationalValue> operations;
  final String selectedFunction;
  final DeviceFunctionData? selectedFunctionData;
  final List<CpsFunctions> cpsFunctions;
  final AllDevicesModel? device;
  final String operationName;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: operations.length,
      itemBuilder: (context, index) {
        final operation = operations[index];
        final isSelected = selectedFunctionData?.value == operation.value;
        return RoutineDialogSelectionListTile(
          iconPath: operation.icon,
          description: operation.description,
          isSelected: isSelected,
          onTap: () {
            if (!isSelected) {
              context.read<FunctionBloc>().add(
                    AddFunction(
                      functionData: DeviceFunctionData(
                        entityId: device?.uuid ?? '',
                        functionCode: selectedFunction,
                        operationName: operationName,
                        value: operation.value,
                        condition: selectedFunctionData?.condition,
                        valueDescription: selectedFunctionData?.valueDescription,
                      ),
                    ),
                  );
            }
          },
        );
      },
    );
  }
}
