import { EType } from "../../tyq3.x/Platform/wechat/WXCustomAd";
import SdkMgr from "../../tyq3.x/SdkMgr";
import TyqEventMgr from "../../tyq3.x/tyq-event-mgr";
import { tyqSDK } from "../../tyq3.x/tyq-sdk";
import { cocosUtil } from "../../utils/cocosUtil";
import { localText } from "../data/localText";
import { msgac } from "../data/msgac";
import { eventManager } from "./eventManager";


export class sdkManager {

    private static _instance: sdkManager;
    private constructor() { }
    public static get instance(): sdkManager {
        if (!this._instance) {
            this._instance = new sdkManager();
        }
        return this._instance;
    }

    private levelCount: number = 0;

    /**
     * 激励视频广告统一播放接口
     * 完整观看广告后，获得奖励，调用：tmpFunc(1)
     * 中途退出或者观看失败，调用：tmpFunc(0)
     * @param cb 
     */
    openAd(cb: Function) {
        let tmpFunc = (st: number) => {
            if (st == 0) {
                eventManager.instance.send(msgac.showNotice, localText.textObj.adAwardHint);
            } else if (st == -1) {
                eventManager.instance.send(msgac.showNotice, localText.textObj.noRewardAd);
            }
            cb(st);
        };

        if (cocosUtil.isDesktopBrowser()) {
            // pc web默认直接成功
            tmpFunc(1);
            return;
        }

        this.showRewardAd(tmpFunc);
    }

    init(cb?: Function) {
        tyqSDK.init(() => {
            tyqSDK.login();
            SdkMgr.ins.preLoadAd();
            if (SdkMgr.ins.isWechat()) {
                wx.showShareMenu();
            }
            if (cb) {
                cb();
            }
        });
    }

    addLevelCount() {
        this.levelCount++;
        let val = tyqSDK.getSwitchValue("tyq_video_interval");
        if (val) {
            val = parseInt(val);
            if (this.levelCount >= val) {
                this.levelCount = 0;
                this.showRewardAd();
            }
        }
    }

    // 展示激励视频广告
    showRewardAd(cb?: Function) {
        SdkMgr.ins.showRewardAd("", () => {
            // 完全观看结束，可领取奖励
            if (cb) {
                cb(1);
            }
        }, () => {
            // 用户点击关闭
            if (cb) {
                cb(0);
            }
        }, () => {
            // 广告加载失败
            if (cb) {
                cb(-1);
            }
        });
    }

    // 展示底部banner广告
    showBannerAd() {
        let time = tyqSDK.getSwitchValue("tyq_bannerTurns");
        if (time) {
            SdkMgr.ins.showBannerTurns(time);
        }
    }
    // 隐藏banner广告
    hideBannerAd() {
        SdkMgr.ins.hideBanner();
    }

    // 展示原生格子广告，包括顶部，左边和右边
    showCustomAdCommon() {
        if (tyqSDK.getSwitchValue("tyq_gametop")) {
            SdkMgr.ins.showWxCustomAd("custom_ad_top", EType.horizontal, {
                top: 0,
            });
        }
        if (tyqSDK.getSwitchValue("tyq_gameFire")) {
            SdkMgr.ins.showWxCustomAd("custom_ad_left", EType.vertical, {
                left: 0
            });
            SdkMgr.ins.showWxCustomAd("custom_ad_right", EType.vertical, {
                right: 0
            })
        }
    }
    // 隐藏原生格子广告，包括顶部，左边和右边
    hideCustomAdCommon() {
        SdkMgr.ins.hideWxCustomAd("custom_ad_top");
        SdkMgr.ins.hideWxCustomAd("custom_ad_left");
        SdkMgr.ins.hideWxCustomAd("custom_ad_right");
    }

    // 展示原生格子广告，包括左边和右边
    showCustomAdLeftRight() {
        if (tyqSDK.getSwitchValue("tyq_gameFire")) {
            SdkMgr.ins.showWxCustomAd("custom_ad_left", EType.vertical, {
                left: 0
            });
            SdkMgr.ins.showWxCustomAd("custom_ad_right", EType.vertical, {
                right: 0
            })
        }
    }
    hideCustomAdLeftRight() {
        SdkMgr.ins.hideWxCustomAd("custom_ad_left");
        SdkMgr.ins.hideWxCustomAd("custom_ad_right");
    }

    // 展示原生格子广告，矩阵类型
    showCustomAdRect() {
        SdkMgr.ins.showWxCustomAd("custom_ad_rect", EType.rect, {
            centerX: 0,
            centerY: 0
        });
    }
    hideCustomAdRect() {
        SdkMgr.ins.hideWxCustomAd("custom_ad_rect");
    }

    // 回到主界面 需要处理的广告逻辑
    onStartLayerShow() {
        let val = tyqSDK.getSwitchValue("tyq_returnMain");
        if (val == 1) {
            // 显示插屏广告
            SdkMgr.ins.showIntersAd();
        } else if (val == 2) {
            // 显示矩阵广告
            this.showCustomAdRect();
        }
    }

    // 关卡事件埋点
    sendEventLevelStart(level: string | number) {
        tyqSDK.startGame(level);
    }
    sendEventLevelEnd(isWin: boolean) {
        tyqSDK.endGame(isWin);
    }
    // 自定义事件埋点
    sendEvent(name: string) {
        TyqEventMgr.ins.sendEvent(name);
    }
}

