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/custom_routines_textbox.dart';
import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/ceiling_sensor/cps_slider_helpers.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return CustomRoutinesTextbox(
      withSpecialChar: true,
      currentCondition: selectedFunctionData.condition,
      dialogType: dialogType,
      sliderRange:
          CpsSliderHelpers.sliderRange(selectedFunctionData.functionCode),
      displayedValue: CpsSliderHelpers.displayText(
        value: selectedFunctionData.value,
        functionCode: selectedFunctionData.functionCode,
      ),
      initialValue: selectedFunctionData.value ?? 0,
      unit: CpsSliderHelpers.unit(selectedFunctionData.functionCode),
      onConditionChanged: (condition) => context.read<FunctionBloc>().add(
            AddFunction(
              functionData: DeviceFunctionData(
                entityId: device?.uuid ?? '',
                functionCode: selectedFunction,
                operationName: operationName,
                condition: condition,
                value: selectedFunctionData.value ?? 0,
              ),
            ),
          ),
      onTextChanged: (value) => context.read<FunctionBloc>().add(
            AddFunction(
              functionData: DeviceFunctionData(
                entityId: device?.uuid ?? '',
                functionCode: selectedFunction,
                operationName: operationName,
                value: double.parse(value.toStringAsFixed(2)),
                condition: selectedFunctionData.condition,
              ),
            ),
          ),
      dividendOfRange: CpsSliderHelpers.dividendOfRange(
        selectedFunctionData.functionCode,
      ),
      stepIncreaseAmount: step,
    );
  }
}
