import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:intaleq_maps/intaleq_maps.dart';
import 'package:flutter/services.dart';

class MarkerGenerator {
  // دالة لرسم ماركر يحتوي على نص (للوقت والمسافة)
  static Future<InlqBitmap> createCustomMarkerBitmap({
    required String title,
    required String subtitle,
    required Color color,
    required IconData iconData,
  }) async {
    final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(pictureRecorder);

    // إعدادات القياسات
    const double width = 220.0;
    const double height = 110.0;
    const double circleRadius = 25.0;

    // 1. رسم المربع (Info Box) مع تدرج لوني بسيط
    final Paint paint = Paint()..color = color;
    final RRect rRect = RRect.fromRectAndRadius(
      const Rect.fromLTWH(0, 0, width, 65),
      const Radius.circular(20), // زوايا أكثر استدارة لشكل عصري
    );

    // ظل أقوى لمظهر بارز (Premium Feel)
    canvas.drawShadow(Path()..addRRect(rRect), Colors.black54, 10.0, true);
    canvas.drawRRect(rRect, paint);

    // 2. رسم مثلث صغير أسفل المربع (Arrow Tail)
    final Path path = Path();
    path.moveTo(width / 2 - 10, 60);
    path.lineTo(width / 2, 75);
    path.lineTo(width / 2 + 10, 60);
    path.close();
    canvas.drawPath(path, paint);

    // 3. رسم الدائرة (مكان الأيقونة)
    canvas.drawCircle(const Offset(width / 2, 85), circleRadius, paint);

    // 4. رسم الأيقونة داخل الدائرة
    TextPainter iconPainter = TextPainter(textDirection: TextDirection.ltr);
    iconPainter.text = TextSpan(
      text: String.fromCharCode(iconData.codePoint),
      style: TextStyle(
        fontSize: 30.0,
        fontFamily: iconData.fontFamily,
        color: Colors.white,
      ),
    );
    iconPainter.layout();
    iconPainter.paint(
      canvas,
      Offset((width - iconPainter.width) / 2, 85 - (iconPainter.height / 2)),
    );

    // 5. رسم النصوص (العنوان والوصف) داخل المربع
    // العنوان (مثلاً: المدة)
    TextPainter titlePainter = TextPainter(
      textDirection: TextDirection.rtl,
      textAlign: TextAlign.center,
    );
    titlePainter.text = TextSpan(
      text: title,
      style: const TextStyle(
        fontSize: 20.0,
        fontWeight: FontWeight.bold,
        color: Colors.white,
      ),
    );
    titlePainter.layout(minWidth: width);
    titlePainter.paint(canvas, const Offset(0, 8));

    // الوصف (مثلاً: المسافة)
    TextPainter subTitlePainter = TextPainter(
      textDirection: TextDirection.rtl,
      textAlign: TextAlign.center,
    );
    subTitlePainter.text = TextSpan(
      text: subtitle,
      style: const TextStyle(
        fontSize: 16.0,
        color: Colors.white70,
      ),
    );
    subTitlePainter.layout(minWidth: width);
    subTitlePainter.paint(canvas, const Offset(0, 32));

    // تحويل الرسم إلى صورة
    final ui.Image image = await pictureRecorder.endRecording().toImage(
          width.toInt(),
          (height + 20).toInt(), // مساحة إضافية
        );
    final ByteData? data =
        await image.toByteData(format: ui.ImageByteFormat.png);
    return InlqBitmap.fromBytes(data!.buffer.asUint8List());
  }

  // دالة خاصة لرسم ماركر السائق (دائرة وخلفها سهم)
  static Future<InlqBitmap> createDriverMarker() async {
    final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(pictureRecorder);
    const double size = 100.0;

    final Paint paint = Paint()..color = const Color(0xFF2E7D32); // أخضر غامق
    final Paint borderPaint = Paint()
      ..color = Colors.white
      ..style = PaintingStyle.stroke
      ..strokeWidth = 4.0;

    // الدائرة
    canvas.drawCircle(const Offset(size / 2, size / 2), size / 2.5, paint);
    canvas.drawCircle(
        const Offset(size / 2, size / 2), size / 2.5, borderPaint);

    // رسم السهم (Arrow Up)
    TextPainter iconPainter = TextPainter(textDirection: TextDirection.ltr);
    iconPainter.text = TextSpan(
      text: String.fromCharCode(Icons.navigation.codePoint), // سهم ملاحة
      style: TextStyle(
        fontSize: 40.0,
        fontFamily: Icons.navigation.fontFamily,
        color: Colors.white,
      ),
    );
    iconPainter.layout();
    iconPainter.paint(
      canvas,
      Offset((size - iconPainter.width) / 2, (size - iconPainter.height) / 2),
    );

    final ui.Image image = await pictureRecorder
        .endRecording()
        .toImage(size.toInt(), size.toInt());
    final ByteData? data =
        await image.toByteData(format: ui.ImageByteFormat.png);
    return InlqBitmap.fromBytes(data!.buffer.asUint8List());
  }

  // دالة لرسم ماركر المشي
  static Future<InlqBitmap> createWalkMarker() async {
    final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(pictureRecorder);
    const double size = 60.0;

    final Paint paint = Paint()..color = const Color(0xFF0288D1);
    final Paint borderPaint = Paint()
      ..color = Colors.white
      ..style = PaintingStyle.stroke
      ..strokeWidth = 3.0;

    canvas.drawCircle(const Offset(size / 2, size / 2), size / 2.5, paint);
    canvas.drawCircle(const Offset(size / 2, size / 2), size / 2.5, borderPaint);

    TextPainter iconPainter = TextPainter(textDirection: TextDirection.ltr);
    iconPainter.text = TextSpan(
      text: String.fromCharCode(Icons.directions_walk.codePoint),
      style: TextStyle(
        fontSize: 30.0,
        fontFamily: Icons.directions_walk.fontFamily,
        color: Colors.white,
      ),
    );
    iconPainter.layout();
    iconPainter.paint(
      canvas,
      Offset((size - iconPainter.width) / 2, (size - iconPainter.height) / 2),
    );

    final ui.Image image = await pictureRecorder
        .endRecording()
        .toImage(size.toInt(), size.toInt());
    final ByteData? data =
        await image.toByteData(format: ui.ImageByteFormat.png);
    return InlqBitmap.fromBytes(data!.buffer.asUint8List());
  }
}
