// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2022
//                www.magicbane.com


package engine.devcmd.cmds;

import engine.devcmd.AbstractDevCmd;
import engine.gameManager.DbManager;
import engine.mbEnums.ItemType;
import engine.mbEnums.OwnerType;
import engine.objects.*;
import engine.powers.EffectsBase;

/**
 * @author Eighty
 */
public class MakeItemCmd extends AbstractDevCmd {

    public MakeItemCmd() {
        super("makeitem");
    }

    @Override
    protected void _doCmd(PlayerCharacter pc, String[] words,
                          AbstractGameObject target) {

        if (words.length < 3 || words.length > 5) {
            this.sendUsage(pc);
            return;
        }

        int quantity = 1;
        if (words.length > 3) {
            try {
                quantity = Integer.parseInt(words[3]);
            } catch (NumberFormatException e) {
                throwbackError(pc, "Quantity must be a number, " + words[3] + " is invalid");
                return;
            }
            if (quantity < 1 || quantity > 100)
                quantity = 1;
        }

        int numItems = 1;
        if (words.length > 4) {
            try {
                numItems = Integer.parseInt(words[4]);
            } catch (NumberFormatException e) {
                throwbackError(pc, "numResources must be a number, " + words[4] + " is invalid");
                return;
            }
            numItems = (numItems < 1) ? 1 : numItems;
            numItems = (numItems > 5000) ? 5000 : numItems;
        }

        int templateID;
        ItemTemplate template;
        try {
            templateID = Integer.parseInt(words[0]);
            template = ItemTemplate.templates.get(words[0].toLowerCase());

            if (template == null) {
                throwbackError(pc, "Supplied type " + words[0]
                        + " failed to parse to an Integer");
                return;
            }

            if (template.item_type == ItemType.GOLD) {
                this.throwbackInfo(pc, "use /addgold to add gold.");
                return;
            }

        } catch (Exception e) {
            throwbackError(pc,
                    "An unknown exception occurred when trying to use createitem command for type "
                            + words[0]);
            return; // NaN
        }


        String prefix = "";
        String suffix = "";

        if (!(words[1].equals("0"))) {
            prefix = words[1];
            if (!(prefix.substring(0, 4).equals("PRE-")))
                prefix = EffectsBase.getItemEffectsByName(prefix.toLowerCase());
            if (!(prefix.substring(0, 4).equals("PRE-"))) {
                throwbackError(pc, "Invalid Prefix. Prefix must consist of PRE-001 to PRE-334 or 0 for no Prefix.");
                return;
            }

            boolean validInt = false;
            try {
                int num = Integer.parseInt(prefix.substring(4, 7));
                if (num > 0 && num < 335)
                    validInt = true;
            } catch (Exception e) {
                throwbackError(pc, "error parsing number " + prefix);
            }
            if (!validInt) {
                throwbackError(pc, "Invalid Prefix. Prefix must consist of PRE-001 to PRE-334 or 0 for no Prefix.");
                return;
            }
        }

        if (!(words[2].equals("0"))) {
            suffix = words[2];

            if (!(suffix.substring(0, 4).equals("SUF-")))
                suffix = EffectsBase.getItemEffectsByName(suffix.toLowerCase());
            if (!(suffix.substring(0, 4).equals("SUF-"))) {
                throwbackError(pc, "Invalid Suffix. Suffix must consist of SUF-001 to SUF-328 or 0 for no Suffix.");
                return;
            }

            boolean validInt = false;
            try {
                int num = Integer.parseInt(suffix.substring(4, 7));
                if (num > 0 && num < 329)
                    validInt = true;
            } catch (Exception e) {
                throwbackError(pc, "error parsing number " + suffix);
            }
            if (!validInt) {
                throwbackError(pc, "Invalid Suffix. Suffix must consist of SUF-001 to SUF-328 or 0 for no Suffix.");
                return;
            }
        }
        template = ItemTemplate.templates.get(templateID);

        if (template == null) {
            throwbackError(pc, "Unable to find template " + templateID);
            return;
        }

        if ((numItems > 1)
                && (template.item_type.equals(ItemType.RESOURCE) == false)
                && (template.item_type.equals(ItemType.OFFERING)) == false)
            numItems = 1;

        CharacterItemManager cim = pc.charItemManager;

        if (cim == null) {
            throwbackError(pc, "Unable to find the character item manager for player " + pc.getFirstName() + '.');
            return;
        }

        String result = "";

        for (int i = 0; i < quantity; i++) {

            int weight = template.item_wt;

            if (!cim.hasRoomInventory(weight)) {
                throwbackError(pc, "Not enough room in inventory for any more of this item. " + i + " produced.");

                if (i > 0)
                    cim.updateInventory();

                return;
            }

            Item item = new Item(templateID);
            item.ownerID = pc.getObjectUUID();
            item.ownerType = OwnerType.PlayerCharacter;

            if (numItems > 1)
                item.setNumOfItems(numItems);

            try {
                item = DbManager.ItemQueries.PERSIST(item);
            } catch (Exception e) {
                throwbackError(pc, "DB error 1: Unable to create item. " + e.getMessage());
                return;
            }

            //create prefix
            if (!prefix.isEmpty())
                item.addPermanentEnchantmentForDev(prefix, 0);

            //create suffix
            if (!suffix.isEmpty())
                item.addPermanentEnchantmentForDev(suffix, 0);

            //add item to inventory
            cim.addItemToInventory(item);
            result += " " + item.getObjectUUID();
        }
        this.setResult(result);
        cim.updateInventory();
    }

    @Override
    protected String _getHelpString() {
        return "Creates an item of type templateID with a prefix and suffix";
    }

    @Override
    protected String _getUsageString() {
        return "'./makeitem templateID PrefixID SuffixID [quantity] [numResources]'";
    }

}
