package org.jill.game.entities.obj;

import org.jill.game.entities.obj.abs.AbstractParameterObjectEntity;
import java.awt.image.BufferedImage;
import org.jill.openjill.core.api.entities.ObjectEntity;
import org.jill.openjill.core.api.entities.ObjectParam;
import org.jill.openjill.core.api.keyboard.KeyboardLayout;
import org.jill.openjill.core.api.message.EnumMessageType;
import org.jill.openjill.core.api.message.object.ObjectListMessage;
import org.jill.openjill.core.api.message.statusbar.inventory.
        EnumInventoryObject;
import org.jill.openjill.core.api.message.statusbar.inventory.
        InventoryItemMessage;

/**
 * Bonus item manager.
 *
 * @author Emeric MARTINEAU
 */
public final class BonusManager extends AbstractParameterObjectEntity {
    /**
     * Array to know if message is already send.
     */
    private static boolean[] msgAlreadySendArray = null;
    
    /**
     * Picture array.
     */
    private BufferedImage images;

    /**
     * Inventory object to add.
     */
    private InventoryItemMessage inventory;

    /**
     * To remove this object from object list.
     */
    private ObjectListMessage killme;
    
    /**
     * Message to display.
     */
    private String msg;
    
    /**
     * Default constructor.
     *
     * @param objectParam object paramter
     */
    @Override
    public void init(final ObjectParam objectParam) {
        super.init(objectParam);

        final EnumInventoryObject[] enumList
                = EnumInventoryObject.getEnumList();
       
        final String nameOfInventoryItem = enumList[counter].toString();

        final String key = getConfString(nameOfInventoryItem);
        final String[] keySplit = key.split(",");
        
        if (keySplit.length > 2) {
            this.msg = keySplit[2];
        }

        // Init list of picture
        final int tileIndex = Integer.valueOf(keySplit[1]);
        final int tileSetIndex = Integer.valueOf(keySplit[0]);

        this.images = this.pictureCache.getImage(tileSetIndex, tileIndex);

        final String dontRemove = getConfString("dontRemove");

        if (dontRemove.contains(nameOfInventoryItem)) {
            this.inventory = new InventoryItemMessage(enumList[this.counter],
                true, true);
        } else {
            this.inventory = new InventoryItemMessage(enumList[this.counter],
                true);
            // Remove me from list of object (= kill me)
            this.killme = new ObjectListMessage(this, false);
        }
        
        // If array of message is not create, create it
        if (msgAlreadySendArray == null) {
            msgAlreadySendArray = new boolean[enumList.length];
        }
    }

    @Override
    public BufferedImage msgDraw() {
        return this.images;
    }

    @Override
    public void msgTouch(final ObjectEntity obj,
            final KeyboardLayout keyboardLayout) {
        if (obj.isPlayer()) {
            sendItem();

            this.messageDispatcher.sendMessage(
                EnumMessageType.INVENTORY_ITEM,
                this.inventory);
        }
    }

    /**
     * Send item.
     */
    private void sendItem() {
        // If don't remove, don't send it.
        if (this.killme == null) {
            // Special case.
            // Remove all unremovable
            // TODO how manage it ?

        } else {
            if (!msgAlreadySendArray[getCounter()]) {
                // Disable message
                msgAlreadySendArray[getCounter()] = true;
                // Send message
                sendMessage(this.msg);
            }
            
            this.messageDispatcher.sendMessage(EnumMessageType.OBJECT,
                    this.killme);
        }
    }
}

@@ -872,35 +872,30 @@
 olean%5B%5D 
-msgAlreadySendArray
+displayMessage
  = null;
@@ -1464,24 +1464,32 @@
 ectParam);%0D%0A
+        
 %0D%0A        fi
@@ -1574,24 +1574,352 @@
 numList();%0D%0A
+        %0D%0A        // If array of message is not create, create it%0D%0A        if (displayMessage == null) %7B%0D%0A            displayMessage = new boolean%5BenumList.length%5D;%0D%0A            %0D%0A            for (int index = 0; index %3C displayMessage.length; index++) %7B%0D%0A                displayMessage%5Bindex%5D = true;%0D%0A            %7D%0D%0A        %7D%0D%0A
        %0D%0A   
@@ -2194,24 +2194,146 @@
 ;%0D%0A        %7D
+ else %7B%0D%0A            // Disable message when bonus havn't message%0D%0A            displayMessage%5Bcounter%5D = false;%0D%0A        %7D
 %0D%0A%0D%0A        
@@ -3051,195 +3051,8 @@
  %7D%0D%0A
-        %0D%0A        // If array of message is not create, create it%0D%0A        if (msgAlreadySendArray == null) %7B%0D%0A            msgAlreadySendArray = new boolean%5BenumList.length%5D;%0D%0A        %7D%0D%0A
     
@@ -3768,28 +3768,22 @@
 if (
-!msgAlreadySendArray
+displayMessage
 %5Bget
@@ -3853,27 +3853,22 @@
     
-msgAlreadySendArray
+displayMessage
 %5Bget
@@ -3876,27 +3876,28 @@
 ounter()%5D = 
-tru
+fals
 e;%0D%0A        

Rework message support for bonus object (type 28)