package at.oefai.aaa.animation;

import org.apache.batik.bridge.UpdateManagerEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent;

import at.oefai.aaa.IGUIMediator;
import at.oefai.aaa.IState;

/**
 * encapsulates message passing from and to the SVGDoc and JSVGCanvas.
 * @author wrstlprmpft
 */
public class GraphicsBridge implements ISvgListener {
    private final IGUIMediator guiMediator;
    private final IState stateManager;
    private final AnimationEngine animEngine;

    public GraphicsBridge(final IGUIMediator guiMed,
            final IState stateMan, final AnimationEngine animEng) {
        this.guiMediator = guiMed;
        this.stateManager = stateMan;
        this.animEngine = animEng;
    }

    public final void gvtBuildStarted(final GVTTreeBuilderEvent e) {
        this.guiMediator.showGraphicsStatus("gvtBuildStarted()");
    }

    public final void gvtBuildCompleted(final GVTTreeBuilderEvent e) {
        this.guiMediator.showGraphicsStatus("gvtBuildCompleted()");
    }

    public final void gvtBuildCancelled(final GVTTreeBuilderEvent e) {
        this.guiMediator.showGraphicsStatus("gvtBuildCancelled()");
    }

    public final void gvtBuildFailed(final GVTTreeBuilderEvent e) {
        this.guiMediator.showGraphicsStatus("gvtBuildFailed()");
    }

    public final void svgLoadEventDispatchStarted(final SVGLoadEventDispatcherEvent e) {
        this.guiMediator.showGraphicsStatus("svgLoadEventDispatchStarted()");
        // tell the AnimationEngine that the updatemanager is available
        this.animEngine.cacheUpdateManager();
    }

    public final void svgLoadEventDispatchCompleted(final SVGLoadEventDispatcherEvent e) {
        this.guiMediator.showGraphicsStatus("svgLoadEventDispatchCompleted()");
        // now init the svg file to reflect the worldmodel, synch
        this.stateManager.synchronize(); // posts anims which later synchronize the stage
    }

    public final void svgLoadEventDispatchCancelled(final SVGLoadEventDispatcherEvent e) {
        this.guiMediator.showGraphicsStatus("svgLoadEventDispatchCancelled()");
    }

    public final void svgLoadEventDispatchFailed(final SVGLoadEventDispatcherEvent e) {
        this.guiMediator.showGraphicsStatus("svgLoadEventDispatchFailed()");
    }

    public final void managerStarted(final UpdateManagerEvent e) {
        this.guiMediator.showGraphicsStatus("managerStarted()");
        // actually we should wait until all updates of a synchronizeSVG() call are done
        // before reactivating the GUI, but this isnt necessary for now (assume that
        // the runnable queue of batik orders the runnables correctly;-).
        this.guiMediator.activateGUI();
    }

    public final void managerSuspended(final UpdateManagerEvent e) {
        this.guiMediator.showGraphicsStatus("managerSuspended()");
    }

    public final void managerResumed(final UpdateManagerEvent e) {
        this.guiMediator.showGraphicsStatus("managerResumed()");
        this.guiMediator.activateGUI();
    }

    public final void managerStopped(final UpdateManagerEvent e) {
        this.guiMediator.showGraphicsStatus("managerStopped()");
    }

    public final void updateStarted(final UpdateManagerEvent e) {
        this.guiMediator.showGraphicsStatus("updateStarted()");
    }

    public final void updateCompleted(final UpdateManagerEvent e) {
        this.guiMediator.showGraphicsStatus("updateCompleted()");
    }

    public final void updateFailed(final UpdateManagerEvent e) {
        this.guiMediator.showGraphicsStatus("updateFailed()");
    }

    public final void gvtRenderingPrepare(final GVTTreeRendererEvent e) {
        this.guiMediator.showGraphicsStatus("gvtRenderingPrepare()");
    }

    public final void gvtRenderingStarted(final GVTTreeRendererEvent e) {
        this.guiMediator.showGraphicsStatus("gvtRenderingStarted()");
    }

    public final void gvtRenderingCompleted(final GVTTreeRendererEvent e) {
        this.guiMediator.showGraphicsStatus("gvtRenderingCompleted()");
    }

    public final void gvtRenderingCancelled(final GVTTreeRendererEvent e) {
        this.guiMediator.showGraphicsStatus("gvtRenderingCancelled()");
    }

    public final void gvtRenderingFailed(final GVTTreeRendererEvent e) {
        this.guiMediator.showGraphicsStatus("gvtRenderingFailed()");
    }
}
