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/wps/wps_functions.dart';
import 'package:syncrow_web/pages/routines/widgets/custom_routines_textbox.dart';
import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/wall_sensor/wps_operational_values_list.dart';

class WpsValueSelectorWidget extends StatelessWidget {
  final String selectedFunction;
  final DeviceFunctionData functionData;
  final List<WpsFunctions> wpsFunctions;
  final AllDevicesModel? device;
  final String dialogType;
  final bool removeComparators;

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

  @override
  Widget build(BuildContext context) {
    final selectedFn =
        wpsFunctions.firstWhere((f) => f.code == selectedFunction);
    final values = selectedFn.getOperationalValues();

    if (_isSliderFunction(selectedFunction)) {
      return CustomRoutinesTextbox(
        withSpecialChar: true,
        currentCondition: functionData.condition,
        dialogType: dialogType,
        sliderRange: sliderRange,
        displayedValue: getDisplayText,
        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.toInt(),
                  condition: functionData.condition,
                ),
              ),
            ),
        unit: _unit,
        dividendOfRange: 1,
        stepIncreaseAmount: _steps,
      );
    }

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

  bool _isSliderFunction(String function) =>
      ['dis_current', 'presence_time', 'illuminance_value'].contains(function);

  (double, double) get sliderRange => switch (functionData.functionCode) {
        'presence_time' => (0, 65535),
        'dis_current' => (0, 600),
        'illuminance_value' => (0, 10000),
        _ => (200, 300),
      };

  String get getDisplayText {
    final intValue = int.tryParse('${functionData.value ?? ''}');
    return switch (functionData.functionCode) {
      'presence_time' => '${intValue ?? '0'}',
      'dis_current' => '${intValue ?? '0'}',
      'illuminance_value' => '${intValue ?? '0'}',
      _ => '$intValue',
    };
  }

  String get _unit => switch (functionData.functionCode) {
        'presence_time' => 'Min',
        'dis_current' => 'CM',
        'illuminance_value' => 'Lux',
        _ => '',
      };
  double get _steps => switch (functionData.functionCode) {
        'presence_time' => 1,
        'dis_current' => 1,
        'illuminance_value' => 1,
        _ => 1,
      };
}
