window.onResume = document.createEvent('Event');
window.onResume.initEvent('onResume', true, true);

var extSdk = window.extSdk = {
    fnId : 0,
    cbList : [],
}

/**
 * 定时执行方法，防止ios里的延迟
 */
extSdk.run = function(funName,args,duration){
    var self = this,timer;

        duration = duration || 500;

        self[funName].apply(extSdk,args);

        timer  = setInterval(function(){

             if(typeof window.HostApp != "undefined"){
                 clearInterval(timer);
             }

             self[funName].apply(extSdk,args);
        },duration);
}

extSdk.run2 = function(funName, args, duration){
    var self = this;
    var duration = duration || 500;
    var timer;
    var times = 0; //最大尝试次数
    if(typeof window.HostApp != "undefined") {
        self[funName].apply(extSdk, args);
    } else {
        timer = setInterval(function(){
            //最多尝试5次
            if(typeof window.HostApp != "undefined" || times == 5){
                self[funName].apply(extSdk, args);
                clearInterval(timer);
            } else {
                times += 1;
            }
        }, duration)
    }
}

/**
 * 检查注入状态
 * @param  {String} name 方法名
 */
extSdk.checkInject = function(name) {
    if (typeof window.HostApp != "undefined") {
        if(typeof window.HostApp[name] == "undefined") {
            console.log('没有' + name + '这个方法')
            return false;
        } else {
            return true;
        }
    } else {
        console.log("js方法未注入！name:" + name)
        return false;
    }
}

extSdk.callfn = function() {
    var _fnId = this.fnId++;
    var name = arguments[0];
    var param = [];

    for(var i = 1; i < arguments.length; i++) {
        if(typeof arguments[i] == "function") {
            this.cbList[_fnId] = arguments[i];
            arguments[i] = 'extSdk.cbList[' + _fnId + ']';
        }

        param.push(arguments[i]);
    }

    if(this.checkInject(name)) {
        // console.log('HostApp[' + name + '](' + param.join(',') + ')');
        window.HostApp[name].apply(window.HostApp, param);
        return true;
    } else {
        // console.error('HostApp[' + name + '] is not exist.');
        return false;
    }
}

extSdk.appBridge = function(uri) {
    var iframe = document.createElement("IFRAME");
    iframe.setAttribute('src', uri);
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;
}

extSdk.openLogin = function() {
    return this.callfn("openLogin");
}

extSdk.getUserInfo = function(callback) {
    return this.callfn("getUserInfo", callback);
}

extSdk.getDeviceInfo = function(callback) {
    return this.callfn("getDeviceInfo", callback);
}

extSdk.openUrl = function(url, title, needTopBar = true, hideMenuButton = false) {
    title = title || '';
    return this.callfn("openUrl", url, title, needTopBar, hideMenuButton);
}

extSdk.copy = function(txt) {
    return this.callfn("copy", txt);
}

extSdk.historyBack = function() {
    return this.callfn("historyBack");
}

extSdk.closeWebview = function() {
    return this.callfn("closeWebview");
}

extSdk.setTitle = function(title) {
    return this.callfn("setTitle", title);
}

extSdk.getNetworkType = function(callback) {
    return this.callfn("getNetworkType", callback);
}

extSdk.shareMessage = function(title, link, desc, imgUrl) {
    return this.callfn("shareMessage", title, link, desc, imgUrl);
}

extSdk.showLoading = function(txt) {
    txt = txt || '正在加载中...';
    return this.callfn("showLoading", txt);
}

extSdk.hideLoading = function() {
    return this.callfn("hideLoading");
}

extSdk.showTip = function(txt, timeout) {
    return this.callfn("showTip", txt, timeout);
}

extSdk.showDialog = function(txt) {
    return this.callfn("showDialog", txt);
}

extSdk.confirm = function(txt, callback, title, buttonLables) {
    title = title || '提醒';
    buttonLables = buttonLables || '确定,取消';
    return this.callfn("confirm", title, txt, callback, buttonLables);
}

extSdk.hideMenuButton = function() {
    return this.callfn("hideMenuButton");
}

extSdk.hideShare = function() {
    return this.callfn("hideShare");
}

extSdk.aliPay = function(platformData, callback) {
    return this.callfn("aliPay", platformData, callback);
}

extSdk.wxPay = function(platformData, callback) {
    if (typeof platformData == 'object') {
        platformData = JSON.stringify(platformData);
    }
    return this.callfn("wxPay", platformData, callback);
}

extSdk.setShareInfo = function(title, link, desc, imgUrl) {
    return this.callfn("setShareInfo", title, link, desc, imgUrl);
}

// extSdk.toFeedback = function() {
//     return this.appBridge("HostApp://toFeedback");
// }

// extSdk.toIndex = function() {
//     return this.appBridge("glance://toIndex");
// }

// extSdk.toCommentaryDetail = function(commentaryId) {
//     return this.appBridge("glance://toCommentaryDetail?commentaryId="+commentaryId);
// }

// extSdk.toVideoDetail = function(articleId) {
//     return this.appBridge("glance://toVideoDetail?articleId="+articleId);
// }

// extSdk.toFilmCommentDetail = function(filmCommentId) {
//     return this.appBridge("glance://toFilmCommentDetail?filmCommentId="+filmCommentId);
// }

// extSdk.toPostDetail = function(postId) {
//     return this.appBridge("glance://toPostDetail?postId="+postId);
// }

export default extSdk;