/*==LICENSE==*

CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011  Cyan Worlds, Inc.

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 3 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, see <http://www.gnu.org/licenses/>.

Additional permissions under GNU GPL version 3 section 7

If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.

You can contact Cyan Worlds, Inc. by email legal@cyan.com
 or by snail mail at:
      Cyan Worlds, Inc.
      14617 N Newport Hwy
      Mead, WA   99021

*==LICENSE==*/
//////////////////////////////////////////////////////////////////////////////
//                                                                          //
//  pfGUIButtonMod Definition                                               //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////

#include "HeadSpin.h"
#include "pfGUIButtonMod.h"
#include "pfGUIDraggableMod.h"
#include "pfGameGUIMgr.h"
#include "pfGUIControlHandlers.h"
#include "pfGUIDialogMod.h"

#include "plInputCore/plInputInterface.h"
#include "pnMessage/plRefMsg.h"
#include "pfMessage/pfGameGUIMsg.h"
#include "plMessage/plAnimCmdMsg.h"
#include "plAvatar/plAGModifier.h"
#include "plgDispatch.h"
#include "hsResMgr.h"


//// Control Proc For Managing the Draggable /////////////////////////////////

class pfGUIButtonDragProc : public pfGUICtrlProcObject
{
    protected:

        pfGUICtrlProcObject *fOrigProc;

        pfGUIButtonMod      *fParent;
        pfGUIDraggableMod   *fDraggable;
        bool                fReportDrag;

    public:

        pfGUIButtonDragProc( pfGUIButtonMod *parent, pfGUIDraggableMod *draggable, pfGUICtrlProcObject *origProc, bool reportDrag )
        {
            fParent = parent;
            fDraggable = draggable;
            fOrigProc = origProc;
            fReportDrag = reportDrag;
        }

        virtual void    DoSomething( pfGUIControlMod *ctrl )
        {
            // The draggable was let up, so now we stop dragging, disable the draggable again, and pass
            // on the event to our original proc
            if( fOrigProc != nil && fParent->IsTriggering() )
                fOrigProc->DoSomething( ctrl );
            if (!fParent->IsButtonDown())
                fParent->StopDragging( false );
        }

        virtual void    HandleExtendedEvent( pfGUIControlMod *ctrl, uint32_t event )
        {
            if( event == pfGUIDraggableMod::kDragging )
            {
                // First test if we're inside our button (if so, we stop dragging)
                if( fParent->PointInBounds( fDraggable->GetLastMousePt() ) )
                {
                    // Cancel the drag
                    fParent->StopDragging( true );
                    return;
                }

                if( !fReportDrag )
                    return;
            }
            
            if( fOrigProc != nil )
                fOrigProc->HandleExtendedEvent( ctrl, event );
        }

        virtual void    UserCallback( uint32_t userValue )
        {
            if( fOrigProc != nil )
                fOrigProc->UserCallback( userValue );
        }
};


void    pfGUIButtonMod::StopDragging( bool cancel )
{
    fDraggable->StopDragging( cancel );
    fDraggable->SetVisible( false );
    fDraggable->SetHandler( fOrigHandler );
    fOrigHandler = nil;

    if( !fOrigReportedDrag )
        fDraggable->ClearFlag( pfGUIDraggableMod::kReportDragging );

    // Steal interest back
    fDialog->SetControlOfInterest( this );
}

void    pfGUIButtonMod::StartDragging( void )
{
    fOrigReportedDrag = fDraggable->HasFlag( pfGUIDraggableMod::kReportDragging );
    fDraggable->SetFlag( pfGUIDraggableMod::kReportDragging );

    fOrigHandler = fDraggable->GetHandler();
    fDraggable->SetVisible( true );
    fDraggable->SetHandler( new pfGUIButtonDragProc( this, fDraggable, fOrigHandler, fOrigReportedDrag ) );
    fDraggable->HandleMouseDown( fOrigMouseDownPt, 0 );
}

//// Constructor/Destructor //////////////////////////////////////////////////

pfGUIButtonMod::pfGUIButtonMod()
{
    fDraggable = nil;
    fOrigHandler = nil;

    fClicking = false;
    fTriggering = false;
    fNotifyType = kNotifyOnUp;
    SetFlag( kWantsInterest );
}

//// IEval ///////////////////////////////////////////////////////////////////

bool    pfGUIButtonMod::IEval( double secs, float del, uint32_t dirty )
{
    return pfGUIControlMod::IEval( secs, del, dirty );
}

//// MsgReceive //////////////////////////////////////////////////////////////

bool    pfGUIButtonMod::MsgReceive( plMessage *msg )
{
    plGenRefMsg *refMsg = plGenRefMsg::ConvertNoRef( msg );
    if( refMsg != nil && refMsg->fType == kRefDraggable )
    {
        if( refMsg->GetContext() & ( plRefMsg::kOnCreate | plRefMsg::kOnRequest | plRefMsg::kOnReplace ) )
        {
            fDraggable = pfGUIDraggableMod::ConvertNoRef( refMsg->GetRef() );
            fDraggable->SetVisible( false );        // Disable until we're dragging
        }
        else
            fDraggable = nil;
        return true;
    }

    return pfGUIControlMod::MsgReceive( msg );
}

//// Read/Write //////////////////////////////////////////////////////////////

void    pfGUIButtonMod::Read( hsStream *s, hsResMgr *mgr )
{
    pfGUIControlMod::Read(s, mgr);

    fAnimationKeys.Reset();
    uint32_t i, count = s->ReadLE32();
    for( i = 0; i < count; i++ )
        fAnimationKeys.Append( mgr->ReadKey( s ) );
    fAnimName = s->ReadSafeString_TEMP();

    fMouseOverAnimKeys.Reset();
    count = s->ReadLE32();
    for( i = 0; i < count; i++ )
        fMouseOverAnimKeys.Append( mgr->ReadKey( s ) );
    fMouseOverAnimName = s->ReadSafeString_TEMP();

    fNotifyType = s->ReadLE32();
    mgr->ReadKeyNotifyMe( s, new plGenRefMsg( GetKey(), plRefMsg::kOnCreate, -1, kRefDraggable ), plRefFlags::kActiveRef );
}

void    pfGUIButtonMod::Write( hsStream *s, hsResMgr *mgr )
{
    pfGUIControlMod::Write( s, mgr );

    uint32_t i, count = fAnimationKeys.GetCount();
    s->WriteLE32( count );
    for( i = 0; i < count; i++ )
        mgr->WriteKey( s, fAnimationKeys[ i ] );
    s->WriteSafeString( fAnimName );

    count = fMouseOverAnimKeys.GetCount();
    s->WriteLE32( count );
    for( i = 0; i < count; i++ )
        mgr->WriteKey( s, fMouseOverAnimKeys[ i ] );
    s->WriteSafeString( fMouseOverAnimName );

    s->WriteLE32( fNotifyType );

    mgr->WriteKey( s, fDraggable != nil ? fDraggable->GetKey() : nil );

}

//// UpdateBounds ////////////////////////////////////////////////////////////

void    pfGUIButtonMod::UpdateBounds( hsMatrix44 *invXformMatrix, bool force )
{
    pfGUIControlMod::UpdateBounds( invXformMatrix, force );
    if( fAnimationKeys.GetCount() > 0 || fMouseOverAnimKeys.GetCount() > 0 )
        fBoundsValid = false;
}

//// HandleMouseDown/Up //////////////////////////////////////////////////////

void    pfGUIButtonMod::HandleMouseDown( hsPoint3 &mousePt, uint8_t modifiers )
{
    fClicking = true;
    if( fAnimationKeys.GetCount() > 0 )
    {
        plAnimCmdMsg *msg = new plAnimCmdMsg();
        msg->SetCmd( plAnimCmdMsg::kContinue );
        msg->SetCmd( plAnimCmdMsg::kSetForewards );
        msg->SetCmd( plAnimCmdMsg::kGoToBegin );    
        msg->SetAnimName( fAnimName );
        msg->AddReceivers( fAnimationKeys );
        plgDispatch::MsgSend( msg );
    }

    IPlaySound( kMouseDown );

    fOrigMouseDownPt = mousePt;
    if ( fNotifyType == kNotifyOnDown || fNotifyType == kNotifyOnUpAndDown)
    {
        fTriggering = true;
        DoSomething();
        fTriggering = false;
    }
}

void    pfGUIButtonMod::HandleMouseUp( hsPoint3 &mousePt, uint8_t modifiers )
{

    // make sure that we got the down click first
    if ( !fClicking )
        return;

    fClicking = false;
    if( fAnimationKeys.GetCount() > 0 )
    {
        plAnimCmdMsg *msg = new plAnimCmdMsg();
        msg->SetCmd( plAnimCmdMsg::kContinue );
        msg->SetCmd( plAnimCmdMsg::kSetBackwards );
        msg->SetCmd( plAnimCmdMsg::kGoToEnd );  
        msg->SetAnimName( fAnimName );
        msg->AddReceivers( fAnimationKeys );
        plgDispatch::MsgSend( msg );
    }

    IPlaySound( kMouseUp );

    // Don't run the command if the mouse is outside our bounds
    if( !fBounds.IsInside( &mousePt ) && fNotifyType != kNotifyOnUpAndDown )
        return;     

    if ( fNotifyType == kNotifyOnUp || fNotifyType == kNotifyOnUpAndDown)
        fTriggering = true;
    DoSomething();
    fTriggering = false;
}

void    pfGUIButtonMod::HandleMouseDrag( hsPoint3 &mousePt, uint8_t modifiers )
{
    if( !fClicking )
        return;

    if( fDraggable == nil )
        return;

    if( !fDraggable->IsVisible() )
    {
        // Are we outside ourselves?
        if( !PointInBounds( mousePt ) )
        {
            // Yes, start dragging
            StartDragging();

            // Hand off our interest to the draggable
            fDialog->SetControlOfInterest( fDraggable );
        }
    }
}

void    pfGUIButtonMod::SetNotifyType(int32_t kind)
{
    fNotifyType = kind;
}

int32_t   pfGUIButtonMod::GetNotifyType()
{
    return fNotifyType;
}

bool    pfGUIButtonMod::IsButtonDown()
{
    return fClicking;
}

//// SetInteresting //////////////////////////////////////////////////////////
//  Overridden to play mouse over animation when we're interesting

void    pfGUIButtonMod::SetInteresting( bool i )
{
    pfGUIControlMod::SetInteresting( i );

    if( fMouseOverAnimKeys.GetCount() )
    {
        plAnimCmdMsg *msg = new plAnimCmdMsg();
        msg->SetCmd( plAnimCmdMsg::kContinue );
        msg->SetCmd( fInteresting ? plAnimCmdMsg::kSetForewards : plAnimCmdMsg::kSetBackwards );
        msg->SetAnimName( fMouseOverAnimName );
        msg->AddReceivers( fMouseOverAnimKeys );
        plgDispatch::MsgSend( msg );
    }

    if( i )
        IPlaySound( kMouseOver );
    else
        IPlaySound( kMouseOff );
}


void    pfGUIButtonMod::SetAnimationKeys( hsTArray<plKey> &keys, const plString &name )
{
    fAnimationKeys = keys;
    fAnimName = name;
}

void    pfGUIButtonMod::SetMouseOverAnimKeys( hsTArray<plKey> &keys, const plString &name )
{
    fMouseOverAnimKeys = keys;
    fMouseOverAnimName = name;
}


//// IGetDesiredCursor ///////////////////////////////////////////////////////

uint32_t      pfGUIButtonMod::IGetDesiredCursor( void ) const
{
    if( fHandler == nil )
        return 0;

    if( fClicking )
        return plInputInterface::kCursorClicked;

    return plInputInterface::kCursorPoised;
}