// 导入 crypto-js 库 
import CryptoJS from 'crypto-js';

const utils = {
	// 加密函数 
	encryptData(data, key) {
		const encryptedData = CryptoJS.AES.encrypt(data, key).toString();
		return encryptedData;
	},

	// 解密函数 
	decryptData(encryptedData, key) {
		const decryptedData = CryptoJS.AES.decrypt(encryptedData, key).toString(CryptoJS.enc.Utf8);
		return decryptedData;
	},

	// 存储数据到Encrypted session Storage 
	storeEncryptedData(key, data) {
		const encryptedData = this.encryptData(data, key);
		sessionStorage.setItem(key, encryptedData);
	},

	// 从Encrypted Local Storage获取数据 
	getEncryptedData(key) {
		const encryptedData = sessionStorage.getItem(key);
		if (encryptedData) {
			const decryptedData = this.decryptData(encryptedData, key);
			return decryptedData;
		}
		return null;
	},

	// 获取任意时间
	getDate(date, AddDayCount = 0) {
		if (!date) {
			date = new Date();
		}

		if (typeof date !== 'object') {
			date = date.replace(/-/g, '/');
		}

		const dd = new Date(date);

		dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期

		var a = new Array("日", "一", "二", "三", "四", "五", "六");
		var week = new Date().getDay();
		var week2 = new Date().getDay() + 1;
		const y = dd.getFullYear();
		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期，不足10补0
		const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
		const h = dd.getHours() < 10 ? '0' + dd.getHours() : dd.getHours();
		const f = dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd.getMinutes();
		const s = dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds();
		return {
			fullDate: y + '-' + m + '-' + d,
			fullDate2: y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s,
			fullTime: m + '月' + d + '日',
			year: y,
			month: m,
			date: d,
			day: dd.getDay(),
			sWeek: "周" + a[week],
			eWeek: "周" + a[week2]
		};
	},

	// 计算指定时间是周几
	getweekday(date) {
		// date例如:'2022-03-05'
		var weekArray = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六")
		var weeka = weekArray[new Date(date).getDay()]
		return weeka
	},

	// 获取年月日时分秒周
	// getDateComponents(now_1, 'YYYY-MM-DD', 'YYYY-MM-DD HH:mm:ss', 'HH:mm:ss','date', 'datetime', 'time', 'weekday_cn', 'week_cn', 'weekday_cnx', 'week_en'));
	getDateComponents(date, ...components) {
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0');
		const day = String(date.getDate()).padStart(2, '0');
		const hour = String(date.getHours()).padStart(2, '0');
		const minute = String(date.getMinutes()).padStart(2, '0');
		const second = String(date.getSeconds()).padStart(2, '0');
		const week_cn = ["日", "一", "二", "三", "四", "五", "六"][date.getDay()];
		const weekday_cn = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"][date.getDay()];
		const weekday_cnx = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"][date.getDay()];
		const week_en = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];

		const result = {};

		for (const component of components) {
			switch (component) {
				case 'YYYY-MM-DD':
				case 'date':
					result['date'] = `${year}-${month}-${day}`;
					break;
				case 'YYYY-MM-DD HH:mm:ss':
				case 'datetime':
					result['datetime'] = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
					break;
				case 'monthday':
					result['monthday'] = `${month}-${day}`;
					break;
				case 'monthday_cn':
					result['monthday_cn'] = `${month}月-${day}日`;
					break;
				case 'HH:mm:ss':
				case 'time':
					result['time'] = `${hour}:${minute}:${second}`;
					break;
				case 'week_cn':
					result['week_cn'] = week_cn;
					break;
				case 'weekday_cn':
					result['weekday_cn'] = weekday_cn;
					break;
				case 'weekday_cnx':
					result['weekday_cnx'] = weekday_cnx;
					break;
				case 'week_en':
					result['week_en'] = week_en;
					break;
				default:
					break;
			}
		}

		return result;
	},

	// 指定日期加任意天数
	addDaysToDate(date, days) {
		const result = new Date(date);
		result.setDate(result.getDate() + days);
		return result;
	},

	// 检测是否为空
	isEmpty(value) {
		if (value === null || value === undefined) {
			return true;
		}

		if (typeof value === 'string' && value.trim() === '') {
			return true;
		}

		if (typeof value === 'object' && Object.keys(value).length === 0) {
			return true;
		}

		return false;
	},

	// 检测是否包含指定的字符串
	containsString(str, searchString) {
		return str.indexOf(searchString) !== -1;
	},

	// 解析URL中的参数
	getUrlKey(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
	},

	generateDates(startDate, endDate) {
		const dates = [];
		var current = new Date(startDate);
		var end = new Date(endDate);

		if ((new Date(end).getTime()) < (new Date(current).getTime())) {
			var temp = current;
			current = end;
			end = temp;
		}
		while (current < end) {
			const formattedDate = current.toISOString().slice(0, 10);
			dates.push(formattedDate);
			current.setDate(current.getDate() + 1);
		}
		
		return dates;
	}
}

export default utils;