/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 */

#import "GlassDragSource.h"
#import "GlassMacros.h"

#import "com_sun_glass_ui_Clipboard.h"

//#define VERBOSE
#ifndef VERBOSE
    #define LOG(MSG, ...)
#else
    #define LOG(MSG, ...) GLASS_LOG(MSG, ## __VA_ARGS__);
#endif

static NSObject<GlassDragSourceDelegate> *gDelegate = nil;
static jint gMask = com_sun_glass_ui_Clipboard_ACTION_NONE;

@implementation GlassDragSource

+ (void)setDelegate:(NSObject<GlassDragSourceDelegate>*)delegate
{
    LOG("GlassDragSource:setDelegate");
    
    gDelegate = delegate; // notice, there is no retain
}

+ (void)flushWithMask:(jint)mask
{
    LOG("GlassDragSource:flushWithMask: %d", mask);
    
    if ([NSThread isMainThread] == YES)
    {
        NSDragOperation operation = [GlassDragSource mapJavaMaskToNsOperation:mask];        
        if (operation != NSDragOperationNone)
        {
            LOG("[gDelegate startDrag:operation]");
        	[gDelegate startDrag:operation];
        }
        else
        {
            LOG("[gDelegate startDrag:operation] NOT TAKEN!");
        }
    }
    else
    {
        NSLog(@"GlassDragSource flush called on nonmain thread!");
    }
}

+ (NSDragOperation)mapJavaMaskToNsOperation:(jint)mask
{
    LOG("GlassDragSource:mapJavaMaskToNsOperation: %d", mask);
    
    NSDragOperation operation = NSDragOperationNone;
    if (mask == com_sun_glass_ui_Clipboard_ACTION_NONE)
    {
        LOG("   com_sun_glass_ui_Clipboard_ACTION_NONE");
        operation = NSDragOperationNone;
    }
    else if (mask == com_sun_glass_ui_Clipboard_ACTION_COPY)
    {
        LOG("   com_sun_glass_ui_Clipboard_ACTION_COPY");
        operation = NSDragOperationCopy;
    }
    else if (mask == com_sun_glass_ui_Clipboard_ACTION_MOVE)
    {
        LOG("   com_sun_glass_ui_Clipboard_ACTION_MOVE");
        operation = NSDragOperationMove;
    }
    else if (mask == com_sun_glass_ui_Clipboard_ACTION_REFERENCE)
    {
        LOG("   com_sun_glass_ui_Clipboard_ACTION_REFERENCE");
        operation = NSDragOperationLink;
    }
    else if (mask == com_sun_glass_ui_Clipboard_ACTION_COPY_OR_MOVE)
    {
        LOG("   com_sun_glass_ui_Clipboard_ACTION_COPY_OR_MOVE");
        operation = NSDragOperationCopy|NSDragOperationMove;
    }
    else if (mask == com_sun_glass_ui_Clipboard_ACTION_ANY)
    {
        LOG("   com_sun_glass_ui_Clipboard_ACTION_ANY");
        operation = NSDragOperationEvery;
    }
    else
    {
        if ((com_sun_glass_ui_Clipboard_ACTION_COPY&mask)==com_sun_glass_ui_Clipboard_ACTION_COPY)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_COPY");
            operation |= NSDragOperationCopy;
        }
        
        if ((com_sun_glass_ui_Clipboard_ACTION_MOVE&mask)==com_sun_glass_ui_Clipboard_ACTION_MOVE)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_MOVE");
            operation |= NSDragOperationMove;
        }
        
        if ((com_sun_glass_ui_Clipboard_ACTION_COPY_OR_MOVE&mask)==com_sun_glass_ui_Clipboard_ACTION_COPY_OR_MOVE)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_COPY_OR_MOVE");
            operation |= NSDragOperationCopy|NSDragOperationMove;
        }
        
        if ((com_sun_glass_ui_Clipboard_ACTION_REFERENCE&mask)==com_sun_glass_ui_Clipboard_ACTION_REFERENCE)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_REFERENCE");
            operation |= NSDragOperationLink;
        }
        
        if ((com_sun_glass_ui_Clipboard_ACTION_ANY&mask)==com_sun_glass_ui_Clipboard_ACTION_ANY)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_ANY");
            operation |= NSDragOperationEvery;
        }
    }
    return operation;
}

+ (jint)mapNsOperationToJavaMask:(NSDragOperation)operation
{
    LOG("GlassDragSource:mapNsOperationToJavaMask: %d", operation);
    
    jint mask = com_sun_glass_ui_Clipboard_ACTION_NONE;
    if (operation == NSDragOperationNone) 
    {
        return mask;
    }
    else if (operation == NSDragOperationEvery)
    {
        mask = com_sun_glass_ui_Clipboard_ACTION_ANY;
    }
    else if (operation == NSDragOperationCopy)
    {
        mask = com_sun_glass_ui_Clipboard_ACTION_COPY;
    }
    else if (operation == NSDragOperationMove)
    {
        mask = com_sun_glass_ui_Clipboard_ACTION_MOVE;
    }
    else if (operation == (NSDragOperationCopy|NSDragOperationMove))
    {
        mask = com_sun_glass_ui_Clipboard_ACTION_COPY_OR_MOVE;
    }
    else if (operation == NSDragOperationLink)
    {
        mask = com_sun_glass_ui_Clipboard_ACTION_REFERENCE;
    }
    else
    {
        if ((NSDragOperationEvery&operation)==NSDragOperationEvery)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_ANY");
            mask = com_sun_glass_ui_Clipboard_ACTION_ANY;
        }
        else if ((NSDragOperationCopy&operation)==NSDragOperationCopy)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_COPY");
            mask = com_sun_glass_ui_Clipboard_ACTION_COPY;
        }
        else if ((NSDragOperationLink&operation)==NSDragOperationLink)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_REFERENCE");
            mask |= com_sun_glass_ui_Clipboard_ACTION_REFERENCE;
        }
        else if ((NSDragOperationMove&operation)==NSDragOperationMove)
        {
            LOG("   masked com_sun_glass_ui_Clipboard_ACTION_MOVE");
            mask |= com_sun_glass_ui_Clipboard_ACTION_MOVE;
        }
        else
        {
            GLASS_LOG("WARNING: unhandled case in mapNsOperationToJavaMask: %d", operation);
        }
    }
    return mask;
}

+ (void)setMask:(jint)mask
{
    LOG("GlassDragSource:mask");
    
    gMask = mask;
}

+ (jint)getMask
{
    LOG("GlassDragSource:getMask");
    
    return gMask;
}

@end
