
import Config from "./Config";
import Gamecfg from "./common/gameCfg";
import GameDataCenter from "./data/GameDataCenter";
import { eg } from "./frameWork/eg";
import FguiMgr from "./frameWork/fgui/FguiMgr";
import HttpRequest from "./network/HttpRequest";
import Network from "./network/Network";
import AssetsBundleMgr from "./utils/AssetsBundleMgr";
import Load from "./utils/Load";
import NativeManager from "./utils/NativeManager";
import { SingletonFactory } from "./utils/SingletonFactory";
import ThinkingDataMgr from "./utils/ThinkingDataMgr";

class GameController {
	network: Network = null;
	rewardedVideoAd: any = null;
	// websocket: NetworkWebSocket = null;

	constructor() {
		CC_PREVIEW && (window['gamecontroller'] = this)
	}

	init(callback: Function) {
		// 新建一个网络单例
		this.network = SingletonFactory.getInstance(Network);
		// this.websocket = SingletonFactory.getInstance(NetworkWebSocket);

		// 所有UI扩展都写在这
		this.initUIExpand()

		NativeManager.Init()
		console.log("初始化配置表")
		let startTime = new Date().getTime()
		Gamecfg.initLoading((isSucc: boolean) => {
			console.log("初始化配置表流程结束,消耗时长:" + (new Date().getTime() - startTime) + "ms")
			if (isSucc) {
				console.log("解析成功")
				// 初始化数据模块
				GameDataCenter.initLoadModule();
				// GameDataCenter.adVideo.setAdOpen()
				// //初始化数数
				ThinkingDataMgr.TDInit();
			} else {
				console.log("解析失败")
			}
			callback(isSucc)
		})
	}

	clear() {
		this.network.RemoveTimers();
		// if (GameDataCenter.plat.instance.rewardedVideoAd) {
		//     // 销毁激励视频广告实例
		//     console.log("销毁广告实例，避免重复触发回调")
		//     GameDataCenter.plat.instance.rewardedVideoAd.destroy();
		// }
		cc.Tween.stopAll()
		//清空已经所有的界面
		FguiMgr.Instance.clearAllUI()
		fgui.TweenManager.clearAll();
		Load.clear()
		if (Config.upid != null) {
			clearInterval(Config.upid)
		}
		if (Config.upFrame != null) {
			clearInterval(Config.upFrame)
		}
		if (Config.upFight != null) {
			clearInterval(Config.upFight)
		}
		cc.director.getScene().removeAllChildren(true)
		Config.inGame = 0
		this.network.stopRequest = false
		HttpRequest.errPostMap.Clear()
		GameDataCenter._rspModel.clear();
		eg.poolManager?.ReleaseAllPool();
		fgui.UIPackage.removeAllPackage()
		AssetsBundleMgr.releaseAllBundle();
	}

	private initUIExpand() {
		// tween的暂停与恢复
		// cc.ActionInterval.prototype.step = function (dt) {
		// 	if (this.paused) {
		// 		return;
		// 	}

		// 	if (this._firstTick && !this._goto) {
		// 		this._firstTick = false;
		// 		this._elapsed = 0;
		// 	} else {
		// 		this._elapsed += dt;
		// 	}

		// 	let t = this._elapsed / (this._duration > 0.0000001192092896 ? this._duration : 0.0000001192092896);
		// 	t = (1 > t ? t : 1);
		// 	this.update(t > 0 ? t : 0);

		// 	//Compatible with repeat class, Discard after can be deleted (this._repeatMethod)
		// 	if (this._repeatMethod && this._timesForRepeat > 1 && this.isDone()) {
		// 		if (!this._repeatForever) {
		// 			this._timesForRepeat--;
		// 		}
		// 		this.startWithTarget(this.target);
		// 		this.step(this._elapsed - this._duration);
		// 	}
		// };

		// cc.Tween.prototype.pause = function () {
		// 	this._finalAction.paused = true;
		// };

		// cc.Tween.prototype.resume = function () {
		// 	this._finalAction.paused = false;
		// };
	}
}

export default new GameController();