
/**
 * Configurable delays for the tests
 */
public final class Delays {

    private static final String KEY_DELAY_PROPERTY = "KEY_DELAY_FACTOR";
    private static final int DEFAULT_KEY_DELAY = 50;
    private static int keyDelay = DEFAULT_KEY_DELAY;
    private static final String MOUSE_DELAY_PROPERTY = "MOUSE_DELAY";
    private static final int DEFAULT_MOUSE_DELAY = 50;
    private static int mouseDelay = DEFAULT_MOUSE_DELAY;
    private static final String ROBOT_DELAY_PROPERTY = "TEST_DELAY_FACTOR";
    private static final int DEFAULT_ROBOT_DELAY = 500;
    private static int robotDelay = DEFAULT_ROBOT_DELAY;
    private static final String WAIT_DELAY_PROPERTY = "WAIT_DELAY";
    private static final int DEFAULT_WAIT_DELAY = 5000;
    private static int waitDelay = DEFAULT_WAIT_DELAY;

    private static int initDelay(String name, int defVal) {
        int val = defVal;
        String sval = System.getProperty(name);
        if (sval != null) {
            try {
                val = Integer.parseInt(sval);
            } catch (Exception e) {
                System.err.println("Incorrect " + name + " delay value " + sval + " specified, using default " + val);
            }
        }
        System.out.println("Delay " + name + " for this test is " + val + " ms");
        return val;
    }

    static {
        keyDelay = initDelay(KEY_DELAY_PROPERTY, DEFAULT_KEY_DELAY);
        mouseDelay = initDelay(MOUSE_DELAY_PROPERTY, DEFAULT_MOUSE_DELAY);
        robotDelay = initDelay(ROBOT_DELAY_PROPERTY, DEFAULT_ROBOT_DELAY);
        waitDelay = initDelay(WAIT_DELAY_PROPERTY, DEFAULT_WAIT_DELAY);
    }

    /**
     * Delay between mouse press and release
     */
    public static int getMouseDelay() {
        return mouseDelay;
    }

    /**
     * Delay between key press and release
     */
    public static int getKeyDelay() {
        return keyDelay;
    }

    /**
     * Delay in robot.pause()
     */
    public static int getRobotDelay() {
        return robotDelay;
    }

    /**
     * Delay in lock.wait()
     */
    public static int getWaitDelay() {
        return waitDelay;
    }

    /**
     * Delay between mouse press and release
     *
     * @param delay to use if no delay configured
     */
    public static int getMouseDelay(int defVal) {
        return mouseDelay == DEFAULT_MOUSE_DELAY ? defVal : mouseDelay;
    }

    /**
     * Delay between key press and release
     *
     * @param delay to use if no delay configured
     */
    public static int getKeyDelay(int defVal) {
        return keyDelay == DEFAULT_KEY_DELAY ? defVal : keyDelay;
    }

    /**
     * Delay in robot.pause()
     *
     * @param delay to use if no delay configured
     */
    public static int getRobotDelay(int defVal) {
        return robotDelay == DEFAULT_ROBOT_DELAY ? defVal : robotDelay;
    }

    /**
     * Delay in lock.wait()
     *
     * @param delay to use if no delay configured
     */
    public static int getWaitDelay(int defVal) {
        return waitDelay == DEFAULT_WAIT_DELAY ? defVal : waitDelay;
    }
}
