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

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

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

        duration = duration || 500;

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

        timer  = setInterval(function(){

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

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

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

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

WjySdk.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] = 'WjySdk.cbList[' + _fnId + ']';
        }

        param.push(arguments[i]);
    }

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

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

//已经反馈
WjySdk.openFeedback = function(){
    return this.callfn("openFeedback");
}

//设置up主抽奖通知栏
WjySdk.openNotificationSettingIfNeeded = function(msg){
    return this.callfn("openNotificationSettingIfNeeded",msg);
}


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


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

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

WjySdk.openUrl = function(url, title, needTopBar) {
    title = title || '';
    needTopBar = needTopBar !== null ? needTopBar : true;
    return this.callfn("openUrl", url, title, needTopBar);
}

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

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

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

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

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

WjySdk.getSupportShareType = function(callback) {
    return this.callfn("getSupportShareType", callback);
}

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

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

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

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

WjySdk.showErrorTip = function(txt, timeout) {
    if (window.Ouj && Ouj.showErrortip) {
        OujSdk.showTip(txt, timeout);
    } else {
        return this.callfn("showErrortip", txt, timeout);
    }
}

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

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

WjySdk.hideTopBar = function(flag) {
    return this.callfn("hideTopBar",flag);
}

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

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

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

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

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

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

//预告片
WjySdk.toVideoPreview = function(articleId) {
    return this.openUrl("glance://toVideoPreview?articleId="+articleId)
}

//解说视频
WjySdk.toCommentaryDetail = function(commentaryId) {
    return this.openUrl("glance://toCommentaryDetail?commentaryId="+commentaryId)
}

//电影详情
WjySdk.toVideoDetail = function(articleId) {
    return this.openUrl("glance://toVideoDetail?articleId="+articleId)
}

export default WjySdk;