import 'dart:math';
import '../enums.dart';
import '../models.dart';

class EffectEventFactory {
  /// Generates a unique ID for events.
  static String _generateId(Random random) {
    return DateTime.now().millisecondsSinceEpoch.toString() +
        random.nextInt(1000).toString();
  }

  /// Creates a successful attack event.
  static EffectEvent createAttackEvent({
    required Character attacker,
    required Character target,
    required EffectTarget effectTarget,
    required RiskLevel risk,
    required int damage,
    required Random random,
  }) {
    return EffectEvent(
      id: _generateId(random),
      type: ActionType.attack,
      risk: risk,
      target: effectTarget,
      feedbackType: null,
      attacker: attacker,
      targetEntity: target,
      damageValue: damage,
      isSuccess: true,
    );
  }

  /// Creates a dodge event.
  static EffectEvent createDodgeEvent({
    required Character attacker,
    required Character target,
    required EffectTarget effectTarget,
    required RiskLevel risk,
    required Random random,
  }) {
    return EffectEvent(
      id: _generateId(random),
      type: ActionType.attack,
      risk: risk,
      target: effectTarget,
      feedbackType: BattleFeedbackType.dodge,
      attacker: attacker,
      targetEntity: target,
      damageValue: 0,
      isSuccess: false,
    );
  }

  /// Creates a miss or failure event.
  static EffectEvent createFailureEvent({
    required Character attacker,
    required Character target,
    required EffectTarget effectTarget,
    required ActionType type,
    required RiskLevel risk,
    required Random random,
  }) {
    BattleFeedbackType feedbackType = (type == ActionType.attack)
        ? BattleFeedbackType.miss
        : BattleFeedbackType.failed;

    return EffectEvent(
      id: _generateId(random),
      type: type,
      risk: risk,
      target: effectTarget,
      feedbackType: feedbackType,
      attacker: attacker,
      targetEntity: target,
      isSuccess: false,
    );
  }

  /// Creates a successful defense event.
  static EffectEvent createDefenseEvent({
    required Character attacker,
    required Character target,
    required EffectTarget effectTarget,
    required RiskLevel risk,
    required int armorGained,
    required Random random,
  }) {
    return EffectEvent(
      id: _generateId(random),
      type: ActionType.defend,
      risk: risk,
      target: effectTarget,
      feedbackType: null,
      attacker: attacker,
      targetEntity: target,
      armorGained: armorGained,
      isSuccess: true,
    );
  }
}
