package electrosphere.net.client.protocol;

import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.state.equip.ClientEquipState;
import electrosphere.entity.state.inventory.ClientInventoryState;
import electrosphere.entity.state.inventory.InventoryUtils;
import electrosphere.game.data.creature.type.equip.EquipPoint;
import electrosphere.logger.LoggerInterface;
import electrosphere.net.parser.net.message.InventoryMessage;
import electrosphere.net.template.ClientProtocolTemplate;

/**
 * The client protocol for handling inventory messages
 */
public class InventoryProtocol implements ClientProtocolTemplate<InventoryMessage> {

    @Override
    public InventoryMessage handleAsyncMessage(InventoryMessage message) {
        return message;
    }

    @Override
    public void handleSyncMessage(InventoryMessage message) {
        switch(message.getMessageSubtype()){
            case ADDITEMTOINVENTORY:
                LoggerInterface.loggerNetworking.DEBUG("[CLIENT] ADD ITEM TO INVENTORY " + message.getentityId());
                if(Globals.playerEntity != null){
                    ClientInventoryState inventoryState;
                    if((inventoryState = InventoryUtils.clientGetInventoryState(Globals.playerEntity))!=null){
                        inventoryState.addNetworkMessage(message);
                    }
                }
                break;
            case SERVERCOMMANDEQUIPITEM: {
                    LoggerInterface.loggerNetworking.DEBUG("[CLIENT] EQUIP ITEM " + message.getentityId());
                    //translate equipper id
                    Entity equipper = Globals.clientSceneWrapper.getEntityFromServerId(message.getequipperId());
                    //spawn in world id
                    Entity inWorldEntity = Globals.clientSceneWrapper.getEntityFromServerId(message.getentityId());
                    if(inWorldEntity != null){
                        //translate id
                        Globals.clientSceneWrapper.mapIdToId(inWorldEntity.getId(), message.getentityId());
                        //grab equip state
                        ClientEquipState equipState = ClientEquipState.getEquipState(equipper);
                        //create entity from template in message
                        //get equippoint
                        String equipPointName = message.getequipPointId();
                        EquipPoint equipPoint = equipState.getEquipPoint(equipPointName);
                        //attach
                        equipState.attemptEquip(inWorldEntity, equipPoint);
                    }
                }
                break;
            case REMOVEITEMFROMINVENTORY:
                LoggerInterface.loggerNetworking.DEBUG("[CLIENT] REMOVE ITEM FROM INVENTORY " + message.getentityId());
                if(Globals.playerEntity != null){
                    ClientInventoryState inventoryState;
                    if((inventoryState = InventoryUtils.clientGetInventoryState(Globals.playerEntity))!=null){
                        inventoryState.addNetworkMessage(message);
                    }
                }
                break;
            case SERVERCOMMANDMOVEITEMCONTAINER:
                LoggerInterface.loggerNetworking.DEBUG("[CLIENT] MOVE ITEM INVENTORY " + message.getentityId());
                if(Globals.playerEntity != null){
                    ClientInventoryState inventoryState;
                    if((inventoryState = InventoryUtils.clientGetInventoryState(Globals.playerEntity))!=null){
                        inventoryState.addNetworkMessage(message);
                    }
                }
                break;
            case SERVERCOMMANDUNEQUIPITEM: {
                LoggerInterface.loggerNetworking.DEBUG("[CLIENT] UNEQUIP ITEM " + message.getentityId());
                if(Globals.playerEntity != null){
                    ClientInventoryState inventoryState;
                    if((inventoryState = InventoryUtils.clientGetInventoryState(Globals.playerEntity))!=null){
                        inventoryState.addNetworkMessage(message);
                    }
                }
            }
            break;
            case CLIENTREQUESTPERFORMITEMACTION:
            case CLIENTREQUESTUNEQUIPITEM:
            case CLIENTREQUESTEQUIPITEM:
                //silently ignore
                break;
        }
    }
}
