import Cookie from 'js-cookie';
//application/vnd.chromium.remoting-viewer 可能为360特有 通过_mine判断是否是360
export function isBrowser() {
	var agent = navigator.userAgent.toLowerCase();
	console.log(agent);
	const System = function () {
		if (agent.indexOf('qqbrowser') > 0) {
			//判断是qq浏览器还是其它浏览器
			return 'qq浏览器';
		}
		if (agent.indexOf('se 2.x') > 0) {
			return '搜狗浏览器';
		}

		var is360 = _mime('type', 'application/vnd.chromium.remoting-viewer');

		if (is360) {
			return '360浏览器';
		}

		//检测是否是谷歌内核(可排除360及谷歌以外的浏览器)
		//测试mime
		function _mime(option, value) {
			var mimeTypes = navigator.mimeTypes;
			console.log(mimeTypes);
			for (var mt in mimeTypes) {
				if (mimeTypes[mt][option] == value) {
					return true;
				}
			}
			return false;
		}
	};
	let s = System();
	if (s) {
		return s;
	}
	if (agent.indexOf('firefox') > 0) {
		return 'firefox浏览器';
	}
	if (agent.indexOf('trident') > 0) {
		return 'IE浏览器';
	}
	if (agent.indexOf('edg') > 0) {
		return 'IE浏览器';
	}
	if (agent.indexOf('safari') > 0 && agent.indexOf('chrome') < 0) {
		return 'Safari';
	}
	if (agent.indexOf('chrome') > 0) {
		return 'chrome';
	}
}

// 获取host
export const getEnvConfig = () => {
	let host, logHost;

	console.log('NODE_ENV=', process.env.NUXT_ENV.MODE);

	// @ts-ignore
	switch (process.env.NUXT_ENV.MODE) {
		case `prod`:
			host = `https://api.denetme.net`;
			logHost = 'https://log.denetme.net';
			break;
		case `pre`:
			host = `https://preapi.denetme.net`;
			logHost = 'https:/prelog.denetme.net';
			break;
		default:
			host = `https://testapi.denetme.net`;
			logHost = 'https://testlog.denetme.net';
			break;
	}

	return {
		host,
		logHost,
	};
};

export function getBrowser() {
	let browser;
	let UserAgent = navigator.userAgent.toLowerCase();
	if (UserAgent.indexOf('chrome') > -1 || UserAgent.indexOf('crios') > -1) {
		browser = `Chrome`;
	} else if (UserAgent.indexOf('firefox') > -1) {
		browser = `Firefox`;
	} else if (UserAgent.indexOf('opera') > -1) {
		browser = `Opera`;
	} else if (UserAgent.indexOf('safari') > -1 && UserAgent.indexOf('chrome') == -1) {
		browser = `Safari`;
	} else if (UserAgent.indexOf('edge') > -1) {
		browser = `Edge`;
	} else {
		browser = `Other`;
	}
	return browser;
}

export function formatSecondsAsTime(secs) {
	var hr = Math.floor(secs / 3600);
	var min = Math.floor((secs - hr * 3600) / 60);
	var sec = Math.floor(secs - hr * 3600 - min * 60);
	var text;
	if (hr < 10) {
		hr = '0' + hr;
	}
	if (min < 10) {
		min = '0' + min;
	}
	if (sec < 10) {
		sec = '0' + sec;
	}
	text = hr + ':' + min + ':' + sec;

	return text;
}

// 抽奖红包 left
export function formatSecondsAsDaysOrTime(secs) {
	if (secs <= 0) {
		return '00:00:00';
	}
	let text = '';
	var hr = Math.floor(secs / 3600);
	if (hr >= 24) {
		let day = parseInt(hr / 24);
		text = `${day} days left`;
	} else {
		text = formatSecondsAsTime(secs);
	}
	return text;
}

export const appVersionCode = 17;

export const appType = 1;

export function getBrowserType() {
	let device = ''; // ios 安卓 chrome no-chrome
	if (/android/i.test(navigator.userAgent)) {
		device = '安卓';
	} else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
		device = 'ios';
	} else if (isBrowser() == 'chrome') {
		device = 'chrome';
	} else {
		device = 'no-chrome';
	}
	return device;
}

const api = {
	prod: 'https://api.denetme.net',
	pre: 'https://preapi.denetme.net',
	test: 'https://testapi.denetme.net',
};
const page = {
	prod: 'https://h5.denetme.net',
	pre: 'https://preh5.denetme.net',
	test: 'https://testh5.denetme.net',
};

export const jumpUrl = page[process.env.NUXT_ENV.MODE] + '/';
export const baseURL = api[process.env.NUXT_ENV.MODE];

export const getStorage = (key) => {
	return JSON.parse(localStorage.getItem(key));
};

export const setStorage = (key, data) => {
	localStorage.setItem(key, JSON.stringify(data));
};

export const removeStorage = (key) => {
	localStorage.removeItem(key);
};

// storageKey
export const storageKey = {
	verifier: 'verifierKey',
	userInfo: 'userInfo',
	backFromTwitterLogin: 'denet-mobile-landpage-back-from-twitter-login',
};

// 推特授权url
export const getOauthUrl = (token) => {
	return `https://api.twitter.com/oauth/authenticate?oauth_token=${token}`;
};

// 创建窗口
export const createWindow = (url, w = 400, h = 600) => {
	var left = Math.round((window.screen.availWidth - w) / 2);
	var top = Math.round((window.screen.availHeight - 100 - h) / 2);
	var win = window.open(url, `newWin`, `width=${w}, height=${h}, top=${top}, left=${left}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
	return win;
};

// 帮助函数
const guid = () => {
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
		var r = (Math.random() * 16) | 0,
			v = c == 'x' ? r : (r & 0x3) | 0x8;
		return v.toString(16);
	});
};

// 获取mid
export const getMid = () => {
	let _mid;
	let _cookie_mid_arr = Cookie.get('mid') || [];
	if (_cookie_mid_arr.length > 0) {
		_mid = JSON.parse(_cookie_mid_arr)[0].mid;
	} else {
		_mid = guid();
		Cookie.set('mid', JSON.stringify([{ mid: _mid }]), { expires: 1000 });
	}
	return _mid;
};

export const getUserInfo = () => {
	let userInfo = getStorage(storageKey.userInfo) || null;
	if (userInfo) {
		return userInfo;
	} else {
		return null;
	}
};

export function getQueryString(name) {
	let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	let r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return decodeURIComponent(r[2]);
	}
	return null;
}

export const denetExtensionId = 'inlfbeejfdgkknpiodhemfcokbdgofja';
export function detectExtension(extensionId, callback) {
	var img;
	img = new Image();
	img.src = 'chrome-extension://' + extensionId + '/img/icon-denet-logo.svg';
	img.onload = function () {
		callback(true);
	};
	img.onerror = function () {
		callback(false);
	};
}
