import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';

class TriangleClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    /// Start Point：(0, H/2)
    /// C1：[ (W/3, H/2), (W/3, 0), (W/2, 0)]
    /// C2：[ (W*2/3, 0), (W*2/3, H/2), (W, H/2)]
    /// C3：[ (W*2/3, H/2), (W*2/3, H), (W/2, H)]
    /// C4：[ (W/3, H), (W/3, H/2), (0, H/2)]
    Path path = Path();
    path.moveTo(0, size.height / 2);
    path.cubicTo(
        size.width / 3, size.height / 2, size.width / 3, 0, size.width / 2, 0);
    path.cubicTo(size.width * 2 / 3, 0, size.width * 2 / 3, size.height / 2,
        size.width, size.height / 2);
    path.cubicTo(size.width * 2 / 3, size.height / 2, size.width * 2 / 3,
        size.height, size.width / 2, size.height);
    path.cubicTo(size.width / 3, size.height, size.width / 3, size.height / 2,
        0, size.height / 2);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(TriangleClipper oldClipper) => false;
}

class ClipShadowPath extends FStatelessWidget {
  final Shadow shadow;
  final CustomClipper<Path> clipper;
  final Widget child;

  const ClipShadowPath({
    Key? key,
    required this.shadow,
    required this.clipper,
    required this.child,
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FCustomPaint(
      painter: _ClipShadowShadowPainter(
        clipper: clipper,
        shadow: shadow,
      ),
      child: FClipPath(child: child, clipper: clipper),
    );
  }
}

class _ClipShadowShadowPainter extends CustomPainter {
  final Shadow shadow;
  final CustomClipper<Path> clipper;

  _ClipShadowShadowPainter({required this.shadow, required this.clipper});

  @override
  void paint(Canvas canvas, Size size) {
    var paint = shadow.toPaint();
    var clipPath = clipper.getClip(size).shift(shadow.offset);
    canvas.drawPath(clipPath, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
