import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/models/space_connection_model.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class SpacesConnectionsArrowPainter extends CustomPainter {
  final List<SpaceConnectionModel> connections;
  final Map<String, Offset> positions;
  final double cardWidth = 150.0;
  final double cardHeight = 90.0;
  final Set<String> highlightedUuids;

  SpacesConnectionsArrowPainter({
    required this.connections,
    required this.positions,
    required this.highlightedUuids,
  });

  @override
  void paint(Canvas canvas, Size size) {
    for (final connection in connections) {
      final isSelected = highlightedUuids.contains(connection.from) ||
          highlightedUuids.contains(connection.to);
      final paint = Paint()
        ..color = isSelected
            ? ColorsManager.blackColor
            : ColorsManager.blackColor.withValues(alpha: 0.5)
        ..strokeWidth = 2.0
        ..style = PaintingStyle.stroke;

      final from = positions[connection.from];
      final to = positions[connection.to];

      if (from != null && to != null) {
        final startPoint =
            Offset(from.dx + cardWidth / 2, from.dy + cardHeight - 10);
        final endPoint = Offset(to.dx + cardWidth / 2, to.dy);

        final path = Path()..moveTo(startPoint.dx, startPoint.dy);

        final controlPoint1 = Offset(startPoint.dx, startPoint.dy + 20);
        final controlPoint2 = Offset(endPoint.dx, endPoint.dy - 60);

        path.cubicTo(controlPoint1.dx, controlPoint1.dy, controlPoint2.dx,
            controlPoint2.dy, endPoint.dx, endPoint.dy);

        canvas.drawPath(path, paint);

        final circlePaint = Paint()
          ..color = isSelected
              ? ColorsManager.blackColor
              : ColorsManager.blackColor.withValues(alpha: 0.5)
          ..style = PaintingStyle.fill
          ..blendMode = BlendMode.srcIn;
        canvas.drawCircle(endPoint, 4, circlePaint);
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
