/*
 * psslotmgr.cpp
 *
 * Copyright (C) 2005 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; 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.
 *
 *
 */


#include <psconfig.h>


#include "psslotmgr.h"
#include "globals.h"

#include "gui/pawsslot.h"

psSlotManager::psSlotManager()
{
    isDragging = false;
}

psSlotManager::~psSlotManager()
{
}

void psSlotManager::CancelDrag()
{
    isDragging = false;
    pawsSlot* dragging = (pawsSlot*)PawsManager::GetSingleton().GetDragDropWidget();

    if ( !dragging )
        return;
        
    draggingSlot.slot->SetPurifyStatus(dragging->GetPurifyStatus());
    int oldStack =  draggingSlot.slot->StackCount();
    oldStack += draggingSlot.stackCount;

    csString res;
    if(dragging->Image() && dragging->Image()->GetDescription())
        res = dragging->Image()->GetDescription()->resourceName;
    else
        res = "";

    draggingSlot.slot->PlaceItem(res,oldStack);    
    PawsManager::GetSingleton().SetDragDropWidget( NULL );
}

void psSlotManager::OnNumberEntered(const char *name,int param,int count)
{
    if ( count == -1 )
        return;

    pawsSlot* parent = NULL;
    size_t i = (size_t)param;
    if (i < slotsInUse.Length())
    {
        // Get the slot ptr
        parent = slotsInUse[i];
        slotsInUse[i] = NULL;
        
        // Clean up the trailing NULLs  (can't just delete the index, as that would change other indicies)
        for (i=slotsInUse.Length()-1; i!=(size_t)-1; i--)
        {
            if (slotsInUse[i] == NULL)
                slotsInUse.DeleteIndex(i);
            else break;
        }
    }

    if (!parent)
        return;

    int purifyStatus = parent->GetPurifyStatus();
    int newStack = parent->StackCount() - count;
    parent->StackCount( newStack );
            
    pawsSlot* widget = new pawsSlot();
    widget->SetRelativeFrame( 0,0, parent->DefaultFrame().Width(), parent->DefaultFrame().Height() );
    
    if ( parent->Image() && parent->Image()->GetDescription() )    
        widget->PlaceItem( parent->Image()->GetDescription()->resourceName, count );
    else        
        widget->PlaceItem( NULL, count );
        
    widget->SetPurifyStatus( purifyStatus );
    widget->SetBackgroundAlpha(0);
    widget->SetParent( NULL );
           
    SetDragDetails( parent, count );
    isDragging = true;
    PawsManager::GetSingleton().SetDragDropWidget( widget );
}

void psSlotManager::SetDragDetails( pawsSlot* slot, int count ) 
{ 
    draggingSlot.containerID    = slot->ContainerID();
    draggingSlot.slotID         = slot->ID();
    draggingSlot.stackCount     = count;
    draggingSlot.slot           = slot;
    draggingSlot.parentID       = slot->ParentContainerID();
}

void psSlotManager::DropItem()
{
    psPoint p = PawsManager::GetSingleton().GetMouse()->GetPosition();
    psSlotMovementMsg msg( draggingSlot.containerID, draggingSlot.slotID, draggingSlot.parentID,
                           CONTAINER_WORLD, p.x < PawsManager::GetSingleton().GetGraphics2D()->GetWidth() / 2, 0,            
                           draggingSlot.stackCount );
    PawsManager::GetSingleton().SetDragDropWidget( NULL );
    msg.SendMessage();                               
    isDragging = false;                           
}
 
void psSlotManager::Handle( pawsSlot* slot )
{     
    if ( !isDragging )
    {
        int stackCount = slot->StackCount();
        if ( stackCount > 0 )
        {          
            int tmpID = (int)slotsInUse.Push(slot);

            if ( stackCount > 1 )
            {
                csString max;
                max.Format("Max %d", stackCount );
                
                pawsNumberPromptWindow::Create(max,
                                               -1, 1, stackCount, this, "StackCount", tmpID);
            }
            else // call directly
                OnNumberEntered("StackCount",tmpID,1);
        }
    }
    else
    {
        psSlotMovementMsg msg( draggingSlot.containerID, draggingSlot.slotID, draggingSlot.parentID,
                               slot->ContainerID(), slot->ID(), slot->ParentContainerID(),               
                               draggingSlot.stackCount );

        PawsManager::GetSingleton().SetDragDropWidget( NULL );
        msg.SendMessage();                               
        isDragging = false;
    }

    
}

