function showData(adData) {
    // 1/2区域点击
    $(".canclick-panle").click((e) => {
            // 链接类型：1 内部游戏(跳转详情页)  2 外部链接 3 打开客户端  4 游戏标签  5 会员订购  6 wiki 7试玩
            var index = (($(e.target).parent().index() - 1) < 0 ? adData.length - 1 : ($(e.target).parent().index() - 1) >= adData.length ? 0 : ($(e.target).parent().index() - 1))
            var param = {}
            param = adData[index].urlType == 3 ? {} : {
                    "redirectType": adData[index].urlType == 4 ? 2 : adData[index].urlType,
                    "redirectContent": adData[index].urlType != 5 ? adData[index].adsUrl : 5,
                    "redirectName": adData[index].urlType != 5 ? adData[index].adsUrl : 5
                }
                // 2 外部链接
            if (adData[index].urlType == null) {
                return false
            } else if (adData[index].urlType == 2) { //2 外部链接
                Client.openUrl(adData[index].adsUrl)
            } else if (adData[index].urlType == 3) { //3 打开客户端 
                Client.runJS("")
            } else if (adData[index].urlType == 7) { //试玩
                let url = is_test ? 'http://pctest.migufun.com:8091/gameplay/pcwegameplay/?gameId=' : 'https://pc.migufun.com/gameplay/pcwegameplay/?gameId='
                Client.openUrl(url + adData[index].adsUrl + '&from=client' + (headers.appChannel ? ('&channel=' + headers.appChannel) : ''))
            } else {
                Client.runJS(`pushLinkInfo('${JSON.stringify(param)}')`)
            }
            var recordData = {
                'eventType': 'screen_1',
                'rese8': '点击开屏广告',
                'pageName': '大弹框',
                'url': adData[index].urlType == 3 ? '打开客户端' : (adData[index].urlType == 5 ? "开通会员" : adData[index].adsUrl),
            }
            recordEventAction(recordData);
        })
        // 按钮点击事件
    $(".openclient").click((e) => {
        var recordData = {
            'eventType': 'screen_3',
            'rese8': '点击开屏浮层广告',
            'pageName': '广告浮层',
        }
        recordEventAction(recordData);
        Client.runJS("")
    })
}
// 关闭
function closeWindow() {
    $(".colse-btn").click((e) => {
        var recordData = {
            'eventType': 'screen_2',
            'rese8': '关闭开屏广告',
            'pageName': '大弹框',
        }
        recordEventAction(recordData);
        setTimeout(() => {
            Client.closeWindow();
        }, 500);
    })
}

function createDom(reg, data, index) {
    let inshwofloatinglayer = (data.popupName != null && data.popupButtonName != null) ? true : false
    if (data.adsPicture.match(reg)) {
        $(".swiper-wrapper").append(`
        <div class="swiper-slide">
            <img class="swiper-img" src=${data.adsPicture}>
            <span class='canclick-panle ${data.urlType? 'cancursor' :''}'></span>
            ${inshwofloatinglayer
            ?`<p class="ad-floating-layer" targetIndex=${index}>
                <i>${data.popupName}</i>
                <b class="openclient">${data.popupButtonName}</b>
            </p> `  
            :''}
            
        </div>`)
    } else {
        $(".swiper-wrapper").append(`
        <div class="swiper-slide">
            <video autoplay muted loop src=${data.adsPicture}></video>
            <span class='canclick-panle ${data.urlType? 'cancursor' :''}'></span>
            ${inshwofloatinglayer
            ?`<p class="ad-floating-layer" targetIndex=${index}>
                <i>${data.popupName}</i>
                <b class="openclient">${data.popupButtonName}</b>
            </p> `  
            :''}
        </div>`)
    }
}

window.onload = function() {
    // document.onkeydown = function(e) {
    //         var key = window.event.keyCode
    //         if (key == 116) {
    //             location.reload()
    //         }
    //     }
    // 获取用户信息成功后调用广告接口
    getHeaderInfo().then(isGetUserId => {
        bootAdsInfo().then((res) => {
            if (res.returnCode === '000000') {
                var reg = /\.(jpeg|jpg|gif|png)$/
                adData = res.resultData
                adData.map((item, index) => {
                    createDom(reg,item,index)
                    $(".adTitle").html(item.adsTitle ? item.adsTitle : '')
                })
                if (res) {
                    new Swiper('.swiper-container', {
                        loop: adData.length > 1,
                        autoplay: {
                            delay: 8000,
                            stopOnLastSlide: false,
                            disableOnInteraction: true,
                        },
                        spaceBetween: 1,
                        on: {
                            slideChange: function(e) {
                                var index = this.activeIndex > adData.length || this.activeIndex < 1 ? 0 : this.activeIndex - 1
                                $(".adTitle").html(adData[index].adsTitle ? adData[index].adsTitle : '')
                                if (adData.length > 1) {
                                    // 链接类型：1 内部游戏  2 外部链接 3 打开客户端  4 游戏标签   5 会员订购  6 wiki  7 试玩游戏
                                    var recordData = {
                                        'eventType': 'screen_0',
                                        'rese8': '曝光开屏广告',
                                        'pageName': '大弹框',
                                        'rese3': adData[index].id,
                                        'url': adData[index].urlType == 3 ? '打开客户端' : (adData[index].urlType == 5 ? "开通会员" : adData[index].adsUrl),
                                    }
                                    recordEventAction(recordData);
                                }
                            }
                        }
                    });
                    if (adData.length == 1) {
                        var recordData = {
                            'eventType': 'screen_0',
                            'rese8': '曝光开屏广告',
                            'pageName': '大弹框',
                            'rese3': adData[0].id,
                            'url': adData[0].urlType == 3 ? '打开客户端' : (adData[0].urlType == 5 ? "开通会员" : adData[0].adsUrl),
                        }
                        recordEventAction(recordData);
                    }
                }
                showData(adData);
                closeWindow();
            }
        }).catch((err) => {
            console.log("error", err);
        })
    })
}