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

class WaterHeaterValueSelectorWidget extends StatelessWidget {
  final String selectedFunction;
  final DeviceFunctionData functionData;
  final List<WaterHeaterFunctions> whFunctions;
  final AllDevicesModel? device;
  final String dialogType;  

  const WaterHeaterValueSelectorWidget({
    required this.selectedFunction,
    required this.functionData,
    required this.whFunctions,
    required this.device,
    required this.dialogType,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final selectedFn = whFunctions.firstWhere(
      (f) => f.code == selectedFunction,
      orElse: () => WHSwitchFunction(
        deviceId: '',
        deviceName: '',
        type: '',
      ),
    );
    if (selectedFunction == 'countdown_1') {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _buildCountDownSlider(
            context,
            functionData.value,
            device,
            selectedFn.operationName,
            functionData,
            selectedFunction,
            dialogType
          ),
          const SizedBox(height: 10),
        ],
      );
    }

    return WaterHeaterOperationalValuesList(
      values: selectedFn.getOperationalValues(),
      selectedValue: functionData.value,
      device: device,
      operationName: selectedFn.operationName,
      selectCode: selectedFunction,
      onSelect: (selectedValue) async {
        context.read<FunctionBloc>().add(
              AddFunction(
                functionData: DeviceFunctionData(
                  entityId: device?.uuid ?? '',
                  functionCode: selectedFunction,
                  operationName: functionData.operationName,
                  value: selectedValue.value,
                  condition: functionData.condition,
                ),
              ),
            );
      },
    );
  }

  static Widget _buildCountDownSlider(
    BuildContext context,
    dynamic initialValue,
    AllDevicesModel? device,
    String operationName,
    DeviceFunctionData? selectedFunctionData,
    String selectCode,
    String dialogType,
  ) {
    return CustomRoutinesTextbox(
      withSpecialChar: false,
      currentCondition: selectedFunctionData?.condition,
      dialogType: dialogType,
      sliderRange: (0, 43200),
      displayedValue: (initialValue ?? 0).toString(),
      initialValue: (initialValue ?? 0).toString(),
      onConditionChanged: (condition) {
        context.read<FunctionBloc>().add(
              AddFunction(
                functionData: DeviceFunctionData(
                  entityId: device?.uuid ?? '',
                  functionCode: selectCode,
                  operationName: operationName,
                  value: condition,
                  condition: condition,
                  valueDescription: selectedFunctionData?.valueDescription,
                ),
              ),
            );
      },
      onTextChanged: (value) {
        final roundedValue = value.round();
        context.read<FunctionBloc>().add(
              AddFunction(
                functionData: DeviceFunctionData(
                  entityId: device?.uuid ?? '',
                  functionCode: selectCode,
                  operationName: operationName,
                  value: roundedValue,
                  condition: selectedFunctionData?.condition,
                  valueDescription: selectedFunctionData?.valueDescription,
                ),
              ),
            );
      },
      unit: 'sec',
      dividendOfRange: 1,
      stepIncreaseAmount: 1,
    );
  }
}
