import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/connection_model.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class CurvedLinePainter extends CustomPainter {
  final List<Connection> connections;

  CurvedLinePainter(this.connections);

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = ColorsManager.blackColor
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;

    // Ensure connections exist before painting
    if (connections.isEmpty) {
      return; // Nothing to paint if there are no connections
    }

    for (var connection in connections) {
      // Ensure positions are valid before drawing lines
      if (connection.endSpace.position == null) {
        continue;
      }

      Offset start = connection.startSpace.position +
          const Offset(75, 60); // Center bottom of start space
      Offset end = connection.endSpace.position +
          const Offset(75, 0); // Center top of end space

      // Curved line for down connections
      final controlPoint = Offset((start.dx + end.dx) / 2, start.dy + 50);
      final path = Path()
        ..moveTo(start.dx, start.dy)
        ..quadraticBezierTo(controlPoint.dx, controlPoint.dy, end.dx, end.dy);
      canvas.drawPath(path, paint);

      final dotPaint = Paint()..color = ColorsManager.blackColor;
      canvas.drawCircle(start, 5, dotPaint); // Start dot
      canvas.drawCircle(end, 5, dotPaint); // End dot
    }
  }

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