/*
 * This file is part of ComputerCraft - http://www.computercraft.info
 * Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
 * Send enquiries to dratcliffe@gmail.com
 */
package dan200.computercraft.data;

import dan200.computercraft.ComputerCraft;
import dan200.computercraft.data.Tags.CCTags;
import dan200.computercraft.shared.PocketUpgrades;
import dan200.computercraft.shared.Registry;
import dan200.computercraft.shared.TurtleUpgrades;
import dan200.computercraft.shared.common.IColouredItem;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.pocket.items.PocketComputerItemFactory;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import dan200.computercraft.shared.util.Colour;
import dan200.computercraft.shared.util.ImpostorRecipe;
import dan200.computercraft.shared.util.ImpostorShapelessRecipe;
import net.minecraft.advancements.criterion.InventoryChangeTrigger;
import net.minecraft.advancements.criterion.ItemPredicate;
import net.minecraft.block.Blocks;
import net.minecraft.data.*;
import net.minecraft.item.*;
import net.minecraft.tags.Tag;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.Tags;

import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.function.Consumer;

public class Recipes extends RecipeProvider
{
    public Recipes( DataGenerator generator )
    {
        super( generator );
    }

    @Override
    protected void buildShapelessRecipes( @Nonnull Consumer<IFinishedRecipe> add )
    {
        basicRecipes( add );
        diskColours( add );
        pocketUpgrades( add );
        turtleUpgrades( add );
    }

    /**
     * Register a crafting recipe for a disk of every dye colour.
     *
     * @param add The callback to add recipes.
     */
    private void diskColours( @Nonnull Consumer<IFinishedRecipe> add )
    {
        for( Colour colour : Colour.VALUES )
        {
            ShapelessRecipeBuilder
                .shapeless( Registry.ModItems.DISK.get() )
                .requires( Tags.Items.DUSTS_REDSTONE )
                .requires( Items.PAPER )
                .requires( DyeItem.byColor( ofColour( colour ) ) )
                .group( "computercraft:disk" )
                .unlocks( "has_drive", inventoryChange( Registry.ModBlocks.DISK_DRIVE.get() ) )
                .save( RecipeWrapper.wrap(
                    ImpostorShapelessRecipe.SERIALIZER, add,
                    x -> x.putInt( IColouredItem.NBT_COLOUR, colour.getHex() )
                ), new ResourceLocation( ComputerCraft.MOD_ID, "disk_" + (colour.ordinal() + 1) ) );
        }
    }

    /**
     * Register a crafting recipe for each turtle upgrade.
     *
     * @param add The callback to add recipes.
     */
    private void turtleUpgrades( @Nonnull Consumer<IFinishedRecipe> add )
    {
        for( ComputerFamily family : ComputerFamily.values() )
        {
            ItemStack base = TurtleItemFactory.create( -1, null, -1, family, null, null, 0, null );
            if( base.isEmpty() ) continue;

            String nameId = family.name().toLowerCase( Locale.ROOT );

            TurtleUpgrades.getVanillaUpgrades().forEach( upgrade -> {
                ItemStack result = TurtleItemFactory.create( -1, null, -1, family, null, upgrade, -1, null );
                ShapedRecipeBuilder
                    .shaped( result.getItem() )
                    .group( String.format( "%s:turtle_%s", ComputerCraft.MOD_ID, nameId ) )
                    .pattern( "#T" )
                    .define( '#', base.getItem() )
                    .define( 'T', upgrade.getCraftingItem().getItem() )
                    .unlocks( "has_items",
                        inventoryChange( base.getItem(), upgrade.getCraftingItem().getItem() ) )
                    .save(
                        RecipeWrapper.wrap( ImpostorRecipe.SERIALIZER, add, result.getTag() ),
                        new ResourceLocation( ComputerCraft.MOD_ID, String.format( "turtle_%s/%s/%s",
                            nameId, upgrade.getUpgradeID().getNamespace(), upgrade.getUpgradeID().getPath()
                        ) )
                    );
            } );
        }
    }

    /**
     * Register a crafting recipe for each pocket upgrade.
     *
     * @param add The callback to add recipes.
     */
    private void pocketUpgrades( @Nonnull Consumer<IFinishedRecipe> add )
    {
        for( ComputerFamily family : ComputerFamily.values() )
        {
            ItemStack base = PocketComputerItemFactory.create( -1, null, -1, family, null );
            if( base.isEmpty() ) continue;

            String nameId = family.name().toLowerCase( Locale.ROOT );

            PocketUpgrades.getVanillaUpgrades().forEach( upgrade -> {
                ItemStack result = PocketComputerItemFactory.create( -1, null, -1, family, null );
                ShapedRecipeBuilder
                    .shaped( result.getItem() )
                    .group( String.format( "%s:pocket_%s", ComputerCraft.MOD_ID, nameId ) )
                    .pattern( "#" )
                    .pattern( "P" )
                    .define( '#', base.getItem() )
                    .define( 'P', upgrade.getCraftingItem().getItem() )
                    .unlocks( "has_items",
                        inventoryChange( base.getItem(), upgrade.getCraftingItem().getItem() ) )
                    .save(
                        RecipeWrapper.wrap( ImpostorRecipe.SERIALIZER, add, result.getTag() ),
                        new ResourceLocation( ComputerCraft.MOD_ID, String.format( "pocket_%s/%s/%s",
                            nameId, upgrade.getUpgradeID().getNamespace(), upgrade.getUpgradeID().getPath()
                        ) )
                    );
            } );
        }
    }

    private void basicRecipes( @Nonnull Consumer<IFinishedRecipe> add )
    {
        ShapedRecipeBuilder
            .shaped( Registry.ModItems.CABLE.get(), 6 )
            .pattern( " # " )
            .pattern( "#R#" )
            .pattern( " # " )
            .define( '#', Tags.Items.STONE )
            .define( 'R', Tags.Items.DUSTS_REDSTONE )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .unlocks( "has_modem", inventoryChange( CCTags.COMPUTER ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.COMPUTER_NORMAL.get() )
            .pattern( "###" )
            .pattern( "#R#" )
            .pattern( "#G#" )
            .define( '#', Tags.Items.STONE )
            .define( 'R', Tags.Items.DUSTS_REDSTONE )
            .define( 'G', Tags.Items.GLASS_PANES )
            .unlocks( "has_redstone", inventoryChange( Tags.Items.DUSTS_REDSTONE ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.COMPUTER_ADVANCED.get() )
            .pattern( "###" )
            .pattern( "#R#" )
            .pattern( "#G#" )
            .define( '#', Tags.Items.INGOTS_GOLD )
            .define( 'R', Tags.Items.DUSTS_REDSTONE )
            .define( 'G', Tags.Items.GLASS_PANES )
            .unlocks( "has_components", inventoryChange( Items.REDSTONE, Items.GOLD_INGOT ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.COMPUTER_COMMAND.get() )
            .pattern( "###" )
            .pattern( "#R#" )
            .pattern( "#G#" )
            .define( '#', Tags.Items.INGOTS_GOLD )
            .define( 'R', Blocks.COMMAND_BLOCK )
            .define( 'G', Tags.Items.GLASS_PANES )
            .unlocks( "has_components", inventoryChange( Blocks.COMMAND_BLOCK ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.DISK_DRIVE.get() )
            .pattern( "###" )
            .pattern( "#R#" )
            .pattern( "#R#" )
            .define( '#', Tags.Items.STONE )
            .define( 'R', Tags.Items.DUSTS_REDSTONE )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.MONITOR_NORMAL.get() )
            .pattern( "###" )
            .pattern( "#G#" )
            .pattern( "###" )
            .define( '#', Tags.Items.STONE )
            .define( 'G', Tags.Items.GLASS_PANES )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.MONITOR_ADVANCED.get(), 4 )
            .pattern( "###" )
            .pattern( "#G#" )
            .pattern( "###" )
            .define( '#', Tags.Items.INGOTS_GOLD )
            .define( 'G', Tags.Items.GLASS_PANES )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModItems.POCKET_COMPUTER_NORMAL.get() )
            .pattern( "###" )
            .pattern( "#A#" )
            .pattern( "#G#" )
            .define( '#', Tags.Items.STONE )
            .define( 'A', Items.GOLDEN_APPLE )
            .define( 'G', Tags.Items.GLASS_PANES )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .unlocks( "has_apple", inventoryChange( Items.GOLDEN_APPLE ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModItems.POCKET_COMPUTER_ADVANCED.get() )
            .pattern( "###" )
            .pattern( "#A#" )
            .pattern( "#G#" )
            .define( '#', Tags.Items.INGOTS_GOLD )
            .define( 'A', Items.GOLDEN_APPLE )
            .define( 'G', Tags.Items.GLASS_PANES )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .unlocks( "has_apple", inventoryChange( Items.GOLDEN_APPLE ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.PRINTER.get() )
            .pattern( "###" )
            .pattern( "#R#" )
            .pattern( "#D#" )
            .define( '#', Tags.Items.STONE )
            .define( 'R', Tags.Items.DUSTS_REDSTONE )
            .define( 'D', Tags.Items.DYES )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.SPEAKER.get() )
            .pattern( "###" )
            .pattern( "#N#" )
            .pattern( "#R#" )
            .define( '#', Tags.Items.STONE )
            .define( 'N', Blocks.NOTE_BLOCK )
            .define( 'R', Tags.Items.DUSTS_REDSTONE )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModItems.WIRED_MODEM.get() )
            .pattern( "###" )
            .pattern( "#R#" )
            .pattern( "###" )
            .define( '#', Tags.Items.STONE )
            .define( 'R', Tags.Items.DUSTS_REDSTONE )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .unlocks( "has_cable", inventoryChange( Registry.ModItems.CABLE.get() ) )
            .save( add );

        ShapelessRecipeBuilder
            .shapeless( Registry.ModBlocks.WIRED_MODEM_FULL.get() )
            .requires( Registry.ModItems.WIRED_MODEM.get() )
            .unlocks( "has_modem", inventoryChange( CCTags.WIRED_MODEM ) )
            .save( add, new ResourceLocation( ComputerCraft.MOD_ID, "wired_modem_full_from" ) );
        ShapelessRecipeBuilder
            .shapeless( Registry.ModItems.WIRED_MODEM.get() )
            .requires( Registry.ModBlocks.WIRED_MODEM_FULL.get() )
            .unlocks( "has_modem", inventoryChange( CCTags.WIRED_MODEM ) )
            .save( add, new ResourceLocation( ComputerCraft.MOD_ID, "wired_modem_full_to" ) );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.WIRELESS_MODEM_NORMAL.get() )
            .pattern( "###" )
            .pattern( "#E#" )
            .pattern( "###" )
            .define( '#', Tags.Items.STONE )
            .define( 'E', Tags.Items.ENDER_PEARLS )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .save( add );

        ShapedRecipeBuilder
            .shaped( Registry.ModBlocks.WIRELESS_MODEM_ADVANCED.get() )
            .pattern( "###" )
            .pattern( "#E#" )
            .pattern( "###" )
            .define( '#', Tags.Items.INGOTS_GOLD )
            .define( 'E', Items.ENDER_EYE )
            .unlocks( "has_computer", inventoryChange( CCTags.COMPUTER ) )
            .unlocks( "has_wireless", inventoryChange( Registry.ModBlocks.WIRELESS_MODEM_NORMAL.get() ) )
            .save( add );
    }

    private static DyeColor ofColour( Colour colour )
    {
        return DyeColor.byId( 15 - colour.ordinal() );
    }

    private static InventoryChangeTrigger.Instance inventoryChange( Tag<Item> stack )
    {
        return InventoryChangeTrigger.Instance.hasItem( ItemPredicate.Builder.item().of( stack ).build() );
    }

    private static InventoryChangeTrigger.Instance inventoryChange( IItemProvider... stack )
    {
        return InventoryChangeTrigger.Instance.hasItem( stack );
    }
}
