package com.tiedup.remake.worldgen;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.levelgen.structure.StructureType;
import net.minecraft.world.level.levelgen.structure.pieces.StructurePiecesBuilder;

/**
 * Custom structure that places kidnapper camp tents together.
 *
 * Phase: Kidnapper Revamp - Cell System
 *
 * Places three tents:
 * - kidnap_tent (with spawner and loot)
 * - cell_tent (prisoner cell)
 * - kidnap_tent_trader (with trader and maid spawn)
 */
public class KidnapperCampStructure extends AbstractKidnapperStructure {

    public static final Codec<KidnapperCampStructure> CODEC =
        RecordCodecBuilder.create(instance ->
            instance
                .group(settingsCodec(instance))
                .apply(instance, KidnapperCampStructure::new)
        );

    public KidnapperCampStructure(StructureSettings settings) {
        super(settings);
    }

    @Override
    protected int getCheckRadius() {
        return 8;
    }

    @Override
    protected int getMaxHeightDifference() {
        return 4;
    }

    @Override
    protected void generatePieces(
        StructurePiecesBuilder builder,
        GenerationContext context,
        BlockPos centerPos,
        Rotation rotation
    ) {
        // Calculate offsets for tent placement (triangular layout)
        int tentOffset = 10;
        boolean xFirst = context.random().nextBoolean();

        // Cell tent offset
        int cellOffsetX = xFirst ? tentOffset : 0;
        int cellOffsetZ = xFirst ? 0 : tentOffset;

        // Trader tent offset (opposite side or diagonal)
        int traderOffsetX = xFirst ? 0 : tentOffset;
        int traderOffsetZ = xFirst ? tentOffset : 0;

        // Place kidnap_tent at center
        BlockPos kidnapTentPos = calculatePiecePosition(
            context,
            centerPos,
            0,
            0,
            rotation
        );
        builder.addPiece(
            new KidnapperCampPiece(
                context.structureTemplateManager(),
                ResourceLocation.fromNamespaceAndPath("tiedup", "kidnap_tent"),
                kidnapTentPos,
                rotation
            )
        );

        // Place cell_tent with offset
        BlockPos cellTentPos = calculatePiecePosition(
            context,
            centerPos,
            cellOffsetX,
            cellOffsetZ,
            rotation
        );
        builder.addPiece(
            new KidnapperCampPiece(
                context.structureTemplateManager(),
                ResourceLocation.fromNamespaceAndPath("tiedup", "cell_tent"),
                cellTentPos,
                rotation
            )
        );

        // Place kidnap_tent_trader with offset (trader and maid spawn)
        BlockPos traderTentPos = calculatePiecePosition(
            context,
            centerPos,
            traderOffsetX,
            traderOffsetZ,
            rotation
        );
        builder.addPiece(
            new KidnapperCampPiece(
                context.structureTemplateManager(),
                ResourceLocation.fromNamespaceAndPath(
                    "tiedup",
                    "kidnap_tent_trader"
                ),
                traderTentPos,
                rotation
            )
        );
    }

    @Override
    public StructureType<?> type() {
        return ModStructures.KIDNAPPER_CAMP.get();
    }
}
