import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:path_drawing/path_drawing.dart';

extension MeasureCanvasExt on Canvas {
  /// 画虚线
  ///
  /// [p1] 第一个点
  ///
  /// [p2] 第二个点
  ///
  /// [dashWidth] 虚线点长度
  ///
  /// [spaceWidth] 虚线点间隔长度
  ///
  /// [paint] 画笔
  void drawDashLine(
    Offset p1,
    Offset p2,
    double dashWidth,
    double spaceWidth,
    Paint paint,
  ) {
    final path = Path()
      ..moveTo(p1.dx, p1.dy)
      ..lineTo(p2.dx, p2.dy);
    drawPath(
      dashPath(
        path,
        dashArray: CircularIntervalList<double>([dashWidth, spaceWidth]),
      ),
      paint,
    );
  }

  static final Paint _vertexPaint = Paint()
    ..strokeWidth = 1
    ..style = PaintingStyle.stroke
    ..isAntiAlias = true;

  /// 画顶点
  ///
  /// [offset] 位置
  ///
  /// [size] 尺寸
  ///
  /// [active] 是否活动
  void drawVertex(
    Offset offset,
    double size, {
    bool active = false,
  }) {
    final radius = size / 2.0;
    double x = offset.dx, y = offset.dy;
    save();
    final path = Path();
    // top_left -> bottom_right
    path.moveTo(x - radius, y - radius);
    path.lineTo(x + radius, y + radius);
    // top_right -> bottom_left
    path.moveTo(x + radius, y - radius);
    path.lineTo(x - radius, y + radius);

    _vertexPaint.color = active ? Colors.green : Colors.yellow;
    drawPath(path, _vertexPaint);
    restore();
  }
}
