import 'dart:math' as math;

import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flutter/material.dart';

import '../../../../../core/theme/app_theme.dart';

const _gold = AppColors.gold;
const _goldDark = Color(0xFF8A5A00); // طلاییِ تیره‌ترِ مخصوصِ نشان‌ها

/// ستاره‌ی طلایی روی بازیکنِ نوبت‌دار (مطابقِ تصویرِ مرجع).
class StarBadge extends PositionComponent {
  final double r;
  StarBadge(this.r, Vector2 pos)
      : super(position: pos, size: Vector2.all(r * 2), anchor: Anchor.center);

  static final _fill = Paint()..color = _gold;
  static final _stroke = Paint()
    ..style = PaintingStyle.stroke
    ..strokeWidth = 1.5
    ..color = _goldDark;

  @override
  void render(Canvas canvas) {
    final path = _starPath(Offset(r, r), r, r * 0.45, 5);
    canvas.drawPath(path, _fill);
    canvas.drawPath(path, _stroke);
  }
}

/// تاجِ طلایی روی حاکم.
class CrownBadge extends PositionComponent {
  final double w;
  CrownBadge(this.w, Vector2 pos)
      : super(
          position: pos,
          size: Vector2(w, w * 0.8),
          anchor: Anchor.center,
        );

  static final _fill = Paint()..color = _gold;
  static final _stroke = Paint()
    ..style = PaintingStyle.stroke
    ..strokeWidth = 1.5
    ..color = _goldDark;

  @override
  void render(Canvas canvas) {
    final h = size.y;
    final p = Path()
      ..moveTo(0, h) // پایین‌چپ
      ..lineTo(0, h * 0.35) // نوکِ چپ
      ..lineTo(w * 0.25, h * 0.62) // فرورفتگی
      ..lineTo(w * 0.5, h * 0.18) // نوکِ میانی (بلندترین)
      ..lineTo(w * 0.75, h * 0.62)
      ..lineTo(w, h * 0.35) // نوکِ راست
      ..lineTo(w, h) // پایین‌راست
      ..close();
    canvas.drawPath(p, _fill);
    canvas.drawPath(p, _stroke);
    // نگین‌های نوکِ تاج
    final gem = Paint()..color = const Color(0xFFB71C1C);
    canvas.drawCircle(Offset(w * 0.5, h * 0.14), w * 0.05, gem);
  }
}

Path _starPath(Offset c, double outer, double inner, int points) {
  final path = Path();
  for (var i = 0; i < points * 2; i++) {
    final rad = i.isEven ? outer : inner;
    final ang = -math.pi / 2 + i * math.pi / points;
    final pt = Offset(c.dx + rad * math.cos(ang), c.dy + rad * math.sin(ang));
    i == 0 ? path.moveTo(pt.dx, pt.dy) : path.lineTo(pt.dx, pt.dy);
  }
  return path..close();
}

/// افکتِ ضربان (برای جلبِ توجه؛ مثلاً ستاره‌ی نوبت).
PulseEffect pulse() => PulseEffect();

class PulseEffect extends ScaleEffect {
  PulseEffect()
      : super.by(
          Vector2.all(0.25),
          EffectController(
            duration: 0.5,
            reverseDuration: 0.5,
            infinite: true,
            curve: Curves.easeInOut,
          ),
        );
}
