package minecrafttransportsimulator.items.components;

import java.util.List;
import java.util.Locale;

import minecrafttransportsimulator.baseclasses.Point3D;
import minecrafttransportsimulator.blocks.components.ABlockBase.Axis;
import minecrafttransportsimulator.mcinterface.AWrapperWorld;
import minecrafttransportsimulator.mcinterface.IWrapperItemStack;
import minecrafttransportsimulator.mcinterface.IWrapperNBT;
import minecrafttransportsimulator.mcinterface.IWrapperPlayer;
import minecrafttransportsimulator.mcinterface.InterfaceManager;

/**
 * Base item class for all MTS items.  Contains multiple methods to define the item's behavior,
 * such as display name, additional text to add to the tooltip, how the item handles left and
 * right-click usage, and so on.
 *
 * @author don_bruce
 */
public abstract class AItemBase {

    /**
     * Returns the registration name of this item.  This MUST be unique for all items, or Bad Stuff will happen.
     */
    public String getRegistrationName() {
        return getClass().getSimpleName().substring("Item".length()).toLowerCase(Locale.ROOT);
    }

    /**
     * Returns the name of this item.  Will be displayed to the player in-game, but is NOT used
     * for item registration, so may change depending on item state or language.
     */
    public String getItemName() {
        return getRegistrationName();
    }

    /**
     * Returns true if this item can break blocks.  Only called in creative for
     * allowing normal block-breaking to occur when this item is held.
     */
    public boolean canBreakBlocks() {
        return true;
    }

    /**
     * Called when the item tooltip is being displayed.  The passed-in list will contain
     * all the lines in the tooltip, so add or remove lines as you see fit.  If you don't
     * want to add any lines just leave this method blank. Data is assured not to be null.
     * However, this does not mean the data block will be populated with values.  If the
     * item is fresh from crafting, it may not have any data.
     */
    public abstract void addTooltipLines(List<String> tooltipLines, IWrapperNBT data);

    /**
     * Called when the player clicks a block with this item.  The position of the block
     * clicked and what axis it was hit at is passed-in for reference.  If this item did a thing
     * due to this clicking, return true, as this prevents calling the block's clicked method.
     */
    public boolean onBlockClicked(AWrapperWorld world, IWrapperPlayer player, Point3D position, Axis axis) {
        if (this instanceof IItemBlock) {
            return ((IItemBlock) this).placeBlock(world, player, position, axis);
        } else {
            return false;
        }
    }

    /**
     * Called when the player right-clicks with this item.  {@link AItemBase#onBlockClicked(AWrapperWorld, IWrapperPlayer, Point3D, Axis)}
     * is called before this method, and if and only if that method returns false will this method be called.
     * If this item does something, return true.
     */
    public boolean onUsed(AWrapperWorld world, IWrapperPlayer player) {
        return false;
    }

    /**
     * Returns the stack size for this item.  Defaults to 1, as most items shouldn't be stack-able.
     */
    public int getStackSize() {
        return 1;
    }

    /**
     * Returns true if this item is to be auto-generated by the Builder system.
     * Normally true, but may be false to allow other mods to manually create
     * their own itmes without the MC registration or creative tabs the builder provides.
     */
    public boolean autoGenerate() {
        return true;
    }

    /**
     * Returns a stack with a single item of this type.
     */
    public IWrapperItemStack getNewStack(IWrapperNBT data) {
        return InterfaceManager.coreInterface.getAutoGeneratedStack(this, data);
    }

    /**
     * Gets the ID of the creative tab for this item to be displayed on.  Tabs are auto-created as required.
     */
    public String getCreativeTabID() {
        return InterfaceManager.coreModID;
    }
}
