/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */


package com.remains_cave.dungeon.room;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.AABB;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class RoomState {
    private final String roomId;
    private final Set<BlockPos> doors = ConcurrentHashMap.newKeySet(); // 线程安全
    private boolean bossActive = false;
    private volatile AABB protectionArea;
    private boolean unlocked = false;

    // RoomState.java 构造器
    public RoomState(String roomId) {
        this.roomId = roomId;
        this.stepId = "";
        this.parentStaircaseId = "";
        this.emblemActive = false;
    }

    public String getRoomId() { return roomId; }
    public boolean isBossActive() { return bossActive; }
    public void setBossActive(boolean active) { this.bossActive = active; }
    public Set<BlockPos> getDoors() { return doors; }
    public void addDoor(BlockPos pos) { doors.add(pos); }
    public void removeDoor(BlockPos pos) { doors.remove(pos); }
    public void setProtectionArea(AABB area) { this.protectionArea = area; }
    public AABB getProtectionArea() { return protectionArea; }

    // 新增 getter/setter
    public boolean isUnlocked() { return unlocked; }
    public void setUnlocked(boolean unlocked) { this.unlocked = unlocked; }

    public boolean isPositionProtected(BlockPos pos) {
        if (unlocked) return false;
        return protectionArea != null && protectionArea.contains(pos.getX(), pos.getY(), pos.getZ());
    }

    private String stepId = "";          // 房间类型标识（如 "staircase", "event_room_l"）
    private String parentStaircaseId = ""; // 所属的 staircase 房间 ID（仅 event 房间有）
    private boolean emblemActive = false;  // 当前房间是否有激活的 emblem（用于 staircase 自身）
    public String getStepId() { return stepId; }
    public void setStepId(String id) { this.stepId = id; }

    public String getParentStaircaseId() { return parentStaircaseId; }
    public void setParentStaircaseId(String id) { this.parentStaircaseId = id; }

    public boolean isEmblemActive() { return emblemActive; }
    public void setEmblemActive(boolean active) { this.emblemActive = active; }
    /**
     * 判断当前房间的骷髅是否应该获得 KingsAura 效果
     * @param level 服务端世界
     * @return 应该添加效果则返回 true
     */
    public boolean shouldApplyKingsAura(ServerLevel level) {
        // 楼梯间自身
        if (stepId.equals("staircase") || stepId.equals("staircase_alt_1")) {
            return emblemActive;
        }
        // 只允许指定的 event 房间受父楼梯间影响
        if (stepId.equals("event_room_l") || stepId.equals("event_room_r") ||
                stepId.equals("event_room_l_alt") || stepId.equals("event_room_r_alt")) {
            if (!parentStaircaseId.isEmpty()) {
                RoomManager rm = RoomManager.get(level);
                RoomState parent = rm.getRooms().get(parentStaircaseId);
                return parent != null && parent.isEmblemActive();
            }
        }
        return false;
    }

    public synchronized void expandProtectionArea(AABB newArea) {
        if (protectionArea == null) {
            protectionArea = newArea;
        } else {
            double minX = Math.min(protectionArea.minX, newArea.minX);
            double minY = Math.min(protectionArea.minY, newArea.minY);
            double minZ = Math.min(protectionArea.minZ, newArea.minZ);
            double maxX = Math.max(protectionArea.maxX, newArea.maxX);
            double maxY = Math.max(protectionArea.maxY, newArea.maxY);
            double maxZ = Math.max(protectionArea.maxZ, newArea.maxZ);
            protectionArea = new AABB(minX, minY, minZ, maxX, maxY, maxZ);
        }
    }
}