/*
 * Manual tests for Sendkeys
 *
 * Copyright 2019 Raising the Floor - International
 *
 * Licensed under the New BSD license. You may not use this file except in
 * compliance with this License.
 *
 * The R&D leading to these results received funding from the
 * Department of Education - Grant H421A150005 (GPII-APCP). However,
 * these results do not necessarily represent the policy of the
 * Department of Education, and you should not assume endorsement by the
 * Federal Government.
 *
 * You may obtain a copy of the License at
 * https://github.com/GPII/universal/blob/master/LICENSE.txt
 */

"use strict";

var fluid = require("gpii-universal"),
    child_process = require("child_process");

require("../../WindowsUtilities/WindowsUtilities.js");
require("../src/sendKeys.js");

var ffi = require("ffi-napi"),
    readline = require("readline");

var gpii = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.tests.windows.sendKeys");

var kernel32 = ffi.Library("kernel32", {
    // https://docs.microsoft.com/en-us/windows/console/getconsolewindow
    "GetConsoleWindow": [
        "uint", []
    ]
});

var user32 = ffi.Library("user32", {
    // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow
    "SetForegroundWindow": [
        "uint", [ "uint" ]
    ]
});

gpii.tests.windows.sendKeys.manualTests = [
    "hello",
    "+{home}",
    "^X",
    "^{V 5}",
    "{enter}waiting 5 seconds...{wait 5}done{enter}",
    "^h{wait window}hello{tab}bye!a{esc}"
];

var rl = readline.createInterface({
    input: process.stdin, output: process.stdout
});

var currentTest = 0;
var consoleWindow = kernel32.GetConsoleWindow();
var notepad;
var notepadWindow;

rl.question("\n\nHit enter to start", function () {
    notepad = child_process.spawn("notepad.exe");

    // Wait for notepad to start, and get the window
    console.log("Waiting for notepad...");
    var timer = setInterval(function () {
        var windows = gpii.windows.findWindows("Notepad");
        fluid.each(windows, function (hwnd) {
            var pid = gpii.windows.getWindowProcessId(hwnd);
            if (pid === notepad.pid) {
                notepadWindow = hwnd;
                clearTimeout(timer);
                nextTest();
            }
        });

    }, 1000);
});

function activateWindow(hwnd) {
    return gpii.windows.waitForCondition(function () {
        user32.SetForegroundWindow(hwnd);
        return gpii.windows.user32.GetForegroundWindow() === hwnd;
    });
}

function nextTest() {

    activateWindow(consoleWindow).then(function () {
        var test = gpii.tests.windows.sendKeys.manualTests[currentTest++];
        if (!test) {
            console.log("the end");
            process.exit();
        }
        rl.question("Hit enter to send: " + test, function () {
            activateWindow(notepadWindow).then(function () {
                setTimeout(function () {
                    gpii.windows.sendKeys.send(test).then(function () {
                        setTimeout(nextTest, 500);
                    });
                }, 500);
            });
        });
    });
}


console.log("tasd");
