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

package com.sun.glass.tests;

import com.sun.glass.ui.*;
import com.sun.glass.events.KeyEvent;
import com.sun.glass.events.MouseEvent;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.AfterClass;

import java.util.List;

// http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/WinPanel/Concepts/UsingPanels.html
public class WindowVsPanel
{
    static int WINDOW_X = 101;
    static int WINDOW_Y = 102;
    static int WINDOW_W = 512;
    static int WINDOW_H = 256;
    
    private static volatile Robot robot;

    private void sleep(long time)
    {
        try { Thread.sleep(time); } catch (InterruptedException e) {}
    }
    
    static public Window createWindow(Application app, boolean panel, int x, int y)
    {
        int windowMask = Window.TITLED|Window.CLOSABLE|Window.MINIMIZABLE;
        if (panel == true)
        {
            windowMask |= Window.UTILITY;
        }
        Window window = app.createWindow(Screen.getMainScreen(), windowMask);
        window.setPosition(x, y);
        window.setContentSize(WINDOW_W, WINDOW_H);
        window.setTitle(app.getName());
        return window;
    }
    
    @BeforeClass
    static public void prepare()
    {
        try
        {
            Application app = Application.GetApplication();
            
            WindowVsPanel.robot = app.createRobot();
            
            Window window = createWindow(app, false, WINDOW_X, WINDOW_Y);
            window.setVisible(true);

            Window panel = createWindow(app, true, WINDOW_X+WINDOW_W+10, WINDOW_Y);
            panel.setVisible(true);
        }
        catch (Throwable t)
        {
            t.printStackTrace();
            System.exit(0);
        }
    }

    @AfterClass
    static public void finish()
    {
        Window window = Window.getWindows().get(0);
        Window panel = Window.getWindows().get(1);
        window.close();
        panel.close();
    }

    @Test
    public void testWindowsList()
    {
        List<Window> list = Window.getWindows();
        assertTrue(list != null);
        assertTrue(list.size() == 2);
    }

    @Test
    public void testVisible()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        assertTrue(window.isVisible() == true);

        Window panel = Window.getWindows().get(1);
        assertTrue(panel != null);
        assertTrue(panel.isVisible() == true);
    }

    @Test
    public void testWindowW()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        assertTrue(window.getWidth() == WINDOW_W);
    }

    @Test
    public void testWindowH()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);
        assertTrue(window.getHeight() > WINDOW_H);
    }

    @Test
    public void testPanelW()
    {
        Window panel = Window.getWindows().get(1);
        assertTrue(panel != null);
        assertTrue(panel.getWidth() == WINDOW_W);
    }

    @Test
    public void testPanelH()
    {
        Window panel = Window.getWindows().get(1);
        assertTrue(panel != null);
        assertTrue(panel.getHeight() > WINDOW_H);
    }

    @Test
    public void testPanelEsc()
    {
        Window panel = Window.getWindows().get(1);
        assertTrue(panel != null);

        panel.toFront();
        panel.requestFocus();
        sleep(100);

        WindowVsPanel.robot.keyPress(KeyEvent.VK_ESCAPE);
        sleep(10);
        WindowVsPanel.robot.keyRelease(KeyEvent.VK_ESCAPE);
        sleep(10);

        // NSPanels on Mac will close when key and ESC pressed
        // but Glass Panels (Utility) Windows should behave
        // just like any other window - ie. stay open
        assertTrue(panel.isVisible() == true);
    }

    @Test
    public void testPanelOnDeactive()
    {
        Window panel = Window.getWindows().get(1);
        assertTrue(panel != null);

        panel.toFront();
        panel.requestFocus();
        sleep(100);

        assertTrue(panel.isVisible() == true);
        
        int x = panel.getX() + panel.getWidth() + 10;
        int y = panel.getY() + panel.getHeight() + 10;
        WindowVsPanel.robot.mouseMove(x, y);
        sleep(10);
        WindowVsPanel.robot.mousePress(MouseEvent.BUTTON_LEFT);
        sleep(10);
        WindowVsPanel.robot.mouseRelease(MouseEvent.BUTTON_LEFT);
        sleep(10);
        
        // NSPanels on Mac will hide when app is not front process
        // but Glass Panels (Utility) Windows should behave
        // just like any other window - ie. stay open
        assertTrue(panel.isVisible() == true);
        
        x = panel.getX() + (panel.getWidth()/2);
        y = panel.getY() + (panel.getHeight()/2);
        WindowVsPanel.robot.mouseMove(x, y);
        sleep(10);
        WindowVsPanel.robot.mousePress(MouseEvent.BUTTON_LEFT);
        sleep(10);
        WindowVsPanel.robot.mouseRelease(MouseEvent.BUTTON_LEFT);
        sleep(10);
    }
    
    @Test
    public void testPanelVsWindow()
    {
        Window window = Window.getWindows().get(0);
        assertTrue(window != null);

        Window panel = Window.getWindows().get(1);
        assertTrue(panel != null);

        assertTrue(window.getWidth() == panel.getWidth());
        assertTrue(window.getHeight() > panel.getHeight());
    }
}
