import axios from 'axios';
/**
 * @desc 函数防抖
 * @param func 函数
 * @param wait 延迟执行毫秒数
 * @param immediate true 表立即执行，false 表非立即执行
 * this:值当前防抖函数下的this，不是func。例如 application.vue onSubmit下的this，
 * args：是绑定函数的参数。例如 application.vue onSubmit(5)下的argument 中有5
 */
function _debounce(func, wait = 500, immediate = true) {
	var timeout;
	return function () {
		var context = this;
		var args = arguments;
		if (timeout) clearTimeout(timeout);
		if (immediate) {
			var callNow = !timeout;
			timeout = setTimeout(() => {
				timeout = null;
			}, wait);
			if (callNow) func.apply(context, args);
		} else {
			timeout = setTimeout(function () {
				func.apply(context, args);
			}, wait);
		}
	};
}
/**
 * @desc 函数节流，
 * @param func 函数
 * @param wait 延迟执行毫秒数
 * @param type 1 表时间戳版，2 表定时器版
 * this:值当前防抖函数下的this，不是func。例如 application.vue onSubmit下的this，
 * args：是绑定函数的参数。例如 application.vue onSubmit(5)下的argument 中有5
 */
function _throttle(func, wait = 500, type = 1) {
	if (type === 1) {
		var previous = 0;
	} else if (type === 2) {
		var timeout;
	}
	return function () {
		var context = this;
		var args = arguments;
		if (type === 1) {
			var now = Date.now();
			if (now - previous > wait) {
				func.apply(context, args);
				previous = now;
			}
		} else if (type === 2) {
			if (!timeout) {
				timeout = setTimeout(() => {
					timeout = null;
					func.apply(context, args);
				}, wait);
			}
		}
	};
}

/**
 * 长数组分组
 * @param  array  原数组
 * @param  subGroupLength 分组长度
 * @returns 返回分组后的数组
 */
function group(array, subGroupLength) {
	let index = 0;
	let newArray = [];
	while (index < array.length) {
		newArray.push(array.slice(index, (index += subGroupLength)));
	}
	return newArray;
}
/**
 *UUID
 */
function createGuid() {
	return 'xxxxxxxx-xxxx-8xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
		var r = (Math.random() * 16) | 0;
		var v = c === 'x' ? r : (r & 0x3) | 0x8;
		return v.toString(17);
	});
}

/**
 *
 * @returns 获取当前时间年+月+日+时+分+秒
 */
export function getCurDataTime() {
	let time = new Date();
	let year, month, day, hour, min, sec;
	year = time.getFullYear();
	month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
	day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
	hour = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
	min = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
	sec = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
	let dateTime = `${year}-${month}-${day} ${hour}:${min}:${sec}`; //+year + month + day + hour + min + sec;
	return dateTime;
}
/**
 *
 * @returns  获取当前时间整点年+月+日+时+00+00
 */
export function getWholeDataTime() {
	let time = new Date();
	let year, month, day, hour;
	year = time.getFullYear();
	month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
	day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
	hour = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
	let dateTime = `${year}-${month}-${day} ${hour}:00:00`; //+year + month + day + hour + 00 + 00;
	return dateTime;
}
/**
 *
 * @returns  获取当前时间的下n个整点时刻年+月+日+时+00+00
 */
export function addWholeDataTime(n = 1) {
	let a = new Date() + n * 1000 * 60 * 60;
	let time = new Date(a);
	let year, month, day, hour;
	year = time.getFullYear();
	month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
	day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
	hour = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
	let dateTime = `${year}-${month}-${day} ${hour}:00:00`; //+year + month + day + hour + 00 + 00;
	return dateTime;
}
/**
 *
 * @returns  获取当前时间的上n个整点时刻年+月+日+时+00+00
 */
export function supWholeDataTime(n = 1) {
	let a = new Date() - n * 1000 * 60 * 60;
	let time = new Date(a);
	let year, month, day, hour;
	year = time.getFullYear();
	month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
	day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
	hour = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
	let dateTime = `${year}-${month}-${day} ${hour}:00:00`; //+year + month + day + hour + 00 + 00;
	return dateTime;
}

/**
 * ping链接
 * @param {String} ip
 * @param {Function} cb
 */
function ping(ip, cb) {
	axios({
		method: 'post',
		url: `${ip}/cache/service/connectivity`,
		timeout: 1000,
	})
		.then(res => {
			cb(true);
		})
		.catch(error => {
			cb(false);
		});
}
/**
 * echarts字体自适应
 * @param {*} font 字号大小
 */
export function fontPxToRem(font) {
	let docEl = document.documentElement,
		clientWidth =
			window.innerWidth ||
			document.documentElement.clientWidth ||
			document.body.clientWidth;
	if (!clientWidth) return;
	let fontSize = clientWidth / 1920;
	return font * fontSize;
}
// 对象深拷贝
export function deepClone(data) {
	let type = getObjectType(data)
	let obj
	if (type === 'array') {
		obj = []
	} else if (type === 'object') {
		obj = {}
	} else {
		// 不再具有下一层次
		return data
	}
	if (type === 'array') {
		for (let i = 0, len = data.length; i < len; i++) {
			data[i] = (() => {
				if (data[i] === 0) {
					return data[i]
				}
				return data[i]
			})()
			if (data[i]) {
				delete data[i].$parent
			}
			obj.push(deepClone(data[i]))
		}
	} else if (type === 'object') {
		for (let key in data) {
			if (data) {
				delete data.$parent
			}
			obj[key] = deepClone(data[key])
		}
	}
	return obj
}
// 获取对象类型
function getObjectType(obj) {
	let toString = Object.prototype.toString
	let map = {
		'[object Boolean]': 'boolean',
		'[object Number]': 'number',
		'[object String]': 'string',
		'[object Function]': 'function',
		'[object Array]': 'array',
		'[object Date]': 'date',
		'[object RegExp]': 'regExp',
		'[object Undefined]': 'undefined',
		'[object Null]': 'null',
		'[object Object]': 'object',
	}
	if (obj instanceof Element) {
		return 'element'
	}
	return map[toString.call(obj)]
}




export { _debounce, _throttle, group, createGuid, ping };
