var numToArray = function(num) {
	var arr = [];
	for (var i = 0; i < num; i++) {
		arr[i] = i;
	}
	return arr;
}

var formatNumber = function(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

var strToInt = function(n) {
	return parseInt(n)
}

var minuteToHour = function(minute) {
	// minute = Math.ceil(minute/60)
	var hour = Math.floor(minute / 60)
	var remainMinute = minute - hour * 60
	var minutehour = ""
	if (hour != 0) {
		minutehour = remainMinute != 0 ? (hour + "小时" + remainMinute + "分钟") : (hour + "小时")
	} else {
		minutehour = remainMinute != 0 ? (remainMinute + "分钟") : (hour + "小时")
	}
	return minutehour
	//return remainMinute !=0 ? (hour+"小时"+remainMinute+"分钟"):(hour+"小时")
}

/**
 * 时间戳格式化时间
 */
var formatTime = function(datetime) {

	if (datetime == 0) {
		return ""
	}

	var date = getDate(datetime * 1000);
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()
	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()
	return year + '-' + formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' +
		formatNumber(minute) + ':' + formatNumber(second);
}
// 时间戳格式化 返回日期
var formatGetDate = function(datetime) {
	var date = getDate(datetime * 1000);
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()

	return year + '-' + formatNumber(month) + '-' + formatNumber(day);
}



/**
 * 隐藏字符串 多余字符使用*替代
 */
var maskStr = function(str, replace, len) {
	if (typeof str != 'string')
		return '';
	if (str.length < len) return str;
	return str.substring(0, len) + replace + replace + replace;
}

/**
 * 将字符串分割成数组
 */
var splitx = function(str, splitstr) {
	if (typeof str != 'string')
		return [];
	return str.split(splitstr);
}

var toFix = function(value) {
	//return value.toFixed(2)
	return parseFloat(parseFloat(value).toFixed(2)) //此处2为保留两位小数
}

var toFixLength = function(value) {
	return parseFloat(parseFloat(value).toFixed(2)).toString().length //此处2为保留两位小数
}

var toFixThree = function(value) {
	return parseFloat(parseFloat(value).toFixed(3)) //此处3为保留两位小数
}

var toFixCeli = function(value) {
	return parseFloat(parseFloat(Math.ceil(value * 100) / 100).toFixed(2))

	//return parseFloat(parseFloat(value).toFixed(2))//此处2为保留两位小数
}

var formatToday = function() {
	var date = getDate()
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()
	var wday = date.getDay()
	var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

	return [formatNumber(month) + '月' + formatNumber(day) + '日', weekday[wday]];
}
var timestampToHHMM = function(timestamp) {
	var date = getDate(timestamp);
	var hours = date.getHours().toString().padStart(2, '0');
	var minutes = date.getMinutes().toString().padStart(2, '0');
	return hours + ':' + minutes;
}
var formatDistance = function(distanceMeters) {
	// 判断距离是否超过1000米  
	// var distanceMeters = Number(_distanceMeters)
	if (distanceMeters >= 1000) {
		// 如果超过1000米，则转换为千米并返回  
		return (distanceMeters / 1000).toFixed(2) + 'km';
	} else {
		// 否则直接返回米  
		return distanceMeters.toFixed(0) + 'm';
	}
}

var formatWeight = function(weightMeters) {
	if (weightMeters >= 1000) {
		return (weightMeters / 1000).toFixed(2) + 'kg';
	} else {
		return weightMeters.toFixed(0) + 'g';
	}
}

// 时间戳格式化 返回x月x日 x:x
var formatTimeDate = function(datetime) {
	if (datetime == 0) {
		return ""
	}
	var date = getDate(datetime * 1000);
	var month = date.getMonth() + 1
	var day = date.getDate()
	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()
	return formatNumber(month) + '月' + formatNumber(day) + '日 ' + formatNumber(hour) + ':' + formatNumber(minute);
}

// 时间戳格式化 返回x月x日 x:x
var formatTimeSecond = function(datetime) {
	if (datetime == 0) {
		return ""
	}
	var date = getDate(datetime * 1000);
	var month = date.getMonth() + 1
	var day = date.getDate()
	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()
	return formatNumber(month) + '.' + formatNumber(day) + '-' + formatNumber(hour) + ':' + formatNumber(minute);
}

var countToDay = function(count, unit) {
	var data = '';
	switch (unit - 0) {
		case 1: //日
			data = count
			break;
		case 2: //月
			data = count * 30
			break;
		case 4: //时
			data = count
			break;
		case 6: //周
			data = count * 7
			break;
		case 7: //季
			data = count * 90
			break;
	}
	return data;
}


module.exports = {
	numToArray: numToArray,
	formatTime: formatTime,
	formatGetDate: formatGetDate,
	maskStr: maskStr,
	splitx: splitx,
	toFix: toFix,
	toFixLength: toFixLength,
	toFixCeli: toFixCeli,
	formatToday: formatToday,
	minuteToHour: minuteToHour,
	strToInt: strToInt,
	timestampToHHMM: timestampToHHMM,
	formatDistance: formatDistance,
	formatWeight: formatWeight,
	toFixThree: toFixThree,
	formatTimeDate: formatTimeDate,
	formatTimeSecond: formatTimeSecond,
	countToDay: countToDay
}