const timeText = (time, format) => {

	if (format == null) {
		format = "yyyy-MM-dd HH:mm:ss"
	}

	let timeStr = "";

	let todayEndTime = getTodayEndTime();
	let todayStartTime = getTodayStartTime();
	let thisWeekStartTime = getThisWeekStartTime();

	console.log("kaishi" + todayStartTime)
	console.log("jieshu" + thisWeekStartTime)


	// 今天23:59:59:999之后
	if (time > todayEndTime) {
		// 显示年月日
		timeStr = timeFormat(time, format)
	} else if (thisWeekStartTime > time) {
		// 此周前（本周星期一之前）

		let i = format.indexOf(" ");
		let formatStyle = format.substring(i + 1);
		let formatNew = timeFormat(time, formatStyle);

		if (time > todayStartTime - 86399999 && time < todayStartTime) {
			// 显示昨天
			timeStr = "昨天 " + formatNew;
		} else {
			timeStr = timeFormat(time, format)
		}

	} else {
		// 显示星期 时分
		let i = format.indexOf(" ");
		let formatStyle = format.substring(i + 1);
		let formatNew = timeFormat(time, formatStyle);

		if (todayStartTime - 86399999 > time) {
			// 显示星期
			timeStr = getWeekStr(time) + " " + formatNew
		} else if (time > todayStartTime - 86399999 && time < todayStartTime) {
			// 显示昨天
			timeStr = "昨天 " + formatNew;
		} else {
			// 显示
			timeStr = formatNew;
		}
	}

	return timeStr
}

/**
 * 获取本周开始时间
 */
const getThisWeekStartTime = () => {

	let todayEndTime = getTodayEndTime();

	var date = new Date();
	var weekDays = date.getDay();
	var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
	var week = weeks[weekDays];

	return todayEndTime + 1 - weekDays * 86400000;
}

/**
 * 获取今天是周几
 */
const getWeekStr = (time) => {


	if (time instanceof Date) {

	} else {
		let temp = new Date(time);
		time = temp;
	}

	let todayEndTime = getTodayEndTime();

	var weekDays = time.getDay();
	var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
	var week = weeks[weekDays];

	return week;
}

/**
 * 获取今天开始时间
 */
const getTodayStartTime = () => {

	let startTime = 0;

	let myDate = new Date();

	let current = myDate.getTime();

	try {

		let format = timeFormat(myDate);
		let split = format.split(" ");
		let time = split[1].split(":");

		startTime = current - time[0] * 60 * 60 * 1000 - time[1] * 60 * 1000 - time[2] * 1000 - time[3]

	} catch (e) {
		console.log(e)
	}

	return startTime;
}

/**
 * 获取今天结束时间
 */
const getTodayEndTime = () => {

	let endTime = 0;

	let myDate = new Date();

	let current = myDate.getTime();

	try {

		let format = timeFormat(myDate);
		let split = format.split(" ");
		let time = split[1].split(":");

		let startTime = current - time[0] * 60 * 60 * 1000 - time[1] * 60 * 1000 - time[2] * 1000 - time[3]
		endTime = startTime + 86399999;

	} catch (e) {
		console.log(e)
	}

	return endTime;
}

/**
 * 格式化时间
 * 支持时间戳 以及 date类型
 */
const timeFormat = (date, formoat) => {

	if (date == undefined || date == null) {
		return "";
	}

	if (date instanceof Date) {
		console.log("正确")
	} else {
		let temp = new Date(Number(date));
		date = temp;
	}

	let fmt = formoat;

	if (fmt == null) {
		fmt = "yyyy-MM-dd HH:mm:ss:SSS"
	}


	let ret;
	const opt = {
		"y+": date.getFullYear().toString(), // 年
		"M+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"H+": date.getHours().toString(), // 时
		"m+": date.getMinutes().toString(), // 分
		"s+": date.getSeconds().toString(), // 秒
		"S+": date.getMilliseconds().toString()

	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}

const currentMillions = () => {
	let current = new Date().getTime();
	return current;
}

export {
	timeText,
	timeFormat,
	currentMillions,
	getWeekStr,
	getTodayEndTime
}
