import 'package:fis_common/event/event_type.dart';
import 'package:fis_measure/interfaces/process/calculators/calculator.dart';
import 'package:fis_measure/interfaces/process/calculators/output.dart';
import 'package:fis_measure/interfaces/process/items/item.dart';
import 'package:flutter/foundation.dart';

abstract class Calculator<T extends IMeasureItem, TValue>
    implements ICalculator<TValue> {
  final List<OutputItem<TValue>> _outputs = [];
  OutputItem<TValue>? _outputItem;
  late T _ref;

  Calculator(T ref) {
    _ref = ref;
  }

  @override
  OutputItem<TValue>? get output => _outputItem;
  @protected
  set output(OutputItem<TValue>? value) {
    if (value != _outputItem) {
      _outputItem = value;
      outputChanged.emit(this, value);
    }
  }

  @override
  List<OutputItem<TValue>> get outputs => _outputs;

  T get ref => _ref;

  @override
  var outputChanged = FEventHandler<OutputItem<TValue>?>();

  /// 对double四舍五入
  double roundDouble(double value, [int digits = 2]) {
    final digitsStr = value.toStringAsFixed(digits);
    final result = double.parse(digitsStr);
    return result;
  }
}
