/*
 * pawscontainerdescriptionwidow.cpp - Author: Thomas Towey
 *
 * 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>

// CS INCLUDES
#include <csgeom/vector3.h>
#include <iutil/objreg.h>

// COMMON INCLUDES
#include <propclass/linmove.h>

// CLIENT INCLUDES
#include "pscelclient.h"

// PAWS INCLUDES
#include "pawscontainerdescwindow.h"
#include "paws/pawstextbox.h"
#include "paws/pawslistbox.h"
#include "inventorywindow.h"
#include "paws/pawsmanager.h"
#include "net/messages.h"
#include "net/msghandler.h"

#include "util/log.h"
#include "gui/pawsslot.h"

#include "globals.h"


// BUTTONS AND SLOTS
#define VIEW_BUTTON 11
#define INVENTORY_BUTTON 12

char* pawsContainerDescWindow::container_slot_names[] =
{"Bulk0","Bulk1","Bulk2","Bulk3",
 "Bulk4","Bulk5","Bulk6","Bulk7",
 "Bulk8","Bulk9","Bulk10","Bulk11",
 "Bulk12","Bulk13","Bulk14","Bulk15", 0};

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

pawsContainerDescWindow::pawsContainerDescWindow()
{
    containerSlots.SetLength(PSITEM_MAX_CONTAINER_SLOTS);
}

pawsContainerDescWindow::~pawsContainerDescWindow()
{
}

bool pawsContainerDescWindow::PostSetup()
{
    msgHandler = psengine->GetMsgHandler();

    if ( !msgHandler ) return false;
    if ( !msgHandler->Subscribe(this, MSGTYPE_VIEW_CONTAINER ) )
        return false;        
    if ( !msgHandler->Subscribe(this, MSGTYPE_UPDATE_ITEM ) )
        return false;        

    // Store some of our children for easy access later on.
    name = (pawsTextBox*)FindWidget("ItemName");
    if (!name)
        return false;

    description = dynamic_cast<pawsMultiLineTextBox*> (FindWidget("ItemDescription"));
    if (!description)
        return false;
    
    pic = (pawsWidget*)FindWidget("ItemImage");
    if (!pic)
        return false;
    
    // Create bulk slots.
    contents = dynamic_cast <pawsListBox*> (FindWidget("BulkList"));
    if (!contents)
        return false;

    const int cols = 6;
    for (int i = 0; i < int(ceil((float)PSITEM_MAX_CONTAINER_SLOTS/cols)); i++)
    {
        pawsListBoxRow* listRow = contents->NewRow(i);
        for (int j = 0; j < cols; j++)
        {
            pawsSlot* slot = dynamic_cast <pawsSlot*> (listRow->GetColumn(j));
            CS_ASSERT( slot );

            if((i * cols) + j >= PSITEM_MAX_CONTAINER_SLOTS)
            {
                slot->Hide();
                continue;;
            }

            slot->SetName(container_slot_names[i*cols+j]);
            slot->SetSlotID( i*cols+j );    
            containerSlots[i*cols+j] = slot;
        }
    }

    return true;
}

void pawsContainerDescWindow::HandleUpdateItem( MsgEntry* me )
{
    psViewItemUpdate mesg( me );

    if( mesg.containerID == containerID)
    {
        int slot = mesg.slotID;            
        if(slot >= 0 && slot < PSITEM_MAX_CONTAINER_SLOTS)
        {
            pawsSlot* itemSlot = containerSlots[slot];    
            itemSlot->SetSlotID( slot );

            if( mesg.clearSlot )
            {
                itemSlot->Clear();        
                itemSlot->SetToolTip( "Empty" );
            }
            else
            {
                itemSlot->PlaceItem( mesg.icon, mesg.stackCount );                  
                itemSlot->SetToolTip( mesg.name );
            }
        }
    }
}

void pawsContainerDescWindow::HandleViewItem( MsgEntry* me )
{
    Show();
    psViewItemDescription mesg( me );
    
    description->SetText( mesg.itemDescription );
    name->SetText( mesg.itemName );       
    pic->SetBackground( mesg.itemIcon );
    containerID = mesg.containerID;

    if ( mesg.hasContents )
    {            
        contents->Show();
        for (size_t n = 0; n < containerSlots.Length(); n++ )
        {
            pawsSlot* itemSlot = containerSlots[n];
            itemSlot->SetContainer( mesg.containerID );    
            itemSlot->ParentContainerID( mesg.parentContainerID );
                
            itemSlot->Clear();
            itemSlot->SetToolTip( "Empty" );
        }

        for (size_t i = 0; i < mesg.contents.Length(); i++ )
        {
            int slot = mesg.contents[i].slotID;            
            if(slot >= 0 && slot < PSITEM_MAX_CONTAINER_SLOTS)
            {
                pawsSlot* itemSlot = containerSlots[slot];    
                itemSlot->SetSlotID( slot );
                
                itemSlot->PlaceItem( mesg.contents[i].icon,                    
                                    mesg.contents[i].stackCount );                  
                itemSlot->SetToolTip( mesg.contents[i].name );
            }
        }            
    }
    else
    {
        contents->Hide();
    }
}

void pawsContainerDescWindow::HandleMessage( MsgEntry* me )
{
    switch ( me->GetType() )
    {
        case MSGTYPE_VIEW_CONTAINER:
        {
            HandleViewItem( me );
            break;
        }
        case MSGTYPE_UPDATE_ITEM:
        {
            HandleUpdateItem( me );
            break;
        }
    }
    
}

void pawsContainerDescWindow::ClearContents()
{
    for (size_t n = 0; n < containerSlots.Length(); n++ )
    {
        containerSlots[n]->Clear();        
        containerSlots[n]->SetToolTip( "Empty" );
    }
}

bool pawsContainerDescWindow::OnButtonPressed( int mouseButton, int keyModifier, pawsWidget* widget )
{   
    csString widgetName(widget->GetName());
    
    if ( widgetName == "SmallInvButton" )
    {
        pawsWidget* widget = PawsManager::GetSingleton().FindWidget("SmallInventoryWindow");
        if ( widget )
            widget->Show();
        return true;        
    }
    
    // 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;
    }
    else if ( widget->GetID() == INVENTORY_BUTTON )
    {     
        if ( psengine->GetSlotManager()->IsDragging() )
        {            
            pawsInventoryWindow* inv = (pawsInventoryWindow*)PawsManager::GetSingleton().FindWidget("InventoryWindow");
            pawsSlot* slot = inv->GetFreeSlot();
        
            if(!slot)
            {
                PawsManager::GetSingleton().CreateWarningBox("Your inventory is full!");
                return true;
            }

            psengine->GetSlotManager()->Handle(slot);
        }

        return true;
    }

    return true;
}

