import 'package:fis_measure/interfaces/date_types/point.dart';
import 'package:fis_measure/interfaces/process/calculators/output.dart';
import 'package:fis_measure/interfaces/process/items/measure_terms.dart';
import 'package:vid/us/vid_us_unit.dart';

import '../primitives/straightline.dart';
import 'calculator.dart';

class DistanceCal extends Calculator<StraightLine, double> {
  DistanceCal(StraightLine ref) : super(ref);

  @override
  void calculate() {
    if (ref.feature == null) return;

    // TODO:xxx
    final viewport = ref.feature!.hostVisualArea!.viewport!;
    final p1 = ref.feature!.startPoint;
    final p2 = ref.feature!.endPoint;
    final pp1 =
        DPoint(p1.x * viewport.region.width, p1.y * viewport.region.height);
    final pp2 =
        DPoint(p2.x * viewport.region.width, p2.y * viewport.region.height);

    final value = (pp2 - pp1).length.abs();
    // final value =  ref.feature!.length;
    final outputItem = OutputItem(
      value: value,
      name: MeasureTerms.Distance,
      unit: VidUsUnit.cm,
    );
    final description =
        "${ref.description}: ${roundDouble(value)}${VidUsUnit.cm.name}";
    outputItem.updateDescription(description: description);
    output = outputItem;
  }
}
