package p.studio.workspaces.assets.metadata;

import java.nio.file.Path;
import java.util.List;
import java.util.Objects;

public record AssetStudioSceneBankMetadata(
        int mapWidth,
        int mapHeight,
        int layerCount,
        List<AssetStudioSceneLayerBinding> layerBindings,
        Path supportFile) {

    public AssetStudioSceneBankMetadata {
        layerBindings = List.copyOf(Objects.requireNonNull(layerBindings, "layerBindings"));
        supportFile = Objects.requireNonNull(supportFile, "supportFile").toAbsolutePath().normalize();
        if (mapWidth <= 0 || mapHeight <= 0) {
            throw new IllegalArgumentException("map dimensions must be positive");
        }
        if (layerCount != 4) {
            throw new IllegalArgumentException("layerCount must stay fixed at 4");
        }
        if (layerBindings.size() != layerCount) {
            throw new IllegalArgumentException("layerBindings must match layerCount");
        }
    }

    public long enabledLayerCount() {
        return layerBindings.stream().filter(AssetStudioSceneLayerBinding::enabled).count();
    }

    public AssetStudioSceneBankMetadata withUpdatedBindings(final List<AssetStudioSceneLayerBinding> assetStudioSceneLayerBindings) {
        return new AssetStudioSceneBankMetadata(
                mapWidth,
                mapHeight,
                layerCount,
                assetStudioSceneLayerBindings,
                supportFile);
    }
}
