/*
 * SPDX-FileCopyrightText: 2026 KOKA99CAB
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

package net.koka99cab.portals.portal;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import net.koka99cab.portals.PortalsMod;
import net.koka99cab.portals.registry.ModBlocks;
import net.koka99cab.portals.world.ModDimensions;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;

public final class PortalDefinitions {
	private static final List<PortalDefinition> DEFINITIONS = new ArrayList<>();

	public static final PortalDefinition ORE_CAVES = register(new PortalDefinition(
			PortalsMod.id("ore_caves"),
			Blocks.STONE,
			Items.FLINT_AND_STEEL,
			ModBlocks.ORE_PORTAL,
			ModDimensions.ORE_CAVES
	));
	public static final PortalDefinition OVERWORLD2 = register(new PortalDefinition(
			PortalsMod.id("overworld2"),
			Blocks.DIRT,
			Items.FLINT_AND_STEEL,
			ModBlocks.OVERWORLD2_PORTAL,
			ModDimensions.OVERWORLD2,
			state -> state.is(Blocks.DIRT) || state.is(Blocks.GRASS_BLOCK)
	));
	public static final PortalDefinition SHEEP_LAND = register(new PortalDefinition(
			PortalsMod.id("sheep_land"),
			Blocks.WHITE_WOOL,
			Items.FLINT_AND_STEEL,
			ModBlocks.SHEEP_LAND_PORTAL,
			ModDimensions.SHEEP_LAND
	));
	public static final PortalDefinition SKELETON_WASTE = register(new PortalDefinition(
			PortalsMod.id("skeleton_waste"),
			Blocks.BONE_BLOCK,
			Items.FLINT_AND_STEEL,
			ModBlocks.SKELETON_WASTE_PORTAL,
			ModDimensions.SKELETON_WASTE
	));
	public static final PortalDefinition PAINTED_CANYONS = register(new PortalDefinition(
			PortalsMod.id("painted_canyons"),
			Blocks.TERRACOTTA,
			Items.FLINT_AND_STEEL,
			ModBlocks.PAINTED_CANYONS_PORTAL,
			ModDimensions.PAINTED_CANYONS,
			PortalFramePredicates.TERRACOTTA
	));

	private PortalDefinitions() {
	}

	public static void init() {
		PortalsMod.LOGGER.debug("Registered {} portal definitions", DEFINITIONS.size());
	}

	public static Optional<PortalDefinition> find(Item activationItem, net.minecraft.world.level.block.state.BlockState frameState) {
		return DEFINITIONS.stream()
				.filter(definition -> definition.activationItem() == activationItem)
				.filter(definition -> definition.matchesFrame(frameState))
				.findFirst();
	}

	private static PortalDefinition register(PortalDefinition definition) {
		DEFINITIONS.add(definition);
		return definition;
	}
}
