import CryptoJS from 'crypto-js';

const tools = {
	data: {
		set(cacheKey: string, data: any, expireIn: number = 0) {
			let cacheValue = {
				content: data,
				expireIn: expireIn === 0 ? 0 : new Date().getTime() + expireIn * 1000
			}
			return localStorage.setItem(cacheKey, tools.base64.encrypt(JSON.stringify(cacheValue)))
		},
		get(cacheKey: string) {
			try {
				const cacheValue = JSON.parse(tools.base64.decrypt(localStorage.getItem(cacheKey)))
				if (cacheValue) {
					let nowTime = new Date().getTime()
					if (nowTime > cacheValue.expireIn && cacheValue.expireIn !== 0) {
						localStorage.removeItem(cacheKey)
						return null;
					}
					return cacheValue.content
				}
				return null
			} catch (err) {
				return null
			}
		},
		remove(cacheKey: string) {
			return localStorage.removeItem(cacheKey)
		},
		clear() {
			return localStorage.clear()
		}
	},
	base64: {
		encrypt(data: string) {
			return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data))
		},
		decrypt(cipher: string) {
			return CryptoJS.enc.Base64.parse(cipher).toString(CryptoJS.enc.Utf8)
		}
	},
	go: async function (fn: Function) {
		try {
			let res = await fn
			return [res, null]
		} catch (err) {
			return [null, err]
		}
	},
	crypto: {
		//MD5加密
		MD5(data: string) {
			return CryptoJS.MD5(data).toString()
		},
	},
	makeMenu: function (menus, parent_id = 0) {
		const arr = [];
		for (let item of menus) {
			if (item.parent_id === parent_id) {
				// 数据格式处理
				const tmp = {
					name: item['name'],
					path: '/' + item['path'],
					component: item['path'],
					meta: {
						'title': item['title'],
						'icon': item['icon'],
						'hidden': item['hidden'],
						'type': 'menu'
					}
				};
				const children = this.makeMenu(menus, item.menu_id);
				if (children.length > 0) {
					tmp['children'] = children
				}
				arr.push(tmp)
			}
		}
		return arr
	},
	screen: function (element) {
		var isFull = !!(document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement);
		if (isFull) {
			if (document.exitFullscreen) {
				document.exitFullscreen();
			} else if (document.msExitFullscreen) {
				document.msExitFullscreen();
			} else if (document.mozCancelFullScreen) {
				document.mozCancelFullScreen();
			} else if (document.webkitExitFullscreen) {
				document.webkitExitFullscreen();
			}
		} else {
			if (element.requestFullscreen) {
				element.requestFullscreen();
			} else if (element.msRequestFullscreen) {
				element.msRequestFullscreen();
			} else if (element.mozRequestFullScreen) {
				element.mozRequestFullScreen();
			} else if (element.webkitRequestFullscreen) {
				element.webkitRequestFullscreen();
			}
		}
	},
	/* 复制对象 */
	objCopy: function (obj) {
		return JSON.parse(JSON.stringify(obj));
	},
	/* 日期格式化 */
	dateFormat: function (date, fmt='yyyy-MM-dd hh:mm:ss') {
		date = new Date(date)
		var o = {
			"M+" : date.getMonth()+1,                 //月份
			"d+" : date.getDate(),                    //日
			"h+" : date.getHours(),                   //小时
			"m+" : date.getMinutes(),                 //分
			"s+" : date.getSeconds(),                 //秒
			"q+" : Math.floor((date.getMonth()+3)/3), //季度
			"S"  : date.getMilliseconds()             //毫秒
		};
		if(/(y+)/.test(fmt)) {
			fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
		}
		for(var k in o) {
			if(new RegExp("("+ k +")").test(fmt)){
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
			}
		}
		return fmt;
	}
}

export default tools
