export function timeFix() {
	const time = new Date();
	const hour = time.getHours();
	return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好';
}

export function welcome() {
	const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了'];
	const index = Math.floor(Math.random() * arr.length);
	return arr[index];
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
	const event = document.createEvent('HTMLEvents');
	event.initEvent('resize', true, true);
	event.eventType = 'message';
	window.dispatchEvent(event);
}

export function handleScrollHeader(callback) {
	let timer = 0;

	let beforeScrollTop = window.pageYOffset;
	callback = callback || function() {};
	window.addEventListener(
		'scroll',
		() => {
			clearTimeout(timer);
			timer = setTimeout(() => {
				let direction = 'up';
				const afterScrollTop = window.pageYOffset;
				const delta = afterScrollTop - beforeScrollTop;
				if (delta === 0) {
					return false;
				}
				direction = delta > 0 ? 'down' : 'up';
				callback(direction);
				beforeScrollTop = afterScrollTop;
			}, 50);
		},
		false
	);
}

export function isIE() {
	const bw = window.navigator.userAgent;
	const compare = s => bw.indexOf(s) >= 0;
	const ie11 = (() => 'ActiveXObject' in window)();
	return compare('MSIE') || ie11;
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate(id = '', timeout = 1500) {
	if (id === '') {
		return;
	}
	setTimeout(() => {
		document.body.removeChild(document.getElementById(id));
	}, timeout);
}

/**
 * 删除cookie
 * @param name 名称
 */
export function delCookie(name) {
	document.cookie = name + '=' + escape('') + ';expires=-1';
}

/**
 * 设置cookie
 * @param name 名称
 * @param value 值
 */
export function setCookie(name, value) {
	if (value) {
		let days = 1; //定义一天
		let exp = new Date();
		exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
		// 写入Cookie, toGMTString将时间转换成字符串
		document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString;
	}
}


/**
 * 验证xml格式的正确性
 * @param xmlContent
 * @returns {boolean}
 */
export function validateXML(xmlContent) {
	let xmlDoc = 0;
	// code for IE
	if (window.ActiveXObject) {
		xmlDoc  = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(xmlContent);

		if(xmlDoc.parseError.errorCode != 0) {
			return false;
		} else {
			return true;
		}
	}
	// code for Mozilla, Firefox, Opera, chrome, safari,etc.
	else if (document.implementation.createDocument) {
		let parser=new DOMParser();
		xmlDoc = parser.parseFromString(xmlContent,"text/xml");
		let error = xmlDoc.getElementsByTagName("parsererror");
		if (error.length > 0) {
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

/**
 * 校验json格式正确性
 * @param jsonStr
 * @returns {boolean}
 */
export function validateJSON(jsonStr) {
	if (typeof jsonStr == 'string') {
		try {
			let obj = JSON.parse(jsonStr);
			if (typeof obj == 'object' && obj ) {
				return true;
			} else {
				return false;
			}

		} catch(e) {
			console.log('error：' + jsonStr + '!!!' + e);
			return false;
		}
	}
}

/**
 * 填补空的名称
 * @param node 节点
 * @returns {*}
 */
export function replaceNullName(node) {
	let name = node.name;
	switch (node.struTypeCode) {
			case '14':
					name = i18nRender('global-dict.current-measures');
					break;
			case '07':
					name = i18nRender('global-dict.optimization-measures');
					break;
			case '08':
					name = node.name ? node.name : i18nRender('global-dict.group');
					break;
	}
	return name;
}
