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


package engine.devcmd.cmds;

import engine.devcmd.AbstractDevCmd;
import engine.gameManager.ChatManager;
import engine.gameManager.DbManager;
import engine.mbEnums;
import engine.objects.AbstractGameObject;
import engine.objects.Item;
import engine.objects.ItemTemplate;
import engine.objects.PlayerCharacter;
import org.pmw.tinylog.Logger;

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

    public CreateItemCmd() {
        super("createitem");
    }

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

        if (words.length < 2) {
            this.sendUsage(playerCharacter);
            return;
        }

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

        if (template == null) {
            ChatManager.chatSystemInfo(playerCharacter, "No such template found.");
            return;
        }
        int size = 1;

        if (words.length == 2)
            size = Integer.parseInt(words[1]);

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

            if (!playerCharacter.charItemManager.hasRoomInventory(template.item_wt)) {
                ChatManager.chatSystemInfo(playerCharacter, "You are encumbered!.");
                break;
            }

            Item item = new Item(templateID);
            item.ownerID = playerCharacter.getObjectUUID();
            item.ownerType = mbEnums.OwnerType.PlayerCharacter;
            item.containerType = mbEnums.ItemContainerType.INVENTORY;

            try {
                item = DbManager.ItemQueries.PERSIST(item);
                playerCharacter.charItemManager.addItemToInventory(item);
            } catch (Exception e) {
                Logger.error(e);
            }
        }
        playerCharacter.charItemManager.updateInventory();
    }

    @Override
    protected String _getHelpString() {
        return "Fill your inventory with items";
    }

    @Override
    protected String _getUsageString() {
        return "' /createitem <templateID> <quantity>'";
    }

}
