import { Connector } from "./Connector";
import { EPageToHostCmd } from "./EPageToHostCmd";
import { IDriver } from "./IDriver";
export class DriverWin extends IDriver {
    sendToHost(cmd, dataObj, callback) {
        let tResBool;
        let tResNumb;
        let tResStr;
        switch (cmd) {
            case EPageToHostCmd.StorePageState:
                tResBool = window.external.storePageState(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd.RequestPageState:
                tResStr = window.external.getPageState();
                if (callback) {
                    callback(JSON.parse(tResStr));
                }
                break;
            case EPageToHostCmd.StoreSessionState:
                tResBool = window.external.storeSessionState(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd.AddSessionState:
                tResBool = window.external.addSessionState(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd.RequestSessionState:
                tResStr = window.external.getSessionState();
                if (callback) {
                    callback(JSON.parse(tResStr));
                }
                break;
            case EPageToHostCmd.NextPageUrl:
                if (typeof dataObj !== "string") {
                    dataObj = "";
                }
                tResBool = window.external.setNextPagePath(dataObj);
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd.LoadPageUrl:
                tResBool = false;
                if (typeof dataObj == "string") {
                    tResBool = window.external.loadPage(dataObj, false);
                }
                else if (typeof dataObj == "object") {
                    let 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.RequestCurrentPageUrl:
                tResStr = window.external.requestCurrentPageUrl();
                if (callback) {
                    callback(tResStr);
                }
                break;
            case EPageToHostCmd.SetProgress:
                tResNumb = window.external.setProgress(dataObj);
                if (callback) {
                    callback(tResNumb);
                }
                break;
            case EPageToHostCmd.RequestProgress:
                tResNumb = window.external.getProgress();
                if (callback) {
                    callback(tResNumb);
                }
                break;
            case EPageToHostCmd.RequestTextResources:
                tResStr = window.external.requestTextResources(dataObj);
                if (callback) {
                    callback(tResStr);
                }
                break;
            case EPageToHostCmd.RequestJsonFile:
                tResStr = window.external.requestJsonFile(dataObj);
                if (callback) {
                    callback(JSON.parse(tResStr));
                }
                break;
            case EPageToHostCmd.RequestImageFile:
                tResStr = window.external.requestImageFile(dataObj);
                if (callback) {
                    callback(tResStr);
                }
                break;
            case EPageToHostCmd.RequestTextFile:
                tResStr = window.external.requestTextFile(dataObj);
                if (callback) {
                    callback(tResStr);
                }
                break;
            case 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.FinishModuleSubmitData:
                tResBool = window.external.finishModuleSubmitData(JSON.stringify(dataObj));
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd.CancelModule:
                tResBool = window.external.cancelModule();
                if (callback) {
                    callback(tResBool);
                }
                break;
            case EPageToHostCmd.ShowMessageBox:
                tResNumb = window.external.showMessageBox(dataObj.type, dataObj.text);
                if (callback) {
                    callback(tResNumb);
                }
                break;
            default:
                throw new Error(Connector.NotImplementedException);
        }
    }
    detachListeners() {
        //
    }
}
