/// Created by Haoyi on 2020/12/29
///
///
///

part of 'tips.dart';

enum TipsNip { none, left, right, top, bottom }

class TipsClipper extends CustomClipper<Path> {
  final TipsNip nip;
  Radius? radius;
  final double nipRadius;
  final double nipWidth;
  final double nipHeight;
  final double nipOffset;
  late double _nipPX;
  late double _nipPY;

  TipsClipper({this.nip = TipsNip.top, this.nipOffset = 0, this.radius, this.nipRadius = 4, this.nipHeight = 8, this.nipWidth = 16}) {
    var k = nipHeight / (nipWidth * 0.5);
    _nipPX = nipRadius / sqrt(1 + k * k);
    _nipPY = _nipPX * k;
    Log.v("_nipPX:$_nipPX _nipPy:$_nipPY nipHeight:$nipHeight $nipWidth");
  }

  @override
  Path getClip(Size size) {
    final path = Path();
    switch (nip) {
      case TipsNip.top:
        path.addRRect(RRect.fromLTRBR(0, nipHeight, size.width, size.height, radius ?? Radius.circular(12)));

        path.moveTo(nipOffset - (nipWidth / 2), nipHeight);
        path.lineTo(nipOffset - _nipPX, _nipPY);
        path.arcToPoint(Offset(nipOffset + _nipPX, _nipPY), radius: Radius.circular(nipRadius), clockwise: true);
        path.lineTo(nipOffset + (nipWidth / 2), nipHeight);

        path.close();
        break;
      case TipsNip.bottom:
        final rect = RRect.fromLTRBR(0, 0, size.width, size.height - nipHeight, radius ?? Radius.circular(12));
        path.addRRect(rect);
        path.moveTo(nipOffset - (nipWidth / 2), rect.bottom);
        path.lineTo(nipOffset - _nipPX, rect.bottom + nipHeight - _nipPY);
        path.arcToPoint(Offset(nipOffset + _nipPX, rect.bottom + nipHeight - _nipPY), radius: Radius.circular(nipRadius), clockwise: false);
        path.lineTo(nipOffset + (nipWidth / 2), rect.bottom);
        path.close();
        break;
      default:
        break;
    }
    return path;
  }

  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
    return false;
  }
}
