import 'dart:ui';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:path_parsing/path_parsing.dart';

/// Created by Haoyi on 2021/7/12

class PathWrapper extends PathProxy {
  Path _path;

  PathWrapper(this._path);

  Path get path => _path;

  void transform(Matrix4 matrix4) {
    _path = _path.transform(matrix4.storage);
  }

  @override
  void close() {
    path.close();
  }

  @override
  void cubicTo(double x1, double y1, double x2, double y2, double x3, double y3) {
    path.cubicTo(x1, y1, x2, y2, x3, y3);
  }

  @override
  void lineTo(double x, double y) {
    path.lineTo(x, y);
  }

  @override
  void moveTo(double x, double y) {
    path.moveTo(x, y);
  }
}

class SvgParser {
  final String svgPath;
  final PathFillType fillType;

  Path? _path;

  Path get path {
    if (_path == null) {
      final p = Path()..fillType = fillType;
      final pathWrapper = PathWrapper(p);
      writeSvgPathDataToPath(svgPath, pathWrapper);
      _path = p;
    }
    return _path!;
  }

  SvgParser.create({required this.svgPath, this.fillType = PathFillType.evenOdd});
}
