/*
 * inventorywindow.cpp - Author: Andrew Craig
 *
 * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
 *
 *
 * 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 (version 2 of the License)
 * 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.
 *
 */

#include <psconfig.h>
#include <csutil/util.h>
#include <csutil/xmltiny.h>
#include <iutil/evdefs.h>
#include <imesh/spritecal3d.h>

#include "net/message.h"
#include "net/msghandler.h"
#include "net/cmdhandler.h"
#include "util/strutil.h"
#include "util/psconst.h"
#include "globals.h"

#include "pscelclient.h"



#include "paws/pawstextbox.h"
#include "paws/pawsprefmanager.h"
#include "inventorywindow.h"
#include "paws/pawsmanager.h"
#include "paws/pawsbutton.h"
#include <propclass/mesh.h>
#include "paws/pawstexturemanager.h"
#include "paws/pawslistbox.h"
#include "paws/pawsnumberpromptwindow.h"

#include "gui/pawsmoney.h"
#include "gui/pawsexchangewindow.h"
#include "gui/pawsslot.h"
#include "gui/pawscontrolwindow.h"
#include "gui/pawsinventorydollview.h"


#define VIEW_BUTTON 1005
#define QUIT_BUTTON 1006

#define DEC_STACK_BUTTON 1010
#define INC_STACK_BUTTON 1011


/**********************************************************************
*
*                        class pawsInventoryWindow
*
***********************************************************************/

pawsInventoryWindow::pawsInventoryWindow()
{
    msgHandler = NULL;

    loader = CS_QUERY_REGISTRY( PawsManager::GetSingleton().GetObjectRegistry() , iLoader );
    
    bulkSlots.SetLength( 32 );
    equipmentSlots.SetLength( PSCHARACTER_SLOT_COUNT );
    for ( size_t n = 0; n < equipmentSlots.Length(); n++ )
        equipmentSlots[n] = NULL;
}

void pawsInventoryWindow::Show()
{
    pawsControlledWindow::Show();

    // Ask the server to send us the inventory
    psGUIInventoryMessage outGoingMessage;
    msgHandler->SendMessage( outGoingMessage.msg );
}

bool pawsInventoryWindow::SetupSlot( const char* slotName )
{
    pawsSlot* slot = dynamic_cast <pawsSlot*> (FindWidget(slotName));
    if (slot == NULL)
    {
        Error2("Could not locate pawsSlot %s.",slotName);
        return false;
    }
    uintptr_t slotID = psengine->slotName.GetID(slotName);
    if ( slotID == csInvalidStringID )
    {
        Error2("Could not located the %s slot", slotName); 
        return false;
    }
    slot->SetContainer(  CONTAINER_INVENTORY_EQUIPMENT );
    slot->SetSlotID( slotID );
    slot->DrawStackCount(true);
   
    equipmentSlots[slotID] = slot;
    return true;
}

bool pawsInventoryWindow::PostSetup()
{    
    msgHandler = psengine->GetMsgHandler();
    if ( !msgHandler )
        return false;
    
    // Setup the Doll
    if ( !SetupDoll() )
        return false;

    trias  = dynamic_cast <pawsTextBox*> (FindWidget("TotalTrias")); 
    if ( !trias )
        return false;
    weight = dynamic_cast <pawsTextBox*> (FindWidget("TotalWeight")); 
    if ( !weight )
        return false;
        
    money = dynamic_cast <pawsMoney*> (FindWidget("Money")); 
    if ( !money )
        return false;
    
    money->SetContainer( CONTAINER_INVENTORY_MONEY );            
     
    // If you add something here, DO NOT FORGET TO CHANGE 'INVENTORY_EQUIP_COUNT'!!!
    if ( !SetupSlot("lefthand") )    return false;
    if ( !SetupSlot("righthand") )   return false;
    if ( !SetupSlot("leftfinger") )  return false;
    if ( !SetupSlot("rightfinger") ) return false;
    if ( !SetupSlot("head") )        return false;
    if ( !SetupSlot("neck") )        return false;
    if ( !SetupSlot("back") )        return false;
    if ( !SetupSlot("arms") )        return false;    
    if ( !SetupSlot("gloves") )      return false;        
    if ( !SetupSlot("boots") )       return false;            
    if ( !SetupSlot("legs") )        return false;            
    if ( !SetupSlot("belt") )        return false;                
    if ( !SetupSlot("bracers") )     return false;            
    if ( !SetupSlot("torso") )       return false;                
    if ( !SetupSlot("mind") )        return false;
        
    pawsListBox * bulkList = dynamic_cast <pawsListBox*> (FindWidget("BulkList"));        
    for (int i = 0; i < INVENTORY_BULK_COUNT/2; i++)
    {
        pawsListBoxRow * listRow = bulkList->NewRow(i);
        for (int j = 0; j < 2; j++)
        {
            pawsSlot * slot;
            slot = dynamic_cast <pawsSlot*> (listRow->GetColumn(j));
            slot->SetContainer( CONTAINER_INVENTORY_BULK );
            //csString name;
            slot->SetSlotID( i*2+j );     
            csString name;
            name.Format("sigbulk_%d", i*2+j);
            PawsManager::GetSingleton().Subscribe( name, slot );                     
            bulkSlots[i*2+j] = slot;            
        }        
    }

    // Ask the server to send us the inventory
    psGUIInventoryMessage outGoingMessage;
    msgHandler->SendMessage( outGoingMessage.msg );
    
    return true;
}

bool pawsInventoryWindow::SetupDoll()
{
    pawsObjectView* widget = dynamic_cast<pawsObjectView*>(FindWidget("InventoryDoll"));
    GEMClientActor* actor = psengine->GetCelClient()->GetMainPlayer();
    if (!widget || !actor)
        return false;

    csRef<iPcMesh> mesh = actor->pcmesh;
    if (!mesh) return false;

    // Set the doll view
    widget->View( mesh->GetMesh() );
    
    // Register this doll for updates
    widget->SetID( actor->GetEntity()->GetID() );

    csRef<iSpriteCal3DState> spstate = SCF_QUERY_INTERFACE(widget->GetObject()->GetMeshObject(),iSpriteCal3DState);
    if (spstate)
    {
        // Setup cal3d to select random 0 velocity anims
        spstate->SetVelocity(0.0,&psengine->GetRandomGen());
    }

    // Build doll appearance and equipment
    bool a = psengine->BuildAppearance( widget->GetObject(), actor->traits );
    bool e = psengine->BuildEquipment( widget->GetObject(), actor->equipment, actor->traitList );
    
    return (a && e);
}

bool pawsInventoryWindow::OnMouseDown( int button, int keyModifier, int x, int y )
{
    // Check to see if we are dropping an item
    if ( psengine->GetSlotManager() && psengine->GetSlotManager()->IsDragging() )
    {
        psengine->GetSlotManager()->CancelDrag();
        return true;
    }
    return pawsControlledWindow::OnMouseDown(  button, keyModifier, x, y );
}


bool pawsInventoryWindow::OnButtonPressed( int mouseButton, int keyModifer, pawsWidget* widget )
{    
    // Check to see if this was the view button.
    if ( widget->GetID() == VIEW_BUTTON )
    {       
        if ( psengine->GetSlotManager()->IsDragging() )
        {
            psViewItemDescription out( psengine->GetSlotManager()->HoldingContainerID(), 
                                       psengine->GetSlotManager()->HoldingSlotID(),
                                       psengine->GetSlotManager()->HoldingParentID() );   
            msgHandler->SendMessage( out.msg );

            psengine->GetSlotManager()->CancelDrag();
        }
    
        return true;
    }

    return true;
}

void pawsInventoryWindow::Close()
{
    Hide();
}

pawsSlot* pawsInventoryWindow::GetFreeSlot()
{
    for ( size_t n = 0; n < bulkSlots.Length(); n++ )
    {    
        if ( bulkSlots[n] && bulkSlots[n]->IsEmpty() )
        {
            return bulkSlots[n];
        }       
    }

    return NULL;
}


void pawsInventoryWindow::Dequip( const char* itemName )
{    
    if ( itemName != NULL )
    {                    
        pawsSlot* fromSlot = NULL;
        // See if we can find the item in the equipment slots.
        for ( size_t z = 0; z < equipmentSlots.Length(); z++ )
        {                   
            if ( equipmentSlots[z] && !equipmentSlots[z]->IsEmpty() )
            {                               
                csString tip(equipmentSlots[z]->GetToolTip()); 
                if ( tip.CompareNoCase(itemName) )                
                {
                    fromSlot = equipmentSlots[z];
                    break;
                }                
            }
        }
         
        if ( fromSlot == NULL ) // if item was not found, look in slotnames 
            fromSlot = dynamic_cast <pawsSlot*> (FindWidget(itemName));    
    
        if ( fromSlot )
        {
            int container   = fromSlot->ContainerID();
            int slot        = fromSlot->ID();
            int parent      = fromSlot->ParentContainerID();
            int stackCount  = fromSlot->StackCount();
                   
            pawsSlot* freeSlot = GetFreeSlot();
            if ( freeSlot )
            {
            
                // Move from the equiped slot to an empty slot
                psSlotMovementMsg msg( container, slot, parent,
                                       freeSlot->ContainerID() ,
                                       freeSlot->ID(),
                                       freeSlot->ParentContainerID(),
                                       stackCount );
                               
                msgHandler->SendMessage( msg.msg );                                               
                fromSlot->Clear();
            }
            
        }    
    }        
}

void pawsInventoryWindow::Equip( const char* itemName, int stackCount )
{    
    if ( itemName != NULL )
    {        
        pawsSlot* fromSlot = NULL;        
        for ( size_t z = 0; z < bulkSlots.Length(); z++ )
        {
            if ( !bulkSlots[z]->IsEmpty() )
            {
                csString tip(bulkSlots[z]->GetToolTip()); 
                if ( tip.CompareNoCase(itemName) )
                {
                    fromSlot = bulkSlots[z];
                    break;
                }                
            }
        }
            
        if ( fromSlot )
        {
            int container   = fromSlot->ContainerID();
            int slot        = fromSlot->ID();
            int parent      = fromSlot->ParentContainerID();
        
            //psItem* item = charData->GetItemInSlot( slot );    
            csRef<MsgHandler> msgHandler = psengine->GetMsgHandler();
            psSlotMovementMsg msg( container, slot, parent,
                               CONTAINER_INVENTORY_EQUIPMENT, -1, 0,                
                               stackCount );
            msgHandler->SendMessage( msg.msg );                                               
        }
    }         
}

