import 'dart:math' as math;

import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
import 'package:wolf_3d_entities/src/entities/enemies/enemy.dart';
import 'package:wolf_3d_entities/src/entities/enemies/enemy_animation.dart';
import 'package:wolf_3d_entities/src/entities/enemies/enemy_type.dart';
import 'package:wolf_3d_entities/src/entity.dart';

class Guard extends Enemy {
  static const double speed = 0.03;
  bool _hasFiredThisCycle = false;

  static EnemyType get type => EnemyType.guard;

  Guard({
    required super.x,
    required super.y,
    required super.angle,
    required super.mapId,
  }) : super(spriteIndex: type.animations.idle.start, state: EntityState.idle);

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

    checkWakeUp(
      elapsedMs: elapsedMs,
      playerPosition: playerPosition,
      isWalkable: isWalkable,
    );

    double distance = position.distanceTo(playerPosition);
    double angleToPlayer = position.angleTo(playerPosition);

    if (isAlerted && state != EntityState.dead) {
      newAngle = angleToPlayer;
    }

    // Calculate angle diff for the octant logic
    double diff = angleToPlayer - newAngle;
    while (diff <= -math.pi) {
      diff += 2 * math.pi;
    }
    while (diff > math.pi) {
      diff -= 2 * math.pi;
    }

    // Use the centralized animation logic to avoid manual offset errors
    EnemyAnimation currentAnim = switch (state) {
      EntityState.patrolling => EnemyAnimation.walking,
      EntityState.attacking => EnemyAnimation.attacking,
      EntityState.pain => EnemyAnimation.pain,
      EntityState.dead => isDying ? EnemyAnimation.dying : EnemyAnimation.dead,
      _ => EnemyAnimation.idle,
    };

    spriteIndex = type.getSpriteFromAnimation(
      animation: currentAnim,
      elapsedMs: elapsedMs,
      lastActionTime: lastActionTime,
      angleDiff: diff,
    );

    if (state == EntityState.patrolling) {
      if (!isAlerted || distance > 0.8) {
        double currentMoveAngle = isAlerted ? angleToPlayer : angle;
        double moveX = math.cos(currentMoveAngle) * speed;
        double moveY = math.sin(currentMoveAngle) * speed;

        movement = getValidMovement(
          Coordinate2D(moveX, moveY),
          isWalkable,
          tryOpenDoor,
        );
      }

      if (isAlerted && distance < 6.0 && elapsedMs - lastActionTime > 1500) {
        if (hasLineOfSight(playerPosition, isWalkable)) {
          state = EntityState.attacking;
          lastActionTime = elapsedMs;
          _hasFiredThisCycle = false;
        }
      }
    }

    if (state == EntityState.attacking) {
      int timeShooting = elapsedMs - lastActionTime;
      // SS-Specific firing logic
      if (timeShooting >= 100 && timeShooting < 200 && !_hasFiredThisCycle) {
        onDamagePlayer(damage);
        _hasFiredThisCycle = true;
      } else if (timeShooting >= 300) {
        state = EntityState.patrolling;
        lastActionTime = elapsedMs;
      }
    }

    if (state == EntityState.pain && elapsedMs - lastActionTime > 250) {
      state = EntityState.patrolling;
      lastActionTime = elapsedMs;
    }

    return (movement: movement, newAngle: newAngle);
  }
}
