import 'dart:math' as math;

import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
import 'package:wolf_3d_entities/wolf_3d_entities.dart';

class DoorManager {
  // Key is '$x,$y'
  final Map<String, Door> doors = {};

  void initDoors(Sprite wallGrid) {
    doors.clear();
    for (int y = 0; y < wallGrid.length; y++) {
      for (int x = 0; x < wallGrid[y].length; x++) {
        int id = wallGrid[y][x];
        if (id >= 90) {
          // Assuming 90+ are doors based on your previous code
          doors['$x,$y'] = Door(x: x, y: y, mapId: id);
        }
      }
    }
  }

  void update(Duration elapsed) {
    for (final door in doors.values) {
      door.update(elapsed.inMilliseconds);
    }
  }

  void handleInteraction(double playerX, double playerY, double playerAngle) {
    int targetX = (playerX + math.cos(playerAngle)).toInt();
    int targetY = (playerY + math.sin(playerAngle)).toInt();

    String key = '$targetX,$targetY';
    if (doors.containsKey(key)) {
      doors[key]!.interact();
    }
  }

  // Helper method for the raycaster
  Map<String, double> getOffsetsForRenderer() {
    Map<String, double> offsets = {};
    for (var entry in doors.entries) {
      if (entry.value.offset > 0.0) {
        offsets[entry.key] = entry.value.offset;
      }
    }
    return offsets;
  }

  void tryOpenDoor(int x, int y) {
    String key = '$x,$y';
    if (doors.containsKey(key)) {
      // If it's closed or closing, interact() will usually start it opening
      if (doors[key]!.offset == 0.0) {
        doors[key]!.interact();
      }
    }
  }

  bool isDoorOpenEnough(int x, int y) {
    String key = '$x,$y';
    if (doors.containsKey(key)) {
      // 0.7 offset means 70% open, similar to the original engine's check
      return doors[key]!.offset > 0.7;
    }
    return false; // Not a door we manage
  }
}
