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

/**
 * 激励视频广告
 * @description
 * @author 冯聪
 */
export class ATRewardVideo extends zgSingleton {

    /** 激励视频状态 */
    public reawardAdState: RewardAdLoadState = 0;
    /** 是否加载完显示 */
    public ifLoadShow: boolean = false;
    /**是否观看完毕 */
    public ifWatchEnd: boolean = false;

    init() {

        cc["ATRewardVideoJS"].proxy = this;
        cc["ATRewardVideoJS"].init();

        this.ifWatchEnd = false;
        if (this.reawardAdState == RewardAdLoadState.Unload) {
            this.loadAd();
        }
    }

    /** 广告位id */
    placementID() {
        if (cc.sys.os === cc.sys.OS_IOS) {
            return ATSDKMgr.topon_rewardVideo_placementId;
        } else if (cc.sys.os === cc.sys.OS_ANDROID) {
            return ATSDKMgr.topon_rewardVideo_placementId;
        }
    }

    /** 加载广告 */
    loadAd() {
        console.log("anythink === 加载激励视频广告", RewardAdLoadState[this.reawardAdState]);
        if (this.reawardAdState == RewardAdLoadState.Loading) {
            return;
        }
        this.reawardAdState = RewardAdLoadState.Loading;
        cc["ATRewardVideoJS"].loadAd("userIdKey", "userDataKey");
    }

    /** 显示广告 */
    showAd() {
        if (this.reawardAdState != RewardAdLoadState.LoadSuccess) {
            this.loadAd();
            this.ifLoadShow = true;
            return;
        }

        cc["ATRewardVideoJS"].showAd();
    }

    /** 检测是否准备完毕 */
    checkReady() {
        return cc["ATRewardVideoJS"].checkReady();
    }

    /**
     * 激励视频加载完毕
     * @param placementId 广告位id
     */
    onRewardedVideoAdLoaded(placementId: string) {
        // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdLoaded(" + placementId + ")");
        // ATRewardVideo.getInstance().showAd()
        console.log("anythink === 加载激励视频广告成功", this.checkReady(), RewardAdLoadState[this.reawardAdState])
        this.reawardAdState = RewardAdLoadState.LoadSuccess;
        if (this.ifLoadShow) {
            this.showAd();
            this.ifLoadShow = false;
        }
    }

    /**
     * 激励视频加载失败
     * @param placementId 广告位id
     * @param errorInfo   错误信息
     */
    onRewardedVideoAdFailed(placementId, errorInfo) {
        // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdFailed(" + placementId + ", " + errorInfo + ")");
        console.log("anythink === 加载激励视频广告失败", RewardAdLoadState[this.reawardAdState]);
        this.reawardAdState = RewardAdLoadState.LoadFail;
    }

    /**
     * 激励视频开始播放
     * @param placementId  广告位id
     * @param callbackInfo 回调信息
     */
    onRewardedVideoAdPlayStart(placementId, callbackInfo) {

        //ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayStart(" + placementId + ", " + callbackInfo + ")");

        //视频埋点开始
        let data = JSON.parse(callbackInfo);
        // HttpMgr.Inst.updateADLog(4, 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);
        gData.adData.updateData(placementId, data);

        this.reawardAdState = RewardAdLoadState.Unload;
        //点击关闭的时候重新加载（亲测，即使在start这边 加载 也是在点击关闭时候才返回成功）
        //this.loadAd();
    }

    /**
     * 激励视频播放完毕
     * @param placementId  广告位id
     * @param callbackInfo 回调信息
     */
    onRewardedVideoAdPlayEnd(placementId, callbackInfo) {
        //ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayEnd(" + placementId + ", " + callbackInfo + ")");
        this.ifWatchEnd = true;
    }

    /**
     * 激励视频播放失败
     * @param placementId  广告位id 
     * @param errorInfo    错误信息
     * @param callbackInfo 回调信息
     */
    onRewardedVideoAdPlayFailed(placementId, errorInfo, callbackInfo) {

        //ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayFailed(" + placementId + ", " + errorInfo + ", " + callbackInfo + ")");

        // 2021/06/03（勿删）此处为星云埋点,先注释，待接口调通，恢复
        // let data = JSON.parse(callbackInfo)
        // HttpMgr.Inst.updateADLog(5, 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);

        ATSDKMgr.getInstance().watchVideoAdCallBack(AdPlayStateType.PlayFail);
    }

    /**
     * 激励视频点击关闭
     * @param placementId  广告位id
     * @param callbackInfo 回调信息
     */
    onRewardedVideoAdClosed(placementId, callbackInfo) {

        // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdClosed(" + placementId + ", " + callbackInfo + ")");

        if (this.ifWatchEnd) {
            // 2021/06/03（勿删）此处为星云埋点,先注释，待接口调通，恢复
            // 视频埋点结束
            // let data = JSON.parse(callbackInfo)
            // HttpMgr.Inst.updateVideoTimes(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);

            ATSDKMgr.getInstance().watchVideoAdCallBack(AdPlayStateType.WatchEnd);
        }
        else {
            ATSDKMgr.getInstance().watchVideoAdCallBack(AdPlayStateType.Cancel);
        }

        this.ifWatchEnd = false;
        
        this.loadAd();
    }

    /**
     * 激励视频被点击
     * @param placementId 
     * @param callbackInfo 
     */
    onRewardedVideoAdPlayClicked(placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayClicked(" + placementId + ", " + callbackInfo + ")");
    }

    /**
     * 能获取奖励
     * @description 引擎的自动判断
     * @param placementId  广告位id
     * @param callbackInfo 回调信息
     */
    onReward(placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkRewardedVideoDemo::onReward(" + placementId + ", " + callbackInfo + ")");
        this.ifWatchEnd = true;
    }
}

/** 激励视频广告加载状态 */
export enum RewardAdLoadState {
    /** 未加载 */
    Unload = 0,
    /** 加载中 */
    Loading = 1,
    /** 加载成功 */
    LoadSuccess = 2,
    /** 加载失败 */
    LoadFail = 3,
    /** 播放结束 */
    PlayEnd = 4
}