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

package com.sun.javafx.tk.quantum;

import com.sun.glass.ui.Application;
import com.sun.glass.ui.Window;
import com.sun.javafx.tk.TKStage;
import com.sun.javafx.tk.desktop.AppletWindow;
import java.lang.ref.WeakReference;
import javafx.stage.Stage;

/**
 * Implementation class for AppletWindow using a Glass Window
 * @author daved
 */
public class GlassAppletWindow implements AppletWindow {
    private final Window glassWindow;
    private WeakReference<Stage> topStage;
    private String serverName;
    
    GlassAppletWindow(long nativeParent, String serverName) {
        if (0 == nativeParent) {
            glassWindow = Application.GetApplication().createWindow(null, Window.NORMAL);
        } else {
            this.serverName = serverName;
            glassWindow = Application.GetApplication().createWindow(nativeParent);
        }
    }
    
    Window getGlassWindow() {
        return glassWindow;
    }
    
    @Override
    public void setBackgroundColor(int color) {
        float RR = (float)((color >> 16) & 0xff) / 255f;
        float GG = (float)((color >> 8)  & 0xff) / 255f;
        float BB = (float)(color & 0xff) / 255f;
        glassWindow.setBackground(RR, GG, BB);
    }

    @Override
    public void setForegroundColor(int color) {
        // TODO: foreground color? Glass doesn't support this
    }

    @Override
    public void setVisible(boolean state) {
        glassWindow.setVisible(state);
    }

    @Override
    public void setSize(int width, int height) {
        glassWindow.setSize(width, height);
    }

    @Override
    public int getWidth() {
        return glassWindow.getWidth();
    }

    @Override
    public int getHeight() {
        return glassWindow.getHeight();
    }

    @Override
    public void setPosition(int x, int y) {
        glassWindow.setPosition(x, y);
    }

    @Override
    public int getPositionX() {
        return glassWindow.getX();
    }

    @Override
    public int getPositionY() {
        return glassWindow.getY();
    }

    void dispose() {
        glassWindow.close();
    }

    @Override
    public void setStageOnTop(Stage topStage) {
        if (null != topStage) {
            this.topStage = new WeakReference<Stage>(topStage);
        } else {
            this.topStage = null;
        }
    }
    
    @Override
    public int getRemoteLayerId() {
        com.sun.glass.ui.View view = glassWindow.getView();
        if (view != null) {
            // 0 if we were not able to get the id
            // >0 marks real id
            return view.getNativeRemoteLayerId(this.serverName);
        } else {
            // -1 to tell plugin view is not ready
            return -1;
        }
    }
    
    /**
     * Call when a child stage becomes visible so we can make sure topStage
     * is pushed to the front where it should be.
     */
    void assertStageOrder() {
        if (null != topStage) {
            Stage ts = topStage.get();
            if (null != ts) {
                TKStage tsp = ts.impl_getPeer();
                if (tsp instanceof WindowStage && ((WindowStage)tsp).isVisible()) {
                    // call the underlying Glass window toFront to bypass
                    // the check in WindowStage.toFront or we'll create an
                    // infinite loop
                    Window pw = ((WindowStage)tsp).getPlatformWindow();
                    if (null != pw) {
                        pw.toFront();
                    }
                }
            }
        }
    }
}
