import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

enum TriangleDirection { left, right, up, down }

class TriangleIndicator extends StatelessWidget {
  final double pointerSize;
  final Color? color;
  final TriangleDirection direction;

  const TriangleIndicator({
    super.key,
    required this.pointerSize,
    this.color,
    this.direction = TriangleDirection.right,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(pointerSize, pointerSize),
      painter: _TrianglePainter(
        color: color ?? AppColors.primaryRedColor,
        direction: direction,
      ),
    );
  }
}

class _TrianglePainter extends CustomPainter {
  final Color color;
  final TriangleDirection direction;

  _TrianglePainter({required this.color, required this.direction});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill
      ..isAntiAlias = true;

    final path = Path();
    final w = size.width;
    final h = size.height;

    switch (direction) {
      case TriangleDirection.right:
        // apex on the right, base on the left
        path.moveTo(0, h / 2);
        path.lineTo(w, 0);
        path.lineTo(w, h);
        path.close();
        break;
      case TriangleDirection.left:
        // apex on the left, base on the right
        path.moveTo(w, h / 2);
        path.lineTo(0, 0);
        path.lineTo(0, h);
        path.close();
        break;
      case TriangleDirection.up:
        // apex on top, base on bottom
        path.moveTo(w / 2, 0);
        path.lineTo(0, h);
        path.lineTo(w, h);
        path.close();
        break;
      case TriangleDirection.down:
        // apex on bottom, base on top
        path.moveTo(w / 2, h);
        path.lineTo(0, 0);
        path.lineTo(w, 0);
        path.close();
        break;
    }

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant _TrianglePainter oldDelegate) {
    return oldDelegate.color != color || oldDelegate.direction != direction;
  }
}
