"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Connector_1 = require("./Connector");
var EPageToHostCmd_1 = require("./EPageToHostCmd");
var IDriver_1 = require("./IDriver");
var DriverWin = /** @class */ (function (_super) {
    tslib_1.__extends(DriverWin, _super);
    function DriverWin() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    DriverWin.prototype.sendToHost = function (cmd, dataObj, callback) {
        var tResBool;
        var tResNumb;
        var tResStr;
        switch (cmd) {
            case EPageToHostCmd_1.EPageToHostCmd.StorePageState:
                tResBool = window.external.storePageState(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestPageState:
                tResStr = window.external.getPageState();
                if (callback) {
                    callback(JSON.parse(tResStr));
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.StoreSessionState:
                tResBool = window.external.storeSessionState(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.AddSessionState:
                tResBool = window.external.addSessionState(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestSessionState:
                tResStr = window.external.getSessionState();
                if (callback) {
                    callback(JSON.parse(tResStr));
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.NextPageUrl:
                if (typeof dataObj !== "string") {
                    dataObj = "";
                }
                tResBool = window.external.setNextPagePath(dataObj);
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.LoadPageUrl:
                tResBool = false;
                if (typeof dataObj == "string") {
                    tResBool = window.external.loadPage(dataObj, false);
                }
                else if (typeof dataObj == "object") {
                    var tRBH = false;
                    if (dataObj.hasOwnProperty("rollBackHistory")) {
                        tRBH = dataObj.rollBackHistory === true;
                    }
                    if (dataObj.hasOwnProperty("url")) {
                        tResBool = window.external.loadPage(dataObj.url, tRBH);
                    }
                    if (dataObj.hasOwnProperty("newProgress")) {
                        window.external.setProgress(dataObj.newProgress);
                    }
                }
                if (callback && tResBool === false) {
                    // no need to return true because the context has been destroyed anyway
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestCurrentPageUrl:
                tResStr = window.external.requestCurrentPageUrl();
                if (callback) {
                    callback(tResStr);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.SetProgress:
                tResNumb = window.external.setProgress(dataObj);
                if (callback) {
                    callback(tResNumb);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestProgress:
                tResNumb = window.external.getProgress();
                if (callback) {
                    callback(tResNumb);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestTextResources:
                tResStr = window.external.requestTextResources(dataObj);
                if (callback) {
                    callback(tResStr);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestJsonFile:
                tResStr = window.external.requestJsonFile(dataObj);
                if (callback) {
                    callback(JSON.parse(tResStr));
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestImageFile:
                tResStr = window.external.requestImageFile(dataObj);
                if (callback) {
                    callback(tResStr);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestTextFile:
                tResStr = window.external.requestTextFile(dataObj);
                if (callback) {
                    callback(tResStr);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.RequestJsonFileLocalized:
                tResStr = window.external.requestJsonFileLocalized(dataObj);
                if (callback) {
                    callback(tResStr);
                }
                break;
            /* usage unclear
            case EPageToHostCmd.ValidateUserCredentials:
                tResBool = ((window.external as any) as External).validateUserCredentials(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
                */
            case EPageToHostCmd_1.EPageToHostCmd.FinishModuleSubmitData:
                tResBool = window.external.finishModuleSubmitData(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.CancelModule:
                tResBool = window.external.cancelModule();
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd_1.EPageToHostCmd.ShowMessageBox:
                tResNumb = window.external.showMessageBox(dataObj.type, dataObj.text);
                if (callback) {
                    callback(tResNumb);
                }
                break;
            default:
                throw new Error(Connector_1.Connector.NotImplementedException);
        }
    };
    DriverWin.prototype.detachListeners = function () {
        //
    };
    return DriverWin;
}(IDriver_1.IDriver));
exports.DriverWin = DriverWin;
