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/entity.dart';

class Officer extends Enemy {
  static const double speed = 0.055;
  bool _hasFiredThisCycle = false;

  Officer({
    required super.x,
    required super.y,
    required super.angle,
    required super.mapId,
  }) : super(
         spriteIndex: EnemyType.officer.spriteBaseIdx,
         state: EntityState.idle,
       ) {
    health = 50;
    damage = 15;
  }

  @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 (state != EntityState.idle && state != EntityState.dead) {
      newAngle = angleToPlayer;
    }

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

    int octant = ((diff + (math.pi / 8)) / (math.pi / 4)).floor() % 8;
    if (octant < 0) octant += 8;

    switch (state) {
      case EntityState.idle:
        spriteIndex = EnemyType.officer.spriteBaseIdx + octant;
        break;

      case EntityState.patrolling:
        if (distance > 0.8) {
          double moveX = math.cos(angleToPlayer) * speed;
          double moveY = math.sin(angleToPlayer) * speed;
          movement = getValidMovement(
            Coordinate2D(moveX, moveY),
            isWalkable,
            tryOpenDoor,
          );
        }

        int walkFrame = (elapsedMs ~/ 150) % 4;
        spriteIndex =
            (EnemyType.officer.spriteBaseIdx + 8) + (walkFrame * 8) + octant;

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

      case EntityState.attacking:
        int timeShooting = elapsedMs - lastActionTime;
        if (timeShooting < 150) {
          spriteIndex = EnemyType.officer.spriteBaseIdx + 40; // Aiming
        } else if (timeShooting < 300) {
          spriteIndex = EnemyType.officer.spriteBaseIdx + 41; // Firing
          if (!_hasFiredThisCycle) {
            onDamagePlayer(damage);
            _hasFiredThisCycle = true;
          }
        } else if (timeShooting < 450) {
          spriteIndex = EnemyType.officer.spriteBaseIdx + 40; // Recoil
        } else {
          state = EntityState.patrolling;
          lastActionTime = elapsedMs;
        }
        break;

      case EntityState.pain:
        spriteIndex = EnemyType.officer.spriteBaseIdx + 42;
        if (elapsedMs - lastActionTime > 250) {
          state = EntityState.patrolling;
          lastActionTime = elapsedMs;
        }
        break;

      case EntityState.dead:
        if (isDying) {
          int deathFrame = (elapsedMs - lastActionTime) ~/ 150;
          if (deathFrame < 3) {
            spriteIndex = (EnemyType.officer.spriteBaseIdx + 43) + deathFrame;
          } else {
            spriteIndex = EnemyType.officer.spriteBaseIdx + 45;
            isDying = false;
          }
        } else {
          spriteIndex = EnemyType.officer.spriteBaseIdx + 45;
        }
        break;

      default:
        break;
    }

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