import Gamecfg from "../../common/gameCfg";
import { gameMethod } from "../../common/gameMethod";
import { UpList } from "../../common/Xys";
import FguiMgr from "../../frameWork/fgui/FguiMgr";
import { FguiClass } from "../../frameWork/fgui/mvc/FguiView";
import IDataModel from "../../frameWork/model/IDataModel";
import UIHelp from "../../logic/ui/UIHelp";
import { I18n } from "../../utils/I18nUtil";
import { OpenType } from "../const/TypeConst";
import GameDataCenter from "../GameDataCenter";


export default class PageModel extends IDataModel {

	constructor() {
		super("page");
	}

	/**
	 * 切换到主页时检查的ui
	 * 功能开启时也会调用
	 */
	// readonly CheckUI: FguiClass<any>[] = [];

	jumpTo(id: string) {
		let conf = null;//Gamecfg.jumpInfo.getItem(id);
		if (conf == null) {
			if (CC_PREVIEW) console.warn("跳转信息不存在,id:", id);
			return;
		}
		if (!this.checkOpenAll(conf.goto as OpenType, true)) return;
		try {
			// this.jumpToByType(conf);
		} catch (err) {
			if (CC_PREVIEW) console.error("页面跳转出错:", err);
		}
	}


	/**
	 * 通用功能开启检测
	 * @param type 功能开启类型
	 * @param showTips 是否展示未开启提示弹窗
	 * @returns
	 */
	checkOpenAll(type: OpenType, showTips: boolean = false): boolean {
		if (this.OpenTypeMap[type] != null) {
			return this.checkOpen(type, showTips);
		} else if (this.HdOpenTypeMap[type] != null) {
			return this.canShowHd(type, showTips);
		} else {
			if (CC_PREVIEW) console.warn("未处理的功能开启类型:", type);
			return false;
		}
	}

	/**
	 * 活动的功能开启检测
	 * @param type 活动的功能开启类型
	 * @param showTips 是否展示未开启提示弹窗
	 * @returns
	 */
	canShowHd(type: OpenType, showTips: boolean = false): boolean {
		let canShow = this.checkOpen(type, showTips);
		// 特殊判断
		if (canShow) {
			// switch (type) {
			// 	//通用活动开启时间判断
			// 	default:
			// 		canShow = this.isInHdTime(type);
			// 		break;
			// }
		}
		return canShow;
	}

	//支付功能未开放时不打开的功能
	readonly payOpenType = [];
	/**
	 * 功能开启检测
	 * @param type 功能开启
	 * @param showTips 是否展示未开启提示弹窗
	 * @returns
	 */
	checkOpen(type: OpenType, showTips = false): boolean {
		if (GameDataCenter.order.canPay == false) {
			if (this.payOpenType.indexOf(type) != -1) {
				return false;
			}
		}

		// 后端判断开启的直接开
		if (GameDataCenter.sevBack?.actKaiQi?.a?.[type] > 0) {
			return true;
		}
		let isOpen = false;
		/**
		 * 主页默认开启,不走配表
		 */
		if (type == OpenType.home) return true;
		let conf = null;//Gamecfg.kaiqiInfo.getItem(type);
		if (conf == null) {
			if (CC_PREVIEW) console.error("未配置的开启类型:", type);
			return false;
		}
		// 如果有服务器时间限制,先判断下
		if (!gameMethod.isEmpty(conf.sevOpenDay)) {
			if (GameDataCenter.user.getSevOpenDay() < conf.sevOpenDay) {
				if (showTips) {
					UIHelp.ShowI18nTips("PageModel_showtips_5", conf.sevOpenDay);
				}
				return false;
			}
		}
		// isOpen = this.checkOpenType(conf);

		// // 服务器开启第n天解锁
		// if (isOpen && !gameMethod.isEmpty(conf.sevOpenDay)) {
		// 	isOpen = GameDataCenter.user.getSevOpenDay() >= conf.sevOpenDay;
		// }
		// if (isOpen == false && showTips) {
		// 	UIHelp.ShowTips(this.checkOpenTips(conf));
		// }
		return isOpen;
	}

	/**
	 * 通用页面打开方法
	 * @param type 页面功能对应的Opentype
	 */
	openPage(type: OpenType, ...params: any[]) {
		let jumpfunc: OpenTypeMap = this.getOpenTypeMap(type);
		if (jumpfunc?.openFunc == null) {
			if (CC_PREVIEW) console.error(`Opentype ${type} 的打开方法不存在`);
			return;
		}
		jumpfunc.openFunc.apply(GameDataCenter.page, params);
	}

	/**
	 * 获取功能对应信息
	 * @param type
	 * @returns
	 */
	getOpenTypeMap(type: OpenType): OpenTypeMap {
		let info = this.OpenTypeMap[type];
		if (info == null) {
			info = this.HdOpenTypeMap[type];
		}
		if (info == null) {
			return;
		}
		info.type = type;
		return info;
	}

	/**
	 * 获取活动功能对应信息
	 * @param type
	 * @returns
	 */
	getHdOpenTypeMap(type: OpenType): HdOpenTypeMap {
		let info = this.HdOpenTypeMap[type];
		if (info == null) {
			return;
		}
		info.type = type;
		return info;
	}

	/**
	 * 常驻功能的打开方法映射
	 * @param openFunc 对应功能的打开方法,为了之后优化功能跳转
	 */
	readonly OpenTypeMap: { [key: string]: OpenTypeMap } = {
		
	};
	/**
	 * 活动功能开启类型和datamodel与服务端下发数据的映射关系
	 * sevBackData用getter形式获取
	 *
	 * @param model 对应的model模块,展示的活动需要设置,常开功能可不设置
	 * @param sevBackData 对应的sevback内的数据,展示的活动需要设置,常开功能可不设置
	 * @param openFunc 对应的打开方法
	 */
	readonly HdOpenTypeMap: { [key: string]: HdOpenTypeMap } = {
		
	};

	/**
	 * 通用活动是否处于活动有效开启时间内的判断
	 * 并给对应的datamodel赋予开启的hdcid
	 * @param openType
	 * @returns
	 */
	isInHdTime(openType: OpenType) {
		let canShow = false;
		let hdmap = this.HdOpenTypeMap[openType];
		if (!hdmap) {
			if (CC_PREVIEW) console.error(`Opentype "${openType}" 未设置对应model和sevback数据的对应关系`);
			return canShow;
		}
		let sevdata = hdmap.sevBackData();
		if (!gameMethod.isEmpty(sevdata)) {
			for (const hdcid in sevdata) {
				const element = sevdata[hdcid];
				if (GameDataCenter.time.sevTime >= element?.a?.info?.sAt && GameDataCenter.time.sevTime <= element?.a?.info?.eAt) {
					(hdmap.model() as any).hdcid = hdcid;
					canShow = true;
					break;
				} else if ((element?.a?.info?.dAt ?? 0) > 0 && GameDataCenter.time.sevTime > element?.a?.info?.eAt && GameDataCenter.time.sevTime <= element?.a?.info?.dAt) {
					// show大于零表示有配置领奖期 在判断是否在领奖期时间段里
					(hdmap.model() as any).hdcid = hdcid;
					canShow = true;
					break;
				}
			}
		}
		return canShow;
	}

	/**
	 * 通用活动是否处于活动领奖期时间内的判断
	 * @param openType
	 */
	checkInHdAwardTime(openType: OpenType) {
		let hdInfo = this.getHdOpenTypeMap(openType);
		if (hdInfo) {
			let eAtTime = hdInfo.sevBackData()?.[(hdInfo.model() as any)?.hdcid]?.a?.info?.eAt ?? 0;
			let dAtTime = hdInfo.sevBackData()?.[(hdInfo.model() as any)?.hdcid]?.a?.info?.dAt ?? 0;
			let sevTime = GameDataCenter.time.sevTime;

			// 判断是否有领奖期
			if (dAtTime > 0 && dAtTime > eAtTime) {
				// 判断是否处于领奖期内
				if (sevTime > eAtTime && sevTime <= dAtTime) {
					return true;
				}
			}
		}

		return false;
	}
}
/**
 * @param openFunc 常驻功能对应的打开方法
 */
class OpenTypeMap {
	openFunc: Function;
	view: FguiClass<any>;
	lastOpen?: OpenType;
	type?: OpenType;
	page?: any;
}

/**
 * @param model 对应的model模块,展示的活动需要设置
 * @param sevBackData 对应的sevback内的数据,展示的活动需要设置
 * @param openFunc 对应的打开方法
 */
export class HdOpenTypeMap extends OpenTypeMap {
	model: () => IDataModel;
	sevBackData: () => { [hdcid: string]: UpList<any> };
}
