package com.thebrokenrail.twine;

import com.thebrokenrail.twine.advancement.BoatCriterion;
import com.thebrokenrail.twine.block.CreativeItemSpawnerBlock;
import com.thebrokenrail.twine.block.CreativeURLOpenerBlock;
import com.thebrokenrail.twine.block.GlowingObsidianBlock;
import com.thebrokenrail.twine.item.BackpackItem;
import com.thebrokenrail.twine.item.DivinerItem;
import com.thebrokenrail.twine.mixin.CriteriaHook;
import com.thebrokenrail.twine.util.backpack.gui.BackpackScreenHandler;
import com.thebrokenrail.twine.util.boat.gui.BoatCraftingScreenHandler;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.fabricmc.fabric.api.tag.TagRegistry;
import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.CraftingScreenHandler;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class Twine implements ModInitializer {
    public static final String NAMESPACE = "twine";

    public static final GlowingObsidianBlock GLOWING_OBSIDIAN = new GlowingObsidianBlock();

    public static final CreativeItemSpawnerBlock CREATIVE_ITEM_SPAWNER_BLOCK = new CreativeItemSpawnerBlock();

    public static final CreativeURLOpenerBlock CREATIVE_URL_OPENER_BLOCK = new CreativeURLOpenerBlock();

    public static final ItemGroup ITEM_GROUP = FabricItemGroupBuilder.build(new Identifier(NAMESPACE, "item_group"), () -> new ItemStack(GLOWING_OBSIDIAN));

    public static final Item SMALL_BACKPACK = new BackpackItem(false);
    public static final Item LARGE_BACKPACK = new BackpackItem(true);

    public static final Item DIVINER = new DivinerItem();

    public static final Tag<Block> ARTIFICIAL_BLOCKS_TAG = TagRegistry.block(new Identifier(NAMESPACE, "artificial_blocks"));

    public static final int STAGE_COUNT = 6;
    public static final int STAGE_TIME = 24000;

    public static final int AI_RANGE = 16;
    public static final int AI_MAX_Y_DIFFERENCE = 4;

    public static ScreenHandlerType<? extends GenericContainerScreenHandler> BACKPACK_SCREEN_TYPE;
    public static ScreenHandlerType<? extends CraftingScreenHandler> BOAT_CRAFTING_SCREEN_TYPE;

    public static BoatCriterion BOAT_CRITERION = CriteriaHook.callRegister(new BoatCriterion());

    @Override
    public void onInitialize() {
        BACKPACK_SCREEN_TYPE = ScreenHandlerRegistry.registerExtended(new Identifier(NAMESPACE, "backpack"), BackpackScreenHandler::new);
        BOAT_CRAFTING_SCREEN_TYPE = ScreenHandlerRegistry.registerSimple(new Identifier(NAMESPACE, "boat_crafting"), BoatCraftingScreenHandler::new);

        Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "small_backpack"), SMALL_BACKPACK);
        Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "large_backpack"), LARGE_BACKPACK);

        Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "diviner"), DIVINER);

        Registry.register(Registry.BLOCK, new Identifier(NAMESPACE, "glowing_obsidian"), GLOWING_OBSIDIAN);
        Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "glowing_obsidian"), new BlockItem(GLOWING_OBSIDIAN, new Item.Settings().group(ITEM_GROUP)));

        Registry.register(Registry.BLOCK, new Identifier(NAMESPACE, "creative_item_spawner"), CREATIVE_ITEM_SPAWNER_BLOCK);
        Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "creative_item_spawner"), new BlockItem(CREATIVE_ITEM_SPAWNER_BLOCK, new Item.Settings().group(ITEM_GROUP)));

        Registry.register(Registry.BLOCK, new Identifier(NAMESPACE, "creative_url_opener"), CREATIVE_URL_OPENER_BLOCK);
        Registry.register(Registry.ITEM, new Identifier(NAMESPACE, "creative_url_opener"), new BlockItem(CREATIVE_URL_OPENER_BLOCK, new Item.Settings().group(ITEM_GROUP)));
    }
}
