/*
 * Copyright (c) 2008, 2011, Oracle  and/or its affiliates. All rights reserved.
 *
 * Use is subject to license terms.
 */

package com.sun.javafx.tk.quantum;


import com.sun.glass.events.WindowEvent;
import com.sun.glass.ui.Window;
import com.sun.javafx.tk.FocusCause;

public class GlassWindowEventHandler extends Window.EventHandler {

    private final GlassStage stage;

    public GlassWindowEventHandler(GlassStage stage) {
        this.stage = stage;
    }

    @Override
    public void handleWindowEvent(Window window, long time, int type) {
        if (stage == null || stage.stageListener == null) {
            return;
        }

        switch (type) {
            case WindowEvent.MINIMIZE:
                stage.stageListener.changedIconified(true);
                break;
            case WindowEvent.MAXIMIZE:
                // stage.stageListener.changeMaximized(true) - TODO
                break;
            case WindowEvent.RESTORE:
                stage.stageListener.changedIconified(false); 
                // stage.stageListener.changeMaximized(false) - TODO
                break;
            case WindowEvent.MOVE:
                stage.stageListener.changedLocation(window.getX(), window.getY());
                break;
            case WindowEvent.RESIZE:
                stage.stageListener.changedSize(window.getWidth(), window.getHeight());
                break;
            case WindowEvent.FOCUS_GAINED:
                GlassStage.addActiveWindow(stage);
                stage.stageListener.changedFocused(true, FocusCause.ACTIVATED);
                break;
            case WindowEvent.FOCUS_LOST:
                stage.stageListener.changedFocused(false, FocusCause.DEACTIVATED);
                break;
            case WindowEvent.FOCUS_UNGRAB:
                stage.stageListener.focusUngrab();
                break;
            case WindowEvent.FOCUS_GAINED_FORWARD:
                GlassStage.addActiveWindow(stage);
                stage.stageListener.changedFocused(true, FocusCause.TRAVERSED_FORWARD);
                break;
            case WindowEvent.FOCUS_GAINED_BACKWARD:
                GlassStage.addActiveWindow(stage);
                stage.stageListener.changedFocused(true, FocusCause.TRAVERSED_BACKWARD);
                break;
            case WindowEvent.FOCUS_DISABLED:
                stage.handleFocusDisabled();
                break;
            case WindowEvent.DESTROY:
                stage.setPlatformWindowClosed();
                stage.stageListener.closed();
                break;
            case WindowEvent.CLOSE:
                stage.stageListener.closing();
                break;
        }
    }
}
