import { SdkDefault } from "./SdkDefault";


var rewardCallback = null;
export class SdkHuawei extends SdkDefault {

    private lastShowInterAdTime: number = 0;

    private interstitialAd = null;
    private rewardedVideoAd = null;
    private interstitialAdLoaded: boolean = false;

    /**
     * 开始游戏
     * @param cb 
     */
    startGame(cb?: Function): void {
        cc.log('SdkHuawei startGame.');

        qg.gameLoginWithReal({
            forceLogin: 1,
            appid: "115744405",
            success: (data) => {
                // 登录成功后，可以存储账号信息。             
                console.log(" game login with real success:" + JSON.stringify(data));
                cb && cb();

                this.getOAID();

                this.reLoadVideo();
            },
            fail: function (data, code) {
                console.log("game login with real fail:" + data + ", code:" + code);
                //根据状态码处理游戏的逻辑。
                //状态码为7004或者2012，表示玩家取消登录。
                //此时，建议返回游戏界面，可以让玩家重新进行登录操作。
                if (code == 7004 || code == 2012) {
                    console.log("玩家取消登录，返回游戏界面让玩家重新登录。");
                    cc.director.loadScene('logo');
                } else
                    //状态码为7021表示玩家取消实名认证。
                    //在中国大陆的情况下，此时需要禁止玩家进入游戏。
                    if (code == 7021) {
                        console.log("The player has canceled identity verification. Forbid the player from entering the game.")
                        cc.director.loadScene('logo');
                    } else {
                        cc.game.end();
                    }
            }
        });
    }

    getOAID() {
        "function" == typeof qg.getOAID && qg.getOAID({
            success: function (ret) {
                console.log("qg.getOAID success :" + JSON.stringify(ret));
            },
            fail: function (errMsg, errCode) {
                console.log("qg.getOAID fail " + errCode + ': ' + errMsg)
            },
            complete: function () {
                console.log("qg.getOAID complete")
            }
        })
    }

    //Ads
    showInterAd() {
        cc.log('show InterAd');
        // if (Date.now() - this.lastShowInterAdTime > 30000) {//最少间隔30秒
        //     this.lastShowInterAdTime = Date.now();


        // }

        this.loadInterAd();

    }

    loadInterAd() {
        if (this.interstitialAd) {
            this.interstitialAd.destroy();
        }
        this.interstitialAd = qg.createInterstitialAd({
            adUnitId:
            //    "g2tz5d4wkv" //测试ID
            "q5m5hlquus"//正式ID
        });
        this.interstitialAd.load({
            "channelExtras": {
                "customerChannel": "1"
            }
        });
        this.interstitialAd.onLoad((data) => {
            cc.log(
                "requestImageAdAndShow onLoad data= " + JSON.stringify(data)
            );
            this.interstitialAd.show();
        });
        this.interstitialAd.onError(err => {
            cc.log(" requestImageAdAndShow on error = " + JSON.stringify(err));
        });
        this.interstitialAd.onClose(() => {
            cc.log("requestImageAdAndShow interstitialAd closed");
            this.interstitialAd.destroy(() => {
                cc.log("destroy");
            });
        });
        this.interstitialAd.onClick(() => {
            cc.log("requestImageAdAndShow interstitialAd clicked");
        });
    }

    reLoadVideo() {
        this.rewardedVideoAd = qg.createRewardedVideoAd({
            adUnitId:
            //    "testx9dtjwj8hp" //测试ID
            "v1gtsb1ga8" //正式ID
            ,
            success: (code) => {
                cc.log("ad demo : loadAndShowVideoAd createRewardedVideoAd success");
            },
            fail: (data, code) => {
                cc.log("ad demo : loadAndShowVideoAd createRewardedVideoAd fail: " + data + "," + code);
            },
            complete: () => {
                cc.log("ad demo : loadAndShowVideoAd createRewardedVideoAd complete");
            }
        });
        this.rewardedVideoAd.onLoad(() => {
            cc.log('ad demo :ad loaded.');
        })

        this.rewardedVideoAd.onError((e) => {
            cc.error('load ad error:' + JSON.stringify(e));
            const errCode = e.errCode
            const errMsg = e.errMsg

            rewardCallback && rewardCallback(false);
        })
        this.rewardedVideoAd.onClose((res) => {
            // if(!this.rewardedVideoAd) return
            // this.rewardedVideoAd.offClose(null);
            cc.log('ad onClose: ' + res.isEnded)
            // this.rewardedVideoAd.offLoad(null);
            if (res && res.isEnded || res === undefined) {
                cc.log('播放激励视频结束，给予奖励');
                rewardCallback && rewardCallback(true);
            } else {
                cc.log('播放没结束，不给予奖励');
                rewardCallback && rewardCallback(false);
            }
        })
        this.rewardedVideoAd.load()
    }

    showVideoAd(cb?: (result: boolean) => void) {
        cc.log('show VideoAd');
        // cb && cb(true);

        rewardCallback = cb;
        this.rewardedVideoAd.show();
        this.rewardedVideoAd.load();
    }

    isPaymentReady(cb?: (result: boolean) => void) {
        cb && cb(false);
    }

}
