"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const keymaster = require("keymaster");
exports.keymaster = keymaster;
const _1 = require(".");
exports.BaseUI = _1.BaseUI;
exports.Cell = _1.Cell;
exports.closeSounds = _1.closeSounds;
exports.GameData = _1.GameData;
exports.GameEngine = _1.GameEngine;
exports.Parser = _1.Parser;
exports.playSound = _1.playSound;
exports.RULE_DIRECTION = _1.RULE_DIRECTION;
const table_1 = require("./ui/table");
exports.TableUI = table_1.default;
class TableEngine {
    constructor(table) {
        this.tableUI = new table_1.default(table);
        this.timer = 0;
        this.currentLevel = 0;
    }
    setGame(source, levelNum) {
        this.tableUI.setGame(source);
        this.tableUI.setLevel(levelNum);
        this.currentLevel = levelNum;
    }
    start() {
        this.startKeyboardListener();
        this.startTickHandler();
    }
    stop() {
        cancelAnimationFrame(this.timer);
    }
    startKeyboardListener() {
        keymaster('up, w', () => this.tableUI.pressUp());
        keymaster('down, s', () => this.tableUI.pressDown());
        keymaster('left, a', () => this.tableUI.pressLeft());
        keymaster('right, d', () => this.tableUI.pressRight());
        keymaster('space, x', () => this.tableUI.pressAction());
        keymaster('z, u', () => this.tableUI.pressUndo());
        keymaster('r', () => this.tableUI.pressRestart());
    }
    startTickHandler() {
        const runLoop = () => __awaiter(this, void 0, void 0, function* () {
            while (this.tableUI.isCurrentLevelAMessage()) {
                alert(this.tableUI.getCurrentLevelMessage());
                this.currentLevel++;
                this.tableUI.setLevel(this.currentLevel);
            }
            const { 
            // changedCells,
            didLevelChange, didWinGame, messageToShow, soundToPlay
            // wasAgainTick
             } = this.tableUI.tick();
            if (soundToPlay) {
                // let sounds play while the game loads or player keeps moving
                /* await */ _1.playSound(soundToPlay); // tslint:disable-line:no-floating-promises
            }
            if (didWinGame) {
                alert(`You Won!`);
                cancelAnimationFrame(this.timer);
                return; // make sure we don't call window.requestAnimationFrame again
            }
            else if (didLevelChange) {
                this.currentLevel += 1;
                this.tableUI.setLevel(this.currentLevel);
                if (!this.tableUI.isCurrentLevelAMessage()) {
                    alert(`Congratulations! You completed the level.`);
                }
            }
            else if (messageToShow) {
                alert(messageToShow);
                this.tableUI.pressAction();
            }
            this.timer = window.requestAnimationFrame(runLoop);
        });
        this.timer = window.requestAnimationFrame(runLoop);
    }
}
exports.TableEngine = TableEngine;
//# sourceMappingURL=index-browser.js.map