import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.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/device_functions.dart';
import 'package:syncrow_web/pages/routines/models/pc/enrgy_clamp_operational_value.dart';

class EnergyOperationalValuesList extends StatelessWidget {
  final List<EnergyClampOperationalValue> values;
  final dynamic selectedValue;
  final AllDevicesModel? device;
  final String operationName;
  final String selectCode;

  const EnergyOperationalValuesList({
    required this.values,
    required this.selectedValue,
    required this.device,
    required this.operationName,
    required this.selectCode,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: const EdgeInsets.all(20),
      itemCount: values.length,
      itemBuilder: (context, index) => _buildValueItem(context, values[index]),
    );
  }

  Widget _buildValueItem(
      BuildContext context, EnergyClampOperationalValue value) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildValueIcon(context, value),
          Expanded(child: _buildValueDescription(value)),
          _buildValueRadio(context, value),
        ],
      ),
    );
  }

  Widget _buildValueIcon(context, EnergyClampOperationalValue value) {
    return Column(
      children: [
        SvgPicture.asset(value.icon, width: 25, height: 25),
      ],
    );
  }

  Widget _buildValueDescription(EnergyClampOperationalValue value) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      child: Text(value.description),
    );
  }

  Widget _buildValueRadio(context, EnergyClampOperationalValue value) {
    return Radio<dynamic>(
      value: value.value,
      groupValue: selectedValue,
      onChanged: (_) => _selectValue(context, value.value),
    );
  }

  void _selectValue(BuildContext context, dynamic value) {
    context.read<FunctionBloc>().add(
          AddFunction(
            functionData: DeviceFunctionData(
              entityId: device?.uuid ?? '',
              functionCode: selectCode,
              operationName: operationName,
              value: value,
            ),
          ),
        );
  }


}
