import 'dart:math' as math;

import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
import 'package:wolf_3d_entities/src/entities/enemies/dog.dart';
import 'package:wolf_3d_entities/src/entities/enemies/enemy_type.dart';
import 'package:wolf_3d_entities/src/entities/enemies/guard.dart';
import 'package:wolf_3d_entities/src/entities/enemies/mutant.dart';
import 'package:wolf_3d_entities/src/entities/enemies/officer.dart';
import 'package:wolf_3d_entities/src/entities/enemies/ss.dart';
import 'package:wolf_3d_entities/src/entity.dart';

abstract class Enemy extends Entity {
  Enemy({
    required super.x,
    required super.y,
    required super.spriteIndex,
    super.angle,
    super.state,
    super.mapId,
    super.lastActionTime,
  });

  int health = 25;
  int damage = 10;
  bool isDying = false;
  bool hasDroppedItem = false;
  bool isAlerted = false;

  // Replaces ob->temp2 for reaction delays
  int reactionTimeMs = 0;

  void takeDamage(int amount, int currentTime) {
    if (state == EntityState.dead) return;

    health -= amount;
    lastActionTime = currentTime;

    isAlerted = true;

    if (health <= 0) {
      state = EntityState.dead;
      isDying = true;
    } else if (math.Random().nextDouble() < 0.5) {
      state = EntityState.pain;
    } else {
      state = EntityState.patrolling;
    }
  }

  void checkWakeUp({
    required int elapsedMs,
    required Coordinate2D playerPosition,
    required bool Function(int x, int y) isWalkable,
    int baseReactionMs = 200,
    int reactionVarianceMs = 600,
  }) {
    if (!isAlerted && hasLineOfSight(playerPosition, isWalkable)) {
      if (reactionTimeMs == 0) {
        reactionTimeMs =
            elapsedMs +
            baseReactionMs +
            math.Random().nextInt(reactionVarianceMs);
      } else if (elapsedMs >= reactionTimeMs) {
        isAlerted = true;

        if (state == EntityState.idle) {
          state = EntityState.patrolling;
        }

        lastActionTime = elapsedMs;
        reactionTimeMs = 0;
      }
    }
  }

  // Matches WL_STATE.C's 'CheckLine' using canonical Integer DDA traversal
  bool hasLineOfSight(
    Coordinate2D playerPosition,
    bool Function(int x, int y) isWalkable,
  ) {
    // 1. Proximity Check (Matches WL_STATE.C 'MINSIGHT')
    // If the player is very close, sight is automatic regardless of facing angle.
    // This compensates for our lack of a noise/gunshot alert system!
    if (position.distanceTo(playerPosition) < 1.2) {
      return true;
    }

    // 2. FOV Check (Matches original sight angles)
    double angleToPlayer = position.angleTo(playerPosition);
    double diff = angle - angleToPlayer;

    while (diff <= -math.pi) {
      diff += 2 * math.pi;
    }
    while (diff > math.pi) {
      diff -= 2 * math.pi;
    }

    if (diff.abs() > math.pi / 2) return false;

    // 3. Map Check (Corrected Integer Bresenham)
    int currentX = position.x.toInt();
    int currentY = position.y.toInt();
    int targetX = playerPosition.x.toInt();
    int targetY = playerPosition.y.toInt();

    int dx = (targetX - currentX).abs();
    int dy = -(targetY - currentY).abs();
    int sx = currentX < targetX ? 1 : -1;
    int sy = currentY < targetY ? 1 : -1;
    int err = dx + dy;

    while (true) {
      if (!isWalkable(currentX, currentY)) return false;
      if (currentX == targetX && currentY == targetY) break;

      int e2 = 2 * err;
      if (e2 >= dy) {
        err += dy;
        currentX += sx;
      }
      if (e2 <= dx) {
        err += dx;
        currentY += sy;
      }
    }
    return true;
  }

  Coordinate2D getValidMovement(
    Coordinate2D intendedMovement,
    bool Function(int x, int y) isWalkable,
    void Function(int x, int y) tryOpenDoor,
  ) {
    double newX = position.x + intendedMovement.x;
    double newY = position.y + intendedMovement.y;

    int currentTileX = position.x.toInt();
    int currentTileY = position.y.toInt();
    int targetTileX = newX.toInt();
    int targetTileY = newY.toInt();

    bool movedX = currentTileX != targetTileX;
    bool movedY = currentTileY != targetTileY;

    // 1. Check Diagonal Movement
    if (movedX && movedY) {
      bool canMoveX = isWalkable(targetTileX, currentTileY);
      bool canMoveY = isWalkable(currentTileX, targetTileY);
      bool canMoveDiag = isWalkable(targetTileX, targetTileY);

      if (!canMoveX || !canMoveY || !canMoveDiag) {
        // Trigger doors if they are blocking the path
        if (!canMoveX) tryOpenDoor(targetTileX, currentTileY);
        if (!canMoveY) tryOpenDoor(currentTileX, targetTileY);
        if (!canMoveDiag) tryOpenDoor(targetTileX, targetTileY);

        if (canMoveX) return Coordinate2D(intendedMovement.x, 0);
        if (canMoveY) return Coordinate2D(0, intendedMovement.y);
        return const Coordinate2D(0, 0);
      }
    }

    // 2. Check Cardinal Movement
    if (movedX && !movedY) {
      if (!isWalkable(targetTileX, currentTileY)) {
        tryOpenDoor(targetTileX, currentTileY); // Try to open!
        return Coordinate2D(0, intendedMovement.y);
      }
    }
    if (movedY && !movedX) {
      if (!isWalkable(currentTileX, targetTileY)) {
        tryOpenDoor(currentTileX, targetTileY); // Try to open!
        return Coordinate2D(intendedMovement.x, 0);
      }
    }

    return intendedMovement;
  }

  ({Coordinate2D movement, double newAngle}) update({
    required int elapsedMs,
    required Coordinate2D playerPosition,
    required bool Function(int x, int y) isWalkable,
    required void Function(int x, int y) tryOpenDoor,
    required void Function(int damage) onDamagePlayer,
  });

  static Enemy? spawn(
    int objId,
    double x,
    double y,
    Difficulty difficulty, {
    bool isSharewareMode = false,
  }) {
    // 124 (Dead Guard) famously overwrote a patrol ID in the original engine!
    if (objId == MapObject.deadGuard || objId == MapObject.deadAardwolf) {
      return null;
    }
    if (objId >= MapObject.playerNorth && objId <= MapObject.playerWest) {
      return null;
    }

    // Prevent bosses from accidentally spawning as regular enemies!
    if (objId >= MapObject.bossHansGrosse &&
        objId <= MapObject.bossFettgesicht) {
      return null;
    }

    final type = EnemyType.fromMapId(objId);
    if (type == null) return null;

    // Reject enemies that don't exist in the shareware data!
    if (isSharewareMode && !type.existsInShareware) return null;

    final mapData = type.mapData;

    // ALL enemies have explicit directional angles!
    double spawnAngle = CardinalDirection.fromEnemyIndex(objId).radians;
    EntityState spawnState;

    if (mapData.isPatrolForDifficulty(objId, difficulty)) {
      spawnState = EntityState.patrolling;
    } else if (mapData.isStaticForDifficulty(objId, difficulty)) {
      spawnState = EntityState.idle;
    } else if (mapData.isAmbushForDifficulty(objId, difficulty)) {
      spawnState = EntityState.ambush;
    } else {
      return null; // ID belongs to this enemy, but not on this difficulty
    }

    return switch (type) {
      EnemyType.guard => Guard(x: x, y: y, angle: spawnAngle, mapId: objId),
      EnemyType.dog => Dog(x: x, y: y, angle: spawnAngle, mapId: objId),
      EnemyType.ss => SS(x: x, y: y, angle: spawnAngle, mapId: objId),
      EnemyType.mutant => Mutant(x: x, y: y, angle: spawnAngle, mapId: objId),
      EnemyType.officer => Officer(x: x, y: y, angle: spawnAngle, mapId: objId),
    }..state = spawnState;
  }
}
