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/device_functions.dart';
import 'package:syncrow_web/pages/routines/models/pc/energy_clamp_functions.dart';
import 'package:syncrow_web/pages/routines/widgets/custom_routines_textbox.dart';
import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/power_clamp_enargy/enargy_operational_values_list.dart';

class EnergyValueSelectorWidget extends StatelessWidget {
  final String selectedFunction;
  final DeviceFunctionData functionData;
  final List<EnergyClampFunctions> functions;
  final AllDevicesModel? device;
  final String dialogType;
  final bool removeComparators;

  const EnergyValueSelectorWidget({
    required this.selectedFunction,
    required this.functionData,
    required this.functions,
    required this.device,
    required this.dialogType,
    required this.removeComparators,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final selectedFn = functions.firstWhere((f) => f.code == selectedFunction);
    final values = selectedFn.getOperationalValues();
    final step = selectedFn.step;
    final _unit = selectedFn.unit ?? '';
    final (double, double) sliderRange =
        (selectedFn.min ?? 0.0, selectedFn.max ?? 100.0);

    if (_isSliderFunction(selectedFunction)) {
      return CustomRoutinesTextbox(
        withSpecialChar: true,
        currentCondition: functionData.condition,
        dialogType: dialogType,
        sliderRange: sliderRange,
        displayedValue: functionData.value,
        initialValue: functionData.value ?? 0.0,
        onConditionChanged: (condition) => context.read<FunctionBloc>().add(
              AddFunction(
                functionData: DeviceFunctionData(
                  entityId: device?.uuid ?? '',
                  functionCode: selectedFunction,
                  operationName: functionData.operationName,
                  condition: condition,
                  value: functionData.value ?? 0,
                ),
              ),
            ),
        onTextChanged: (value) => context.read<FunctionBloc>().add(
              AddFunction(
                functionData: DeviceFunctionData(
                  entityId: device?.uuid ?? '',
                  functionCode: selectedFunction,
                  operationName: functionData.operationName,
                  value: value,
                  condition: functionData.condition,
                ),
              ),
            ),
        unit: _unit,
        dividendOfRange: 1,
        stepIncreaseAmount: step!,
      );
    }

    return EnergyOperationalValuesList(
      values: values,
      selectedValue: functionData.value,
      device: device,
      operationName: selectedFn.operationName,
      selectCode: selectedFunction,
    );
  }

  bool _isSliderFunction(String function) =>
      !['voltage_phase_seq'].contains(function);
}
