import { _decorator, Component, Node } from 'cc';
import { WECHAT } from 'cc/env';
import { Global } from '../Global';
const { ccclass, property } = _decorator;

@ccclass('WxPlatform')
export class WxPlatform {
    static _ins: WxPlatform;
    public wx = window["wx"];

    /**
     * 
     */
    static get ins() {
        if (!this._ins) {
            this._ins = new WxPlatform();
        }
        return this._ins;
    }

    wechat_setting() {
        console.log("1 执行 wechat_setting",WECHAT);
        if (!WECHAT) {
            return;
        }
        console.log("2 执行 wechat_setting");
        this.wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        });
        //upgrade 
        const updateManager = this.wx.getUpdateManager()
        updateManager.onCheckForUpdate(function (res) {
            console.log("请求完新版本信息的回调", res.hasUpdate);
        })
        updateManager.onUpdateReady(function () {
            this.wx.showModal({
                title: '更新提示',
                content: '新版本已经准备好，是否重启应用？',
                success(res) {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate()
                    }
                }
            })
        })
        updateManager.onUpdateFailed(function () {
            // 新版本下载失败
            console.log("新版本下载失败")
        })
    }

    /**
     * 
     * @param delay 
     * @returns 
     */
    show_interstitial(delay = 0) {
        console.log("显示差评广告》〉》〉》〉》〉》〉》〉》");
        if (!WECHAT) {
            return;
        }
        // let id = Main.ins.Platforms[0].intersitialId[0];
        let ad_video_id = Global.ad_intersitial_id;
        let interstitialAd = this.wx.createInterstitialAd({
            adUnitId: ad_video_id.trim(),
        });
        if (interstitialAd) {
            interstitialAd.load().then(() => {
                console.log('插屏 广告加载成功');
            });
            interstitialAd.onClose(res => {
                console.log('关闭插屏广告', res);
            });
            interstitialAd.onError(err => {
                console.log('err:插屏广告加载失败', err);
            });

            setTimeout(() => {
                interstitialAd.show().catch((err) => {
                    console.log('插屏广告展示失败', err);
                });
            }, delay * 1000);
        }
    }

    rewardVideo2: any;
    /**
     * 
     * @param callback 
     */
    show_reward_video(callback: Function) {
        let my = this;
        let ad_video_id = Global.ad_video_id;
        // let id = Main.ins.Platforms[0].videoId[0];
        if (my.rewardVideo2 != null) {
            my.rewardVideo2.offClose(fun);
        }
        let rewardedVideoAd = this.wx.createRewardedVideoAd({
            adUnitId: ad_video_id.trim(),
        });
        my.rewardVideo2 = rewardedVideoAd;
        rewardedVideoAd.load().then(() => {
            this.wx.showToast({
                title: "加载中，请稍后",
                icon: 'success',//图标，支持"success"、"loading" 
                duration: 1500,//提示的延迟时间，单位毫秒，默认：1500 
                mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false 
                success: function () { },
                fail: function () { },
                complete: function () { }

            })
            console.log('激励视频 广告加载成功');
            rewardedVideoAd.show();
        });
        rewardedVideoAd.onError(err => {
            console.log('激励视频 广告显示失败', err);
            this.wx.showToast({
                title: "请稍后再试",
                icon: 'fail',//图标，支持"success"、"loading" 
                duration: 1500,//提示的延迟时间，单位毫秒，默认：1500 
                mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false 
                success: function () { },
                fail: function () { },
                complete: function () { }
            })
            callback(2);
        })
        var fun = function (res) {
            if (res && res.isEnded) {
                console.log('res:  ', res);
                callback(1);
                rewardedVideoAd.offClose(fun);
            } else {
                console.log('播放中途退出');
                callback(0);
            }
        }
        rewardedVideoAd.onClose(fun);
    }


}

