/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 */
package com.sun.javafx.tk.quantum;

import com.sun.glass.ui.ClipboardAssistance;
import com.sun.javafx.embed.EmbeddedSceneDragSourceInterface;
import com.sun.javafx.embed.EmbeddedSceneDragStartListenerInterface;
import com.sun.javafx.embed.EmbeddedSceneDropTargetInterface;
import java.util.concurrent.Callable;
import javafx.application.Platform;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;

final class EmbeddedSceneDnD {

    private final GlassSceneDnDEventHandler dndHandler;

    private EmbeddedSceneDragStartListenerInterface dragStartListener;
    private EmbeddedSceneDragSourceInterface fxDragSource;
    private EmbeddedSceneDropTargetInterface fxDropTarget;
    
    private ClipboardAssistance clipboardAssistant;
    
    private Thread hostThread;

    public EmbeddedSceneDnD(final GlassScene scene) {
        this.dndHandler = new GlassSceneDnDEventHandler(scene, null);
    }
    
    private void startDrag() {
        assert Platform.isFxApplicationThread();

        assert fxDragSource == null;

        fxDragSource = new EmbeddedSceneDragSource(this, dndHandler);
        
        // FIXME: init properly!
        final TransferMode dragAction = TransferMode.COPY;
        
        dragStartListener.dragStarted(fxDragSource, dragAction);
    }

    private void setHostThread() {
        if (hostThread == null) {
            hostThread = Thread.currentThread();
        }
    }

    public boolean isHostThread() {
        return (Thread.currentThread() == hostThread);
    }
    
    public boolean isValid(EmbeddedSceneDragSourceInterface ds) {
        assert Platform.isFxApplicationThread();
        assert ds != null;
        assert fxDragSource == ds;
        assert clipboardAssistant != null;
        return true;
    }
    
    public boolean isValid(EmbeddedSceneDropTargetInterface dt) {
        assert Platform.isFxApplicationThread();
        assert dt != null;
        assert fxDropTarget == dt;
        return true;
    }
    
    public boolean isFxDragSource() {
        assert Platform.isFxApplicationThread();

        return (fxDragSource != null);
    }

    public void onDragSourceReleased(final EmbeddedSceneDragSourceInterface ds) {
        assert isValid(ds);

        fxDragSource = null;
        clipboardAssistant = null;
        
        FxEventLoop.leaveNestedLoop();
    }

    public void onDropTargetReleased(final EmbeddedSceneDropTargetInterface dt) {
        assert isValid(dt);

        fxDropTarget = null;
        if (!isFxDragSource()) {
            clipboardAssistant = null;
        }
    }
    
    // Should be called from Scene.DnDGesture.createDragboard only!
    public Dragboard createDragboard() {
        assert Platform.isFxApplicationThread();
        assert fxDropTarget == null;
        assert clipboardAssistant == null;
        assert fxDragSource == null;
        
        clipboardAssistant = new ClipboardAssistanceImpl(null);
        
        return GlassSceneDnDEventHandler.createDragboard(clipboardAssistant);
    };    

    public void setDragStartListener(EmbeddedSceneDragStartListenerInterface l) {
        setHostThread();
        
        assert isHostThread();
        
        this.dragStartListener = l;
    }
    
    public EmbeddedSceneDropTargetInterface createDropTarget() {
        setHostThread();

        assert isHostThread();
        assert fxDropTarget == null;

        return FxEventLoop.sendEvent(new Callable<EmbeddedSceneDropTargetInterface>() {

            @Override
            public EmbeddedSceneDropTargetInterface call() {
                fxDropTarget = new EmbeddedSceneDropTarget(EmbeddedSceneDnD.this,
                                                           dndHandler);

                return fxDropTarget;
            }
        });
    }
    
    public ClipboardAssistance getClipboardAssistance(
            final EmbeddedSceneDragSourceInterface source) {
        assert Platform.isFxApplicationThread();
        assert isFxDragSource() ? isValid(source) : true;
        assert isFxDragSource() ? (clipboardAssistant != null) : true;
        
        if (clipboardAssistant == null) {
            clipboardAssistant = new ClipboardAssistanceImpl(source);
        }
        return clipboardAssistant;
    }

    private class ClipboardAssistanceImpl extends ClipboardAssistance {
        final EmbeddedSceneDragSourceInterface source;
                
        private boolean isValid() {
            assert Platform.isFxApplicationThread();
            assert EmbeddedSceneDnD.this.clipboardAssistant == this;
            
            return true;
        }
        
        ClipboardAssistanceImpl(final EmbeddedSceneDragSourceInterface source) {
            super("DND-Embedded");
            
            this.source = source;
        }

        @Override
        public void flush() {
            assert isValid();

            super.flush();
            
            startDrag();

            FxEventLoop.enterNestedLoop();
        }

        @Override
        public void emptyCache() {
            assert isValid();

            super.emptyCache();
        }

        @Override
        public Object getData(final String mimeType) {
            assert isValid();

            if (source == null) {
                return super.getData(mimeType);
            }

            return source.getData(mimeType);
        }

        @Override
        public void setData(final String mimeType, final Object data) {
            assert isValid();
            
            if (source != null) {
                return;
            }

            super.setData(mimeType, data);
        }

        @Override
        public void setSupportedActions(final int supportedActions) {
            assert isValid();
            
            if (source != null) {
                return;
            }

            super.setSupportedActions(supportedActions);
        }

        @Override
        public int getSupportedSourceActions() {
            assert isValid();

            if (source == null) {
                return super.getSupportedSourceActions();
            }

            return QuantumClipboard.transferModesToClipboardActions(source.
                    getSupportedActions());
        }

        @Override
        public void setTargetAction(int actionDone) {
            assert isValid();

            throw new UnsupportedOperationException();
        }

        @Override
        public String[] getMimeTypes() {
            assert isValid();

            if (source == null) {
                return super.getMimeTypes();
            }

            return source.getMimeTypes();
        }
    }
}
