/**
 *  Copyright [2022] [https://www.xiaonuo.vip]
 *	Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *	1.请不要删除和修改根目录下的LICENSE文件。
 *	2.请不要删除和修改Snowy源码头部的版权声明。
 *	3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 *	4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 *	5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 *	6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
import enquireJs from 'enquire.js'

export const DEVICE_TYPE = {
	DESKTOP: 'desktop',
	TABLET: 'tablet',
	MOBILE: 'mobile'
}

export const deviceEnquire = function (callback) {
	const matchDesktop = {
		match: () => {
			callback && callback(DEVICE_TYPE.DESKTOP)
		}
	}

	const matchTablet = {
		match: () => {
			callback && callback(DEVICE_TYPE.TABLET)
		}
	}

	const matchMobile = {
		match: () => {
			callback && callback(DEVICE_TYPE.MOBILE)
		}
	}

	// screen and (max-width: 1087.99px)
	enquireJs
		.register('screen and (max-width: 576px)', matchMobile)
		.register('screen and (min-width: 576px) and (max-width: 1199px)', matchTablet)
		.register('screen and (min-width: 1200px)', matchDesktop)
}

export const getDate = function (dateType, dateString, calculate) {
	let result = dateType
	let d = dateString ? new Date(dateString) : new Date()
	calculate && (d = setDate(d, calculate))
	const week = ['日', '一', '二', '三', '四', '五', '六']
	if (dateType.length == 0) {
		return d.getTime()
	}
	if (dateType.includes('Y')) {
		result = result.replace(/Y+/g, d.getFullYear().toString())
	}
	if (dateType.includes('y')) {
		result = result.replace(/y+/g, (d.getFullYear() + '').slice(2, 4))
	}
	if (dateType.includes('M')) {
		result = result.replace(/M+/g, (101 + d.getMonth() * 1 + '').slice(1, 3))
	}
	if (dateType.includes('m')) {
		result = result.replace(/m+/g, (d.getMonth() * 1 + 1).toString())
	}
	if (dateType.includes('D')) {
		result = result.replace(/D+/g, (100 + d.getDate() * 1 + '').slice(1, 3))
	}
	if (dateType.includes('d')) {
		result = result.replace(/d+/g, d.getDate().toString())
	}
	if (dateType.includes('H')) {
		result = result.replace(/H+/g, (100 + d.getHours() * 1 + '').slice(1, 3))
	}
	if (dateType.includes('h')) {
		result = result.replace(/h+/g, d.getHours().toString())
	}
	if (dateType.includes('I')) {
		result = result.replace(/I+/g, (100 + d.getMinutes() * 1 + '').slice(1, 3))
	}
	if (dateType.includes('i')) {
		result = result.replace(/i+/g, d.getMinutes().toString())
	}
	if (dateType.includes('S')) {
		result = result.replace(/S+/g, (100 + d.getSeconds() * 1 + '').slice(1, 3))
	}
	if (dateType.includes('s')) {
		result = result.replace(/s+/g, d.getSeconds().toString())
	}
	if (dateType.includes('w') || dateType.includes('W')) {
		result = result.replace(/[wW]+/g, week[d.getDay() * 1])
	}
	return result

	function setDate (d, calculate = {}) {
		Object.keys(calculate).forEach((v) => {
			if (!setFn[v]) {
				return
			}

			let val = Number(calculate[v]) || 0
			let getDateMethod = d[getFn[v]]()
			let setDateMethod = d[setFn[v]](Number(getDateMethod) + val)
			d = new Date(setDateMethod)
		})
		return d
	}
}
