import { _decorator, Component, Node } from 'cc';
import { GUIManager } from '../../../engines/gui/GUIManager';
import { GUIMediator } from '../../../engines/gui/GUIMediator';
import { SceneManager } from '../../../engines/scenes/SceneManager';
import { LiangLiangSDK } from '../../../libs/liangliangSDK';
import { PlatformManager } from '../../../Platform/PlatformManager';
import { UIConst } from '../UIConst';
const { ccclass, property } = _decorator;

@ccclass('AdvertisingMediator')
export class AdvertisingMediator extends GUIMediator {
    @property({
        type: Node
    })
    BackButton: Node = null;

    start() {

    }

    OnShow(data?: any): void {
        this.BackButton.active = false;
        PlatformManager.hideBanner();
        this.scheduleOnce(this.showBackButton.bind(this), 3);
    }

    OnHide(): void {
        this.BackButton.active = false;
    }

    get LayerIndex() {
        return 4;
    }

    showBackButton(): void {
        this.BackButton.active = true;
    }

    randomCpa():void{
        if(this.wx!=null)
        {
            let adList: any[] = LiangLiangSDK.GetList();
            let data = adList[Math.floor(Math.random()*adList.length)]
            this.wx.navigateToMiniProgram({
                appId: data.app_id,
                path: data.app_path,
                success: (res) => 
                {
                    LiangLiangSDK.CpaReport(self.data);
                },
                fail: (res) => {
                    // GUIManager.single.Show(UIConst.FULL_OUTPUT_UI);
                    console.log("跳转失败！");
                },
                complete: (res) => {},
            });
        }
    }

    onClickBack(): void {
        this.HideSelf();
        if (LiangLiangSDK.CanWuChu() == true) {
            this.randomCpa();
            PlatformManager.showRewardedVideo(()=>{
                PlatformManager.showBanner();
                GUIManager.single.Show(UIConst.FULL_OUTPUT_UI);
            }, ()=>{
                PlatformManager.showBanner();
                GUIManager.single.Show(UIConst.FULL_OUTPUT_UI);
            })
        }
    }

    private get wx(): any {
        return window["wx"];
    }
}
