import JsbSystem from "../../../../system/JsbSystem";
import { IADSDKMgr as IADSDKMgr } from "../../IADSDKMgr";
import { zgSingleton } from "../../zgSingleton";
import ATBanner from "./ATBanner";
import ATInterstitial from "./ATInterstitial";
import { ATNative } from "./ATNative";
import { ATRewardVideo } from "./ATRewardVideo";

const { ccclass, property } = cc._decorator;
@ccclass
export default class ATSDKMgr extends zgSingleton implements IADSDKMgr {

    /**Topon Appid */
    public static topon_android_appId = "a5f8fa5f10c661";
    /**Topon AppSecret */
    public static topon_android_appSecret = "66b742dd0e502b64d783d9c66a9d30c4";
    /**Topon 激励视频广告位 */
    public static topon_rewardVideo_placementId = "b5f8fa6673e180";
    /**Topon 插屏广告位 */
    public static topon_inter_placementId = "b5f8fa77aa5262";
    /**Topon 信息流广告位 */
    public static topon_native_placementId = "b5f8fa77aa5262";
    /**Topon 全屏插屏广告位 */
    public static topon_fullinter_placementId = "b5f8fa77aa5262";
    /**Topon 全屏插屏广告位 */
    public static topon_banner_placementId = "b5f8fa77aa5262";

    /**初始化ATSDK */
    public initSDK() {
        if (!cc.sys.isNative) {
            return;
        }

        this.getAdvertiseChannel();

        cc["ATSDKMgrJS"].proxy = this;
        cc["ATSDKMgrJS"].initATJSSDK(ATSDKMgr.topon_android_appId, ATSDKMgr.topon_android_appSecret);

        //初始化激励视频广告
        ATRewardVideo.getInstance().init();
        //初始化原生广告
        ATNative.getInstance().init();
        // //初始化插屏广告
        ATInterstitial.getInstance().init();
        //初始化banner广告
        ATBanner.getInstance().init();
    }

    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;
        }
    }

    /** 获取投放渠道 和 广告位ID信息 */
    public getAdvertiseChannel() {
        let str = JsbSystem.getAdvertisingChannel();
        if (!str) {
            //channelId,AppId,AppSecreate,激励视频，插屏，信息流，开屏，banner
            str = '1,a60cac090a6eb0,66b742dd0e502b64d783d9c66a9d30c4,b60cac0e43c0e1,b60cac0e4892fa,b60cac0e4d08e8,b60cac0e5754fc,b60cac0e524313';
        }
        console.log("getAdvertiseChannel..." + str);

        let arr = str.split(',')
        let channel = arr[0];
        ATSDKMgr.topon_android_appId = arr[1];
        ATSDKMgr.topon_android_appSecret = arr[2];
        ATSDKMgr.topon_rewardVideo_placementId = arr[3];
        ATSDKMgr.topon_inter_placementId = arr[4];
        ATSDKMgr.topon_native_placementId = arr[5];
        ATSDKMgr.topon_fullinter_placementId = arr[6];
        ATSDKMgr.topon_banner_placementId = arr[7];
    }

    /**显示视频广告 */
    showVideoAd() {
        ATRewardVideo.getInstance().showAd();
    }

    /** 
     * 观看视频回调
     * @param adPlayStateType 广告播放状态类型 观看结束、播放失败、取消（关闭）
     */
    watchVideoAdCallBack(adPlayStateType: AdPlayStateType) {
        switch (adPlayStateType) {
            case AdPlayStateType.WatchEnd:
                mk.ad.wacthAdSuccess();
                break;
            case AdPlayStateType.PlayFail:
                mk.ad.watchAdFail();
                break;
            case AdPlayStateType.Cancel:
                mk.ad.watchAdFail();
                break;
        }
    }

    /**显示插屏广告 */
    showInterAd() {

    }

    /** 
     * 加载原生广告
     * @param type 0 是插屏 1 是全屏
     * @param show 是否显示
     */
    loadNativeAd(type: number, show: boolean) {
        ATNative.getInstance().loadNative(type, show);
    }

    /** 销毁原生广告 */
    destroyNativeAd() {
        ATNative.getInstance().destroyNative();
    }

    showBannerAd() {
        ATBanner.getInstance().showBanner();
    }

    destoryBanner() {
        ATBanner.getInstance().destroyBanner();
    }

    showInter() {
        ATInterstitial.getInstance().showInter(0);
    }
}

/** 广告播放状态类型 */
export enum AdPlayStateType {
    /** 观看结束 */
    WatchEnd = 0,
    /** 播放失败  */
    PlayFail = 1,
    /** 取消 */
    Cancel = 2,
}
