/*
* guihandler.cpp
*
* Copyright (C) 2005 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
*
* Credits : 
*           Keith Fulton <keith@planeshift.it>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Creation Date: 4/20/2005
* Description : Handles network messages to and from the GUI, using PAWS pub/sub system.
*
*/
#include <psconfig.h>

#include <csutil/objreg.h>

#include "globals.h"
#include "net/messages.h"
#include "net/msghandler.h"

#include "guihandler.h"

GUIHandler::GUIHandler() 
           : psCmdBase(NULL,NULL,psengine->GetObjectRegistry())
{
    // Subscribe our message types that we are interested in. 
    psengine->GetMsgHandler()->Subscribe( this, MSGTYPE_GUIINVENTORY );
}


void GUIHandler::HandleMessage(MsgEntry* me)
{
    switch( me->GetType() )
    {
        case MSGTYPE_GUIINVENTORY:
            HandleInventory(me);
            break;
    }                    
}

void GUIHandler::HandleInventory(MsgEntry* me)
{
    psGUIInventoryMessage incomming(me);
    
    PawsManager::GetSingleton().Publish("sigClearSlot", 0 );    
    float itemWeight = 0.0;
    float totalSize = 0.0;
    
    for ( size_t z = 0; z < incomming.totalItems; z++ )
    {
        if ( incomming.items[z].iconImage.Length() != 0 )
        {
            csString sigData;        
            
            if (  incomming.items[z].container == CONTAINER_INVENTORY_BULK )                    
                sigData.Format("sigbulk_%d",incomming.items[z].slot ) ;
            else if (  incomming.items[z].container == CONTAINER_INVENTORY_EQUIPMENT )                        
                sigData.Format("sigslot_%s",psengine->slotName.GetName(incomming.items[z].slot ) ) ;
                
            csString data;
            data.Format( "%s %d %d %s", incomming.items[z].iconImage.GetData(),
                                        incomming.items[z].stackcount,
                                        incomming.items[z].purifyStatus,
                                        incomming.items[z].name.GetData()
                                        );
                                                                    
            PawsManager::GetSingleton().Publish(sigData, data );                                                    
                    
            itemWeight += incomming.items[z].weight;
            totalSize  +=incomming.items[z].size;
        } 
    }        
    PawsManager::GetSingleton().Publish( "fcurrcap", totalSize );         
    PawsManager::GetSingleton().Publish( "sigInvMoney", incomming.money.ToString() );         
    PawsManager::GetSingleton().Publish( "sigInvMoneyTotal", incomming.money.GetTotal() );
    csString weight = csString().Format("%.2f/%.2f",itemWeight, incomming.maxWeight);
    PawsManager::GetSingleton().Publish( "sigInvWeightTotal", weight );                
}

