/**
 * @author uu
 * @file  通用页面控制器和适配
 */
var AC = require('action')
const ADSdk = require("ADSdk")
cc.Class({
	extends: cc.Component,
	properties: {
		status: 0, //页面状态
		pages: [cc.Node],
	},
	// 0 开始游戏页面
	// 1 游戏页面
	// 2 helpUI页面
	// 3 过关页面
	// 4 失败页面
	// 5 复活页面
	// 6 排行榜页面


	start() {


		this.splash = this.node.getChildByName("splash");
		this.splash.active = false;
		this.splashIsOver = false; //闪屏已经执行 才可播放信息流
		this.lateStart()
		// TODO 广告 开屏
		if (AppConst.SHOWADUI) {
			if (AppConst.CHANNEL == AppConst.channel.ANDROID) {
				// 手Q没有关闭监听
				ADSdk.pay(9, this);
			} else {
				// this.showSplash();
			}
		}

	},

	lateStart() {
		this.width = cc.director.getWinSizeInPixels().width
		window.width = this.width
		this.height = cc.director.getWinSizeInPixels().height
		window.height = this.height
		// 存为全局变量
		this.adoptCanvas()
	},

	// 适配解决方案
	adoptCanvas() {
		let canvas = cc.director.getScene().getChildByName('Canvas').getComponent(cc.Canvas)
		// 设计分辨率比
		let rateR = canvas.designResolution.height / canvas.designResolution.width;
		// 显示分辨率比
		let rateV = this.height / this.width;
		if (rateV > rateR) {
			canvas.fitHeight = false;
			canvas.fitWidth = true;
		} else {
			canvas.fitHeight = true;
			canvas.fitWidth = false;
		}
	},

	showSplash() {


		// if(this.isRuningFade) return;
		// this.splash.runAction(
		// 	cc.sequence(
		// 		cc.callFunc(() => {
		// 			this.isRuningFade = true;
		// 		}),
		// 		cc.delayTime(1),
		// 		cc.fadeOut(1.0),
		// 		cc.callFunc(() => {
		// 			this.splashIsOver = true;
		// 			if (AppConst.CHANNEL == AppConst.channel.ANDROID) {
		this.splashIsOver = true;
		this.showNativeAD(0);
		// 			}
		// 		})
		// 	));

	},

	onOpenPage(num, callFun) {
		this.closeAllPages()
		this.pages[num].active = true
		// if (callFun) {
		//   this.callFun();
		// }
		this.showNativeAD(num);
	},
	addPage(num, callFun) {
		this.pages[num].scale = 0.5
		this.pages[num].active = true
		this.pages[num].runAction(AC.popOut(0.5))
		// if (callFun) {
		//   this.callFun();
		// }

		this.showNativeAD(num);

	},
	openPageTarget(target) {
		target.scale = 0.5
		target.active = true
		target.runAction(AC.popOut(0.5))
	},
	removePage(num, callFun) {
		this.pages[num].runAction(cc.sequence(AC.popIn(0.5), cc.callFunc(() => {
			this.pages[num].active = false
		}, this)))
		// if (callFun) {
		//   this.callFun();
		// }
	},
	onButtonOpenPage(event, cust) {
		this.onOpenPage(cust);
	},
	onButtonAddPage(event, cust) {
		this.addPage(cust);
	},
	onButtonRemovePage(event, cust) {
		this.removePage(cust);
	},
	closeAllPages() {
		this.pages.forEach(element => {
			element.active = false
		});
	},

	showNativeAD(id) {
		// TODO 先关闭原生广告
		// TODO 广告 原生广告/插屏广告

		let num = Utils.randomNum(0, 100);
		let nativeAD = AppConst.probabilityConfig.nativeAD;
		let time = 0;
		switch (id) {
			case 0:// 主界面
				if (num < nativeAD.mainUI.probability) {
					if (AppConst.SHOWADUI) {
						// cc.log("信息流")
						if (AppConst.CHANNEL == AppConst.channel.ANDROID) {
							if (this.splashIsOver) {
								time = nativeAD.mainUI.delayTime;
								this.scheduleOnce(() => {
									ADSdk.pay(5, this);
								}, time);
							}
						} else {
							ADSdk.showInsertAd();
						}
					}
				}
				break;
			case 5:// 复活界面

				if (num < nativeAD.gameFailUI.probability) {
					if (AppConst.SHOWADUI) {
						if (AppConst.CHANNEL == AppConst.channel.ANDROID) {
							time = nativeAD.gameFailUI.delayTime;
							this.scheduleOnce(() => {
								ADSdk.pay(7, this);
							}, time);

						} else {
							ADSdk.showInsertAd();
						}

					}
				}
				break;

			case 1:// 游戏界面

				// TODO 广告 banner
				// 玩家主动关闭40s后再次展示
				// 玩家未主动关闭30S自动销毁，再同步调用展示其他广告
				if (AppConst.SHOWADUI) {
					if (AppConst.CHANNEL == AppConst.channel.ANDROID) {
						// 手Q没有关闭监听
						ADSdk.pay(0, this);
					} else {
						ADSdk.showBanner();
						// this.scheduleOnce(() => {
						// 	ADSdk.hideBanner();
						// 	this.showBanner();
						// }, 30);
					}
				}
				break;

			case 3:// 过关界面

				if (num < nativeAD.gameSucessUI.probability) {
					if (AppConst.SHOWADUI) {

						if (AppConst.CHANNEL == AppConst.channel.ANDROID) {
							time = nativeAD.gameSucessUI.delayTime;
							this.scheduleOnce(() => {
								ADSdk.pay(11, this);
							}, time);

						} else {
							ADSdk.showInsertAd();
						}

					}
				}
				break;
		}
	},

	// 原生UI
	showNativeAdUI(id) {

	},

});