
var ua = navigator.userAgent.toUpperCase();
var App = {}
var isAndroid = App.isAndroid = ua.indexOf("ANDROID") != -1;
var isIos = App.isIos = ua.indexOf("IPHONE OS") != -1;
var _isHrxAppIos = ua.indexOf("HRXAPP") != -1;
function callApp(name) {
    var args = Array.prototype.slice.call(arguments, 1);
    var item = null;
    // 遍历参数
    for(var i = 0, len = args.length; i< len; i++) {
        item = args[i]
        if(item === "undefinded") {
            item = '';
        }
        // 如果参数是一个Function类型，则将Function存储到window对象，并将函数名传递给Native
        if(typeof item == 'function') {
            var callbackName = '__whr_' + name + '_callback_' + i + '_' + callbackIndex++;
            var functtt = item;
            window[callbackName] = item;
            item = callbackName;
        }

        args[i] = item;
    }

    if(window.zzapi) {
        callApZZ(name, args)
    } else {
        if(_isHrxAppIos) {
            if(newAppFlag) {
                callAppMethods(name, args)
            } else {
                callAppKLPA(name,args);
            }
        } else {
            console.log('无window.zzapi对象')
        }
    }
}

App.getUser = function(callbackFun) {
    let str = {
        employeeId: 'employeeId',
        umId: 'umId',
        token: 'token',
        name: '张三',
        headerUrl: 'headerUrl',
        enviroment: 'enviroment'
    }

    if(localStorage["accessToken"]) {
        str = {
            ...str,
            umId: localStorage["umId"],
            token: localStorage["accessToken"]
        }
    }

    var params = (typeof str == "string") ? (JSON.parse(str)) : str;

    callbackFun(params)
}

// 改变title
App.changeTitle = function(titleName) {
    document.title = titleName;
    callApp("changeTitle", titleName)
}

App.showLoading = function() {
    console.info("App.showLoading")
}

App.scrollToTop = function() {
    console.info("App.scrollToTop")
}

App.hideLoading = function() {
    console.info("App.hideLoading")
}

App.back = function() {
    console.info("App.back")
}

App.backToHome = function() {
    console.info("phone backToHome")
}

App.gotoLocation = function(url, paramStr) {
    if(paramStr) {
        window.location.href = url + '?' + paramStr
    } else {
        window.location.href = url
    }
}
// 跳转本地包路径
App.gotoLocalHref = function() {
    console.info("phone gotoLocalHref")
}

// 跳转第三方页面，native会默认控制返回
App.gotoAdHref = function() {
    console.info("phone gotoLocalHref")
}


App.showAlert = function(title) {
    console.info("showAlert.title:" + title)
}

App.changeHeader = function(config) {
    console.info("changeHeader" + JSON.stringify(config))
}


/**
 * 打开H5页面，
 * @param {*} url 页面路径
 * @param {*} withBackButton 0：无， 1：左下角有个原型返回键
 * @param {*} [options] h5传给native的配置参数
 * @param {*} [options.animation] 转场动画，例如bottom-top
 * @param {*} [options.param] 传到下一个页面的参数
 */
App.pushH5Window = function(url, withBackButton, options) {
    options = formatOptions(options);
    callApp('pushH5Window', url, withBackButton, JSON.stringify(options))
}
/**
 * 关闭当前H5页面，即返回到上一个页面
 * @param {*} animation 
 * @param {*} param 
 */
App.popH5Window = function(animation, param) {
    callApp('popH5Window', animation, param)
}

/**
 * 横屏
 */
App.landSpace = function(callback) {
    callApp('landSpace');
    callback && callback()
}
/**
 * 强制横屏
 */
App.forceLandSpace = function(callback) {
    callApp('landSpace');
    callback && callback()
}
/**
 * 竖屏操作
 */
App.portrait = function(callback) {
    callApp('portrait');
    callback && callback()
}

/**
 * 获取状态栏高度
 * @param {*} options 配置对象或回调函数
 * @param {function} options.success 成功回调
 */
App.getStatusBarHeight = function(options) {
    // if (typeof options === 'function') {
    //     options(44)
    // } else if (options && typeof options.success === 'function') {
    //     options.success(44)
    // }
    callApp('getStatusBarHeight', callbackfun);
};

export default App