import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:syncrow_web/pages/routines/models/device_functions.dart';

part 'functions_bloc_event.dart';
part 'functions_bloc_state.dart';

class FunctionBloc extends Bloc<FunctionBlocEvent, FunctionBlocState> {
  FunctionBloc() : super(const FunctionBlocState()) {
    on<InitializeFunctions>(_onInitializeFunctions);
    on<AddFunction>(_onAddFunction);
    on<SelectFunction>(_onSelectFunction);
  }
  // void _onAddFunction(AddFunction event, Emitter<FunctionBlocState> emit) {
  //   final functions = List<DeviceFunctionData>.from(state.addedFunctions);
  //   final existingIndex = functions.indexWhere(
  //     (f) => f.functionCode == event.functionData.functionCode,
  //   );

  //   if (existingIndex != -1) {
  //     final existingData = functions[existingIndex];
  //     functions[existingIndex] = DeviceFunctionData(
  //       entityId: event.functionData.entityId,
  //       functionCode: event.functionData.functionCode,
  //       operationName: event.functionData.operationName,
  //       value: event.functionData.value ?? existingData.value,
  //       valueDescription: event.functionData.valueDescription ??
  //           existingData.valueDescription,
  //       condition: event.functionData.condition ?? existingData.condition,
  //       step: event.functionData.step ?? existingData.step,
  //     );
  //   } else {
  //     functions.clear();
  //     functions.add(event.functionData);
  //   }

  //   emit(state.copyWith(
  //     addedFunctions: functions,
  //     selectedFunction: event.functionData.functionCode,
  //   ));
  // }

  void _onAddFunction(AddFunction event, Emitter<FunctionBlocState> emit) {
    final functions = List<DeviceFunctionData>.from(state.addedFunctions);
    final existingIndex = functions.indexWhere(
      (f) => f.functionCode == event.functionData.functionCode,
    );

    if (existingIndex != -1) {
      // Update the function value
      functions[existingIndex] = event.functionData;
    } else {
      // Add new function value
      functions.add(event.functionData);
    }

    emit(state.copyWith(
      addedFunctions: functions,
      selectedFunction: event.functionData.functionCode,
    ));
  }

  void _onInitializeFunctions(
    InitializeFunctions event,
    Emitter<FunctionBlocState> emit,
  ) {
    emit(state.copyWith(addedFunctions: event.functions));
  }

  DeviceFunctionData? getFunction(String functionCode) {
    return state.addedFunctions.firstWhere(
      (data) => data.functionCode == functionCode,
      orElse: () => DeviceFunctionData(
        entityId: '',
        functionCode: functionCode,
        operationName: '',
        value: null,
      ),
    );
  }

  FutureOr<void> _onSelectFunction(
      SelectFunction event, Emitter<FunctionBlocState> emit) {
    emit(state.copyWith(
        selectedFunction: event.functionCode,
        selectedOperationName: event.operationName));
  }
}
