import 'dart:math' as math;

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

class SS extends Enemy {
  static const double speed = 0.04;

  @override
  EnemyType get type => EnemyType.ss;

  SS({
    required super.x,
    required super.y,
    required super.angle,
    required super.mapId,
  }) : super(
         spriteIndex: EnemyType.ss.animations.idle.start,
         state: EntityState.idle,
       ) {
    health = 100;
    damage = 20;
  }
  @override
  ({Coordinate2D movement, double newAngle}) update({
    required int elapsedMs,
    required int elapsedDeltaMs,
    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;

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

    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,
      walkFrameOverride: state == EntityState.patrolling ? currentFrame : null,
    );

    if (state == EntityState.patrolling) {
      if (!isAlerted || distance > 0.8) {
        double currentMoveAngle = isAlerted ? angleToPlayer : angle;
        movement = getValidMovement(
          intendedMovement: Coordinate2D(
            math.cos(currentMoveAngle) * speed,
            math.sin(currentMoveAngle) * speed,
          ),
          playerPosition: playerPosition,
          isWalkable: isWalkable,
          tryOpenDoor: tryOpenDoor,
        );
      }

      if (processTics(elapsedDeltaMs, moveSpeed: speed)) {
        currentFrame = (currentFrame + 1) % 4;
        setTics(8);

        if (isAlerted &&
            distance < 10.0 &&
            hasLineOfSight(playerPosition, isWalkable)) {
          state = EntityState.attacking;
          currentFrame = 0;
          lastActionTime = elapsedMs;
          setTics(10); // Aiming
        }
      }
    }

    if (state == EntityState.attacking) {
      if (processTics(elapsedDeltaMs, moveSpeed: 0)) {
        currentFrame++;
        if (currentFrame == 1) {
          onDamagePlayer(damage);
          setTics(5); // Bang!
        } else if (currentFrame == 2) {
          setTics(5); // Cooldown
        } else {
          // Burst fire chance
          if (distance < 10.0 && hasLineOfSight(playerPosition, isWalkable)) {
            if (math.Random().nextDouble() > 0.5) {
              // 50% chance to burst
              currentFrame = 1;
              onDamagePlayer(damage);
              setTics(5);
              return (movement: movement, newAngle: newAngle);
            }
          }
          state = EntityState.patrolling;
          currentFrame = 0;
          setTics(8);
        }
      }
    }

    if (state == EntityState.pain) {
      if (processTics(elapsedDeltaMs, moveSpeed: 0)) {
        state = EntityState.patrolling;
        currentFrame = 0;
        setTics(5);
      }
    }

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