/*
 * 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;

import com.remains_cave.dungeon.room.RoomManager;
import com.remains_cave.dungeon.room.RoomState;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.StructureManager;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.levelgen.structure.StructurePiece;
import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext;
import com.remains_cave.RemainsCaveMod;
import com.remains_cave.dungeon.config.DungeonRoutineConfig;
import com.remains_cave.dungeon.config.RoutineConfigLoader;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;

public class DungeonPiece extends StructurePiece {
    private final BlockPos origin;
    private final String routineConfigName;

    public DungeonPiece(BlockPos origin, String routineConfigName) {
        super(RemainsCaveMod.DUNGEON_PIECE_TYPE.get(), 0, new BoundingBox(origin));
        this.origin = origin;
        this.routineConfigName = routineConfigName;
    }

    public DungeonPiece(StructurePieceSerializationContext context, CompoundTag tag) {
        super(RemainsCaveMod.DUNGEON_PIECE_TYPE.get(), tag);
        this.origin = new BlockPos(tag.getInt("X"), tag.getInt("Y"), tag.getInt("Z"));
        this.routineConfigName = tag.getString("ConfigName");
    }

    @Override
    protected void addAdditionalSaveData(@NotNull StructurePieceSerializationContext context, CompoundTag tag) {
        tag.putInt("X", origin.getX());
        tag.putInt("Y", origin.getY());
        tag.putInt("Z", origin.getZ());
        tag.putString("ConfigName", routineConfigName);
    }

    @Override
    public void postProcess(@NotNull WorldGenLevel level, @NotNull StructureManager structureManager,
                            @NotNull ChunkGenerator chunkGenerator, @NotNull RandomSource random,
                            @NotNull BoundingBox boundingBox, @NotNull ChunkPos chunkPos, @NotNull BlockPos blockPos) {
        ServerLevel serverLevel;
        if (level instanceof ServerLevel) {
            serverLevel = (ServerLevel) level;
        } else {
            ServerLevel raw = ((ServerLevelAccessor) level).getLevel();
            if (raw instanceof ServerLevel) {
                serverLevel = raw;
            } else {
                serverLevel = null;
            }
        }
        if (serverLevel == null) return;

        String path = "data/remains_cave/dungeon_routine/" + routineConfigName + ".yaml";
        DungeonRoutineConfig config;
        try {
            config = RoutineConfigLoader.load(path);
        } catch (Exception e) {
            return;
        }

        long seed = random.nextLong();

        serverLevel.getServer().execute(() -> {
            DungeonGenerator generator = new DungeonGenerator(serverLevel, origin, config, routineConfigName, seed);
            generator.generate();
        });
    }
}