import store from "store";
/** 获取浏览器信息 */
export const browser = () => {
	const u = navigator.userAgent;
	return {
		trident: u.indexOf("Trident") > -1, //IE内核
		presto: u.indexOf("Presto") > -1, //opera内核
		webKit: u.indexOf("AppleWebKit") > -1, //苹果、谷歌内核
		gecko: u.indexOf("Gecko") > -1 && u.indexOf("KHTML") == -1, //火狐内核
		mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
		ios: !!u.match(/Mac OS X/) || /(iPhone|iPad|iPod|iOS)/i.test(u), //ios终端
		android: u.indexOf("Android") > -1 || u.indexOf("Adr") > -1, //判断是否是 android终端
		iPhone: u.indexOf("ORCHESTRAAPPI") > -1, //是否为iPhone或者QQHD浏览器
		isApp:
			u.includes("DAYAAPPA") ||
			u.includes("DAYAAPPI") ||
			u.includes("COLEXIUAPPA") ||
			u.includes("COLEXIUAPPI") ||
			u.includes("ORCHESTRAAPPI") ||
			u.includes("ORCHESTRAAPPA"),
		isTeacher: u.indexOf("ORCHESTRATEACHER") > -1 || u.includes("COLEXIUTEACHER"),
		isStudent: u.indexOf("ORCHESTRASTUDENT") > -1 || u.includes("COLEXIUSTUDENT"),
		isSchool: u.indexOf("ORCHESTRASCHOOL") > -1,
		iPad: u.indexOf("iPad") > -1, //是否iPad
		webApp: u.indexOf("Safari") == -1, //是否web应该程序，没有头部与底部
		weixin: u.indexOf("MicroMessenger") > -1, //是否微信 （2015-01-22新增）
		alipay: u.indexOf("AlipayClient") > -1, //是否支付宝
		huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
		xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i),
	};
};
/** uuid */
export const getRandomKey = () => {
	const key = "" + Date.now() + Math.floor(Math.random() * 1000000);
	return key;
};

export const AuthorizationKey = "AUTHORIZATION";
/** 设置token */
export const setToken = (value: any) => {
	sessionStorage.setItem(AuthorizationKey, value);
};
/** 获取token */
export const getToken = () => {
	return sessionStorage.getItem(AuthorizationKey) || "";
};

/** 设置全局通信 */
export const setGlobalData = (_key: string, _value: any) => {
	if (!_key || !_value) return;
	const GYM = (window as any).GYM || {};
	GYM[_key] = _value;
	(window as any).GYM = GYM;
};

const BEHAVIORIDKEY = "BEHAVIORID";
/** 设置 behaviorId */
export const setBehaviorId = (value: any) => {
	localStorage.setItem(BEHAVIORIDKEY, value);
};
/** 获取 behaviorId */
export const getBehaviorId = () => {
	return localStorage.getItem(BEHAVIORIDKEY);
};

const campIdKey = "CAMPID";
/** 设置 训练营ID */
export const setCampId = (value: any) => {
	sessionStorage.setItem(campIdKey, value);
};
/** 获取 训练营ID */
export const getCampId = () => {
	return sessionStorage.getItem(campIdKey) || "";
};

// 秒转分
export const getSecondRPM = (second: number, type?: string) => {
	if (isNaN(second)) return "00:00";
	let h = Math.floor((second / 60 / 60) % 24);
	let m = Math.floor((second / 60) % 60);
	let s = Math.floor(second % 60);
	if (type === "cn") {
		return `${h > 0 ? h.toString().padStart(2, "0") + "时" : ""}${m.toString().padStart(2, "0")}分${s
			.toString()
			.padStart(2, "0")}秒`;
	} else {
		return `${h > 0 ? h.toString().padStart(2, "0") + ":" : ""}${m.toString().padStart(2, "0")}:${s
			.toString()
			.padStart(2, "0")}`;
	}
};
const SPEEDKEY = "speeds";
/** 设置曲谱速度 */
export const setStorageSpeed = (id: any, speed: number) => {
	const speeds = store.get(SPEEDKEY) || {};
	speeds[id] = speed;
	store.set(SPEEDKEY, speeds);
};
/** 获取曲谱速度 */
export const getStorageSpeed = (id: any) => {
	const speeds = store.get(SPEEDKEY) || {};
	return speeds[id] || 0;
};

/**
 * Sums the passed percentage to the R, G or B of a HEX color
 * @param {string} color The color to change
 * @param {number} amount The amount to change the color by
 * @returns {string} The processed part of the color
 */
function addLight(color: string, amount: number) {
	const cc = parseInt(color, 16) + amount;
	const c = cc > 255 ? 255 : cc;
	return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
}

/**
 * Lightens a 6 char HEX color according to the passed percentage
 * @param {string} color The color to change
 * @param {number} amount The amount to change the color by
 * @returns {string} The processed color represented as HEX
 */
export function lighten(color: string, amount: number) {
	color = color.indexOf("#") >= 0 ? color.substring(1, color.length) : color;
	amount = Math.trunc((255 * amount) / 100);
	return `#${addLight(color.substring(0, 2), amount)}${addLight(
		color.substring(2, 4),
		amount
	)}${addLight(color.substring(4, 6), amount)}`;
}

export function downloadFile(url: string, fileName?: string) {
	var link = document.createElement('a');
	document.body.appendChild(link);
	link.setAttribute("style","display: none;");
	link.href = url;
	link.download = fileName ? fileName : 'output.wav';
	link.click();
	window.URL.revokeObjectURL(url);
	document.body.removeChild(link);
};

export function encodeUrl(data: object) {
	return window.btoa(encodeURIComponent(JSON.stringify(data)))
}
export function decodeUrl(data: string) {
	return JSON.parse(decodeURIComponent(window.atob(data)))
}
