package com.heavylead.views.concrete.gbui;

import junit.framework.TestCase;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.heavylead.injection.HeavyLeadTestModule;
import com.heavylead.wrappers.concrete.DisplaySystem;
import com.heavylead.wrappers.interfaces.IDisplaySystem;
import com.jme.input.InputHandler;
import com.jme.util.Timer;
import com.jmex.bui.BButton;
import com.jmex.bui.BuiSystem;
import com.jmex.bui.PolledRootNode;
import com.jmex.bui.event.MouseEvent;
import com.jmex.game.state.GameStateManager;

/**
 * The Class GbuiTestCase.
 */
public abstract class GbuiTestCase extends TestCase {
    /** The Constant TINY_TICK. */
    public static final float TINY_TICK = 0.1f;
    
    private static final int TEST_FREQUENCY = 60;

    private static final int TEST_DEPTH = 16;

    private static final int TEST_WIDTH = 10;

    private static final int TEST_HEIGHT = 10;

    private Injector _injector;
    
    /** The _input handler. */
    private InputHandler _inputHandler;

    /** The _display. */
    private IDisplaySystem _displaySystem;

    /**
     * Sets the up gbui.
     */
    protected final void setUpGbui() {
        _injector = Guice.createInjector(new HeavyLeadTestModule());

        _displaySystem = new DisplaySystem("LWJGL");
        _displaySystem.createWindow(TEST_WIDTH, TEST_HEIGHT, TEST_DEPTH, TEST_FREQUENCY, false);

        _inputHandler = new InputHandler();

        final PolledRootNode polledRootNode = new PolledRootNode(Timer.getTimer(),
                _inputHandler);
        
        BuiSystem.init(polledRootNode, "/data/gbui/heavylead.bss");

        GameStateManager.create();
    }
    
    /**
     * Tear down gbui.
     */
    protected final void tearDownGbui() {
        _displaySystem.close();
    }
    
    /**
     * Gets the injector.
     * 
     * @return the injector
     */
    protected final Injector getInjector() {
        return _injector;        
    }
    
    /**
     * Gets the input handler.
     * 
     * @return the input handler
     */
    protected final InputHandler getInputHandler() {
        return _inputHandler;
    }
    
    /**
     * Perform click.
     * 
     * @param button
     *            the button
     */
    protected final void performClick(final BButton button) {
        // In order to trigger a click event, we need to first fire
        // a Mouse Pressed and then Mouse Released
        final MouseEvent mousePressedEvent = new MouseEvent(this, 1, 0,
                MouseEvent.BUTTON1, MouseEvent.MOUSE_PRESSED, 5, 5);
        button.dispatchEvent(mousePressedEvent);
        final MouseEvent mouseReleasedEvent = new MouseEvent(this, 2, 0,
                MouseEvent.MOUSE_RELEASED, 5, 5);
        button.dispatchEvent(mouseReleasedEvent);
    }
}