import BasePlatform from './BasePlatform.js';
//#ifdef MP
import MiniProgramPlatform from './MiniProgramPlatform';
//#endif
//#ifdef APP-PLUS
import AppPlatform from './AppPlatform';
import IOSPlatform from './IOSPlatform';
//#endif
//#ifdef WEB
import H5Platform from './H5Platform';
import WebAppPlatform from './WebAppPlatform';
import NewWebAppPlatform from './NewWebAppPlatform';
//#endif
class PlatformFactory {
	static create() {
		console.log('获取平台获取平台获取平台获取平台获取平台获取平台');

		// 判断小程序环境
		//#ifdef MP
		return new MiniProgramPlatform();
		//#endif
		// 判断 IOS环境
		//#ifdef APP-IOS
		return new IOSPlatform();
		//#endif
		// 判断 App 环境
		//#ifdef APP

		return new AppPlatform();
		//#endif

		// 判断 H5 环境
		//#ifdef WEB
		// if(window.location.href.includes('cb2039d0e35094521ae46a1d11b0ddd1')){
		//     return new WebAppPlatform();
		// }
		let is_web_app = this.checkIsWebApp();
		let is_new_web_app = this.checkIsNewWebApp();

		if (is_web_app) {
			return new WebAppPlatform();
		}
		if (is_new_web_app) {
			return new NewWebAppPlatform();
		}
		return new H5Platform();
		//#endif

		// 默认返回
		return new BasePlatform();
	}

	static checkIsWebApp() {
		return this.checkPlatformType('is_web_app');
	}
	
	static checkIsNewWebApp() {
		return this.checkPlatformType('is_new_web_app','cb20xad0e35094521ae46a1d1fb0ddd1');
	}
	
	static checkPlatformType(storageKey, codeValue = 'cb2039d0e35094521ae46a1d11b0ddd1') {
		let isPlatformType = uni.getStorageSync(storageKey);
		let search = window.location.search;

		if (search != null && search != '') {
			const searchParams = new URLSearchParams(window.location.search);
			const code = searchParams.get('_p');
			if (code && code == codeValue) {
				uni.setStorageSync(storageKey, true);
				isPlatformType = true;
			}
		}
		if (uni.getStorageSync(storageKey) != null && uni.getStorageSync(storageKey) != '') {
			isPlatformType = true;
		}
		
		return isPlatformType;
	}
}

// 使用示例
const platform = PlatformFactory.create();
// console.log(platform.env,"获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台获取平台");
export {
	platform
};