
import { zgSingleton } from "../../zgSingleton";
import ATSDKMgr from "./ATSDKMgr";

/**
 * inter插屏广告
 * @description
 * @author 冯聪
 */
export default class ATInterstitial extends zgSingleton {

    private ATInterstitialJS: any;

    init() {
        this.ATInterstitialJS = cc["ATInterstitialJS"];
        this.ATInterstitialJS.proxy = this;;
        this.ATInterstitialJS.init();
        // this.loadNative();
        this.loadInter(0);
    }

    /** 广告位id */
    placementID(type: number) {
        if (type == 0) {
            if (cc.sys.os === cc.sys.OS_IOS) {
                return ATSDKMgr.topon_inter_placementId;
            } else if (cc.sys.os === cc.sys.OS_ANDROID) {
                return ATSDKMgr.topon_inter_placementId;
            }
        }
        else {
            if (cc.sys.os === cc.sys.OS_IOS) {
                return ATSDKMgr.topon_fullinter_placementId;
            } else if (cc.sys.os === cc.sys.OS_ANDROID) {
                return ATSDKMgr.topon_fullinter_placementId;
            }
        }
    }

    /** 插屏广告
     * @param type 0 普通插屏 1 全屏视频插屏
     */
    loadInter(type) {
        // // console.log('99999999   loadInterPre  ', type)
        // if (!this.onloadInter) {
        //     this.onloadInter = true
        //     // console.log('99999999   loadInter  ', type)
        this.ATInterstitialJS.loadInter(type);

        //     //FC:UROI事件
        //     if (GameM.commonData.ifUROI) {
        //         let eventID = EncryptUtil.randomString(32);
        //         AdM.sendUROIEvent(eventID, '', GameM.commonData.interPlaceId, "ad_request", "interstitial", 'other', '')
        //     }
        // }
    }

    showInter(type) {
        // this.interType = type
        // if (cc["thirdVideo"].checkInterReady(type)) {
        //     // console.log('99999999   showInter success')
        //     cc["thirdVideo"].showInter(type);
        // }
        // else {
        //     // console.log('99999999   showInter fail')
        //     this.loadInter(type)
        // }
        this.ATInterstitialJS.showInter(0);
    }

    //广告回调部分------------------------------------------------------------------------
    onInterstitialAdLoaded(placementId) {
        // this.onloadInter = false
        // // console.log('99999999   onInterstitialAdLoaded  ')
        // if (GameM.commonData.ifUROI) {
        //     let eventID = EncryptUtil.randomString(32);
        //     AdM.sendUROIEvent(eventID, '', placementId, "ad_fill", "interstitial", 'other', '')
        // }
    }

    onInterstitialAdLoadFail(placementId, errorInfo) {
        // this.onloadInter = false
        // // console.log('99999999   onInterstitialAdLoadFail')

        // if (placementId == GameM.commonData.fullInterPlaceId) {
        //     EffectNode.instance.PlayTip('拉取全屏视频失败，请稍后再试')
        //     GameM.commonData.updateADLog(10, placementId)
        // }
        // else if (placementId == GameM.commonData.interPlaceId) {
        //     GameM.commonData.updateADLog(11, placementId)
        // }
    }

    //广告展示成功
    onInterstitialAdShow(placementId, callbackInfo) {
        // // console.log('99999999   onInterstitialAdShow')
        // let data = JSON.parse(callbackInfo)
        // let id = 2
        // if (placementId == GameM.commonData.interPlaceId) {
        //     id = 2
        // }
        // else if (placementId == GameM.commonData.fullInterPlaceId) {
        //     id = 9
        // }
        // GameM.commonData.updateADLog(id, placementId, data.adunit_format, data.network_type, data.network_placement_id,
        //     data.network_firm_id, data.adsource_id, data.adsource_index, data.adsource_price,
        //     data.adsource_isheaderbidding, data.publisher_revenue, data.precision, data.ecpm_level)

        // if (GameM.commonData.ifUROI) {
        //     let eventID = EncryptUtil.randomString(32);
        //     let adAdn = this.getAdAdn(data.network_firm_id)
        //     let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
        //     AdM.sendUROIEvent(eventID, data.id, placementId, "ad_show", "interstitial", adAdn, price)
        // }
    }

    onInterstitialAdClick(placementId, callbackInfo) {
        // let data = JSON.parse(callbackInfo)

        // if (GameM.commonData.ifUROI) {
        //     let eventID = EncryptUtil.randomString(32);
        //     let adAdn = this.getAdAdn(data.network_firm_id)
        //     let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
        //     AdM.sendUROIEvent(eventID, data.id, placementId, "ad_click", "interstitial", adAdn, price)
        // }
    }

    //插屏广告关闭
    onInterstitialAdClose(placementId, callbackInfo) {
        // //给奖励
        // // console.log('AnyThinkInterstitialDemo  ', this.interType)
        // if (placementId == GameM.commonData.fullInterPlaceId) {
        //     GameM.commonData.showFull = false
        //     GameM.commonData.todayFullInterTimes++
        //     GameM.globalStorage.setStorage(STORAGE_KEY.todayFullInterTimes, GameM.commonData.todayFullInterTimes)
        //     AdM.videoReward()

        //     let data = JSON.parse(callbackInfo)
        //     GameM.commonData.updateADLog(8, placementId, data.adunit_format, data.network_type, data.network_placement_id,
        //         data.network_firm_id, data.adsource_id, data.adsource_index, data.adsource_price,
        //         data.adsource_isheaderbidding, data.publisher_revenue, data.precision, data.ecpm_level)
        // }

        // this.loadInter(this.interType)
    }

}
