/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.techhider;

import de.steamwar.Reflection;
import net.minecraft.core.IdMapper;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import org.bukkit.Material;
import org.bukkit.Registry;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;

import java.util.HashSet;
import java.util.Set;

public class BlockIds {
    public static final BlockIds impl = new BlockIds();

    public int materialToId(Material material) {
        return getCombinedId(getBlock(material).defaultBlockState());
    }

    private static final FluidState water = Fluids.WATER.getSource(false);

    public Set<Integer> materialToAllIds(Material material) {
        Set<Integer> ids = new HashSet<>();

        for (BlockState data : getBlock(material).getStateDefinition().getPossibleStates()) {
            ids.add(getCombinedId(data));
        }

        if (material == Material.WATER) {
            for (Block block : BuiltInRegistries.BLOCK) {
                for (BlockState data : block.getStateDefinition().getPossibleStates()) {
                    if (data.getFluidState() == water) {
                        ids.add(getCombinedId(data));
                    }
                }
            }
        }

        return ids;
    }

    private Block getBlock(Material material) {
        return CraftMagicNumbers.getBlock(material);
    }

    public int getCombinedId(BlockState blockData) {
        return Block.getId(blockData);
    }
}
