/*
 * Author: Andrew Craig
 *
 * Copyright (C) 2004 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 <iutil/objreg.h>

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

#include "../globals.h"
#include "paws/pawslistbox.h"
#include "gui/pawscontrolwindow.h"
#include "gui/chatwindow.h"

#include "pawsbuddy.h"

#define TELL    1000
#define REMOVE  2000
#define ADD     3000

pawsBuddyWindow::pawsBuddyWindow()
{
    buddyList = NULL;
}

bool pawsBuddyWindow::PostSetup()
{
    psengine->GetMsgHandler()->Subscribe( this, MSGTYPE_BUDDY_LIST );
    psengine->GetMsgHandler()->Subscribe( this, MSGTYPE_BUDDY_STATUS );
                
    buddyList = (pawsListBox*)FindWidget("BuddyList");
    
    chatWindow = (pawsChatWindow*)PawsManager::GetSingleton().FindWidget("ChatWindow");
    if (!chatWindow)
    {
        Error1("BuddyWindow failed because Chat window was not found.");
        return false;
    }    
    return true;
}

void pawsBuddyWindow::OnListAction( pawsListBox* widget, int status )
{
    if (status==LISTBOX_HIGHLIGHTED)
    {
        pawsListBoxRow* row =  widget->GetSelectedRow();
        if ( row )
        {
            currentBuddy = ((pawsTextBox*)row->GetColumn(0))->GetText();
        }
    }
}

void pawsBuddyWindow::Show()
{
    pawsControlledWindow::Show();
    psUserCmdMessage cmdmsg("/buddylist");
    psengine->GetMsgHandler()->SendMessage(cmdmsg.msg); 
}

void pawsBuddyWindow::OnResize()
{
    pawsWidget::OnResize();

    if (buddyList)
    {
        buddyList->CalculateDrawPositions();
    }
}	

void pawsBuddyWindow::HandleMessage( MsgEntry* me )
{
    buddyList->Clear();
               
    if ( me->GetType() == MSGTYPE_BUDDY_LIST )
    {
        psBuddyListMsg mesg(me);
    
        onlineBuddies.DeleteAll();
        offlineBuddies.DeleteAll();
         
        for (size_t x = 0; x < mesg.buddies.Length(); x++ )
        {
        if (mesg.buddies[x].online)
            onlineBuddies.Push(mesg.buddies[x].name);
        else
            offlineBuddies.Push(mesg.buddies[x].name);
        }
    
    }
    else if ( me->GetType() == MSGTYPE_BUDDY_STATUS )
    {
        psBuddyStatus mesg(me);
        
        // If player is online now remove from the offline list
        // else remove them from offline list and add to the online list.
        if ( mesg.onlineStatus )       
        {   
            size_t loc = offlineBuddies.Find(mesg.buddy.GetData());
            if ( loc != csArrayItemNotFound )
            {
                offlineBuddies.DeleteIndex(loc);
            }                
            onlineBuddies.Push( mesg.buddy );
        }
        else
        {
            size_t loc = onlineBuddies.Find(mesg.buddy.GetData());
            if ( loc != csArrayItemNotFound )
            {
                onlineBuddies.DeleteIndex(loc);
            }                

            offlineBuddies.Push( mesg.buddy );            
        }
    }        
    
    onlineBuddies.Sort();
    offlineBuddies.Sort();
 
    for (size_t x = 0; x < onlineBuddies.Length(); x++ )
    {
        pawsListBoxRow* row = buddyList->NewRow();
        pawsTextBox* buddyname = (pawsTextBox*)row->GetColumn(0);
        buddyname->SetText( onlineBuddies[x] );
        buddyname->SetName( onlineBuddies[x] );
        buddyname->SetColour( graphics2D->FindRGB( 0,255,0 ) );
 
        chatWindow->AddAutoCompleteName( onlineBuddies[x] );
    }
 
    for (size_t x = 0; x < offlineBuddies.Length(); x++ )
    {
        pawsListBoxRow* row = buddyList->NewRow();
        pawsTextBox* buddyname = (pawsTextBox*)row->GetColumn(0);
        buddyname->SetText( offlineBuddies[x] );
        buddyname->SetName( offlineBuddies[x] );
        buddyname->SetColour( graphics2D->FindRGB( 255,0,0 ) );
        
        chatWindow->AddAutoCompleteName( offlineBuddies[x] );
    }
    
}


bool pawsBuddyWindow::OnButtonPressed( int mouseButton, int keyModifier, pawsWidget* widget )
{
    switch ( widget->GetID() )
    {
        case TELL:
        {
            if ( currentBuddy.Length() == 0 )
                return true;

            csString title("Tell ");
            title.Append( currentBuddy );
            pawsStringPromptWindow::Create(title, csString(""),
                                           false, 220, 20, this, currentBuddy);
            return true;
        }
        
        case REMOVE:
        {          
            if ( currentBuddy.Length() == 0 )
                return true;

            pawsStringPromptWindow::Create("Remove", currentBuddy,
                                           false, 220, 20, this, "RemoveBuddy" );            
            return true;
        }
        
        case ADD:
        {
            pawsStringPromptWindow::Create("Add", csString(""),
                                           false, 220, 20, this, "AddBuddy");            
            return true;
        }
    }
    return false;
}

void pawsBuddyWindow::OnStringEntered(const char *name,int param,const char *value)
{
    if (!value || !strlen(value))
        return;

    if (!strcmp(name,"AddBuddy"))
    {
        csString command;
        command.Format("/buddy %s", value);
        psengine->GetCmdHandler()->Execute(command);
    }
    else if (!strcmp(name,"RemoveBuddy"))
    {
        csString command;
        command.Format("/notbuddy %s", value);
        psengine->GetCmdHandler()->Execute(command);
        
        currentBuddy = "";
    }
    else
    {
        csString command;
        command.Format("/tell %s %s", name, value );
        psengine->GetCmdHandler()->Execute(command, false);
    }
}

