package com.tiedup.remake.cells;

import java.util.*;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import org.jetbrains.annotations.Nullable;

/**
 * Immutable result container returned by the flood-fill algorithm.
 *
 * Either a success (with geometry and detected features) or a failure (with an error translation key).
 */
public class FloodFillResult {

    private final boolean success;

    @Nullable
    private final String errorKey;

    // Geometry
    private final Set<BlockPos> interior;
    private final Set<BlockPos> walls;

    @Nullable
    private final Direction interiorFace;

    // Auto-detected features
    private final List<BlockPos> beds;
    private final List<BlockPos> petBeds;
    private final List<BlockPos> anchors;
    private final List<BlockPos> doors;
    private final List<BlockPos> linkedRedstone;

    private FloodFillResult(
        boolean success,
        @Nullable String errorKey,
        Set<BlockPos> interior,
        Set<BlockPos> walls,
        @Nullable Direction interiorFace,
        List<BlockPos> beds,
        List<BlockPos> petBeds,
        List<BlockPos> anchors,
        List<BlockPos> doors,
        List<BlockPos> linkedRedstone
    ) {
        this.success = success;
        this.errorKey = errorKey;
        this.interior = Collections.unmodifiableSet(interior);
        this.walls = Collections.unmodifiableSet(walls);
        this.interiorFace = interiorFace;
        this.beds = Collections.unmodifiableList(beds);
        this.petBeds = Collections.unmodifiableList(petBeds);
        this.anchors = Collections.unmodifiableList(anchors);
        this.doors = Collections.unmodifiableList(doors);
        this.linkedRedstone = Collections.unmodifiableList(linkedRedstone);
    }

    public static FloodFillResult success(
        Set<BlockPos> interior,
        Set<BlockPos> walls,
        Direction interiorFace,
        List<BlockPos> beds,
        List<BlockPos> petBeds,
        List<BlockPos> anchors,
        List<BlockPos> doors,
        List<BlockPos> linkedRedstone
    ) {
        return new FloodFillResult(
            true,
            null,
            interior,
            walls,
            interiorFace,
            beds,
            petBeds,
            anchors,
            doors,
            linkedRedstone
        );
    }

    public static FloodFillResult failure(String errorKey) {
        return new FloodFillResult(
            false,
            errorKey,
            Collections.emptySet(),
            Collections.emptySet(),
            null,
            Collections.emptyList(),
            Collections.emptyList(),
            Collections.emptyList(),
            Collections.emptyList(),
            Collections.emptyList()
        );
    }

    // --- Getters ---

    public boolean isSuccess() {
        return success;
    }

    @Nullable
    public String getErrorKey() {
        return errorKey;
    }

    public Set<BlockPos> getInterior() {
        return interior;
    }

    public Set<BlockPos> getWalls() {
        return walls;
    }

    @Nullable
    public Direction getInteriorFace() {
        return interiorFace;
    }

    public List<BlockPos> getBeds() {
        return beds;
    }

    public List<BlockPos> getPetBeds() {
        return petBeds;
    }

    public List<BlockPos> getAnchors() {
        return anchors;
    }

    public List<BlockPos> getDoors() {
        return doors;
    }

    public List<BlockPos> getLinkedRedstone() {
        return linkedRedstone;
    }
}
