// 计算坐标之间旋转角度
let calcAngle = (start, end) => {
	let y = Math.sin(end.longitude - start.longitude) * Math.cos(end.latitude);
	let x =
		Math.cos(start.latitude) * Math.sin(end.latitude) -
		Math.sin(start.latitude) * Math.cos(end.latitude) * Math.cos(end.longitude - start.longitude);
	let angle = Math.atan2(y, x);
	angle = (180 * angle) / Math.PI;
	if (end.latitude > start.latitude) {
		return angle - 90
	}
	if (end.latitude < start.latitude) {
		return 180 - angle
	}
	return angle - 90;
}

let isDuringDate = (start, end) => {
	let curDate = new Date().getTime();
	let beginDate = new Date(start.replace(/-/g, '/')).getTime();
	let endDate = new Date(end.replace(/-/g, '/')).getTime();
	if (curDate >= beginDate && curDate <= endDate) {
		return true;
	} else {
		return false;
	}
}

let getImagePath = (path) => {
	// console.log('工具类', uni.$u.http.config.baseURL);
	if (uni.$u.test.url(path)) {
		return path;
	} else {
		return uni.$u.http.config.baseURL + path;
	}
}

// 对象数组排序
let compareObjArray = (p) => {
	return (m, n) => {
		var a = m[p];
		var b = n[p];
		return a - b; //升序
	}
}

let getTimeState = () => {
	// 获取当前时间
	let timeNow = new Date();
	// 获取当前小时
	let hours = timeNow.getHours();
	console.log('小时', hours);
	// 设置默认文字
	let text = ``;
	// 判断当前时间段
	if (hours >= 0 && hours < 10) {
		text = `早上好`;
	} else if (hours >= 10 && hours < 14) {
		text = `中午好`;
	} else if (hours >= 14 && hours < 18) {
		text = `下午好`;
	} else if (hours >= 18 && hours < 24) {
		text = `晚上好`;
	}
	console.log(text);
	// 返回当前时间段对应的状态
	return text;
}

let hidePhoneNumberOrName = (hideStr, way = 0) => {
	if (!hideStr) {
		return '号码有误'
	}
	// 获取当前时间
	let result = '';
	if (way === 0) {
		result = hideStr.toString().slice(0, 3) + '****' + hideStr.toString().slice(-4);
	}
	if (way === 1) {
		result = hideStr.toString().slice(0, 1) + '**';
	}
	if (way === 2) {
		// result = hideStr.toString().slice(0, 1) + '**';
		if (hideStr.length <= 7) {
			result = hideStr;
		} else {
			result = hideStr.toString().slice(0, 2) + '***' + hideStr.toString().slice(-2);
		}
	}
	if (way === 3) {
		result = hideStr.toString().slice(0, 1) + '***' + hideStr.toString().slice(-1);
	}
	return result;
}

let isDuringDateHour = (start, end) => {
	// console.log(start, end);
	// let year = new Date().getFullYear();
	// let month = new Date().getMonth() + 1;
	// let day = new Date().getDate();
	// let forward = '';
	// if (month < 10) {
	// 	forward = year + '-0' + month + '-' + day
	// } else {
	// 	forward = year + '-' + month + '-' + day
	// }
	// let tempStart = forward + ' ' + start;
	// let tempEnd = forward + ' ' + end;
	// tempStart = '2023-10-10 00:00:00'
	let curDate = new Date().getTime();
	let beginDate = new Date(start.replace(/-/g, '/')).getTime();
	let endDate = new Date(end.replace(/-/g, '/')).getTime();
	if (curDate < beginDate) {
		return 0;
	}
	if (curDate >= beginDate && curDate <= endDate) {
		return 1;
	}
	if (curDate > endDate) {
		return 2;
	}
}

// 判断是否在今天
let isToday = (time) => {
	let year = new Date().getFullYear();
	let month = new Date().getMonth() + 1;
	let day = new Date().getDate();
	let target = new Date(time);
	let targetYear = new Date().getFullYear();
	let targetMonth = new Date().getMonth() + 1;
	let targetDay = new Date().getDate();
	if (year === targetYear && month === targetMonth && day === targetDay) {
		return true;
	} else {
		return false;
	}
}

let getDayLater = (time) => {
	if (time.length === 0) {
		return ''
	}
	let curTime = new Date().getTime();
	let targetTime = new Date(time.replace(/-/g, '/')).getTime();
	if (curTime >= targetTime) {
		return '今天'
	}
	let sub = targetTime - curTime;
	const oneDay = 24 * 60 * 60 * 1000;
	let result = Math.ceil(sub / oneDay)
	if (result === 1) {
		return '明天'
	}
	if (result === 2) {
		return '后天'
	}
	return `${result}天后`;
}

let todayOrNext = (time) => {
	// console.log('time: ',time);
	let targetYear = new Date(time.replace(/-/g, '/')).getFullYear();
	let targetMonth = new Date(time.replace(/-/g, '/')).getMonth() + 1;
	let targetDay = new Date(time.replace(/-/g, '/')).getDate();
	let targetHour = new Date(time.replace(/-/g, '/')).getHours();
	let targetMin = new Date(time.replace(/-/g, '/')).getMinutes();
	let year = new Date().getFullYear();
	let month = new Date().getMonth() + 1;
	let day = new Date().getDate();

	if (targetMin < 10) {
		targetMin = '0' + targetMin
	}

	if (year === targetYear && month === targetMonth && day === targetDay) {
		return `${targetHour}:${targetMin}`;
	}
	if (year === targetYear && month === targetMonth && (targetDay - day) === 1) {
		return `明天${targetHour}:${targetMin}`;
	}


	return `${targetMonth}月${targetDay}日`
}

// 获取时间差（天）
let getRange = (target) => {
	let targetDate = new Date(target);
	let currentDate = new Date(); // 获取当前日期对象
	let timeDiff = Math.abs(targetDate.getTime() - currentDate.getTime());
	let daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
	if (daysDiff === 1) {
		return '明日开抢'
	}
	return daysDiff + '天后开抢';
}

// 获取时间差（天）
let getTimeDiffYear = (start, end) => {
	console.log('start: ', start);
	console.log('end: ', end);
	if (!start || !end) {
		return true
	}
	let startDate = new Date(start.replace(/-/g, '/')).getTime();
	let endDate = new Date(end.replace(/-/g, '/')).getTime();
	if (endDate < startDate) {
		uni.$u.toast('结束日期小于开始日期')
		return false;
	}
	const oneYearInMs = 365 * 24 * 60 * 60 * 1000; // 近似一年的毫秒数
	if (endDate - startDate > oneYearInMs) {
		uni.$u.toast('结束日期大于开始日期12个月')
		return false;
	}
	return true;
}

let isBeforeToday = (time) => {
	time = time + ' 00:00:00';
	let currentDate = new Date(); // 获取当前日期对象
	let targetDate = new Date(time.replace(/-/g, '/')).getTime();
	return targetDate < currentDate
}

let getWeekAndDay = (time) => {
	console.log(time);
	let targetDate = new Date(time.replace(/-/g, '/'));
	let temp = targetDate.getDay();
	let weekDay = '';
	switch (temp) {
		case 1:
			weekDay = '一'
			break;
		case 2:
			weekDay = '二'
			break;
		case 3:
			weekDay = '三'
			break;
		case 4:
			weekDay = '四'
			break;
		case 5:
			weekDay = '五'
			break;
		case 6:
			weekDay = '六'
			break;
		case 7:
			weekDay = '日'
			break;
		default:
			break;
	}
	return weekDay;
}

let goElemeAppOrMini = (shopId) => {
	console.log(shopId);
	// #ifdef APP-PLUS
	plus.share.getServices(platforms => {
		console.log(platforms);
		let sweixin = null;
		for (let i = 0; i < platforms.length; i++) {
			if (platforms[i].id === 'weixin') {
				sweixin = platforms[i];
				break;
			}
		}
		if (sweixin) {
			sweixin.launchMiniProgram({
				id: 'gh_6506303a12bb',
				path: `ele-estore/ele-takeout-index/ele-takeout-index?&shopId=${shopId}`
			})
		} else {
			uni.$u.toast('请先安装微信')
		}
	})
	// #endif
	// #ifdef MP-WEIXIN
	uni.navigateToMiniProgram({
		appId: 'wxece3a9a4c82f58c9',
		path: `ele-estore/ele-takeout-index/ele-takeout-index?&shopId=${shopId}`,
		envVersion: 'release',
		success: (e) => {
			console.log(e);
		},
		fail: (error) => {
			console.log(error);
		}
	})
	// #endif
}

let openDouyinApp = (videoId) => {
	if (plus.runtime.isApplicationExist({
			pname: 'com.ss.android.ugc.aweme',
			action: 'snssdk1128://'
		})) {
		plus.runtime.openURL(
			`snssdk1128://aweme/detail/${videoId}`, (
				res) => {
				console.log(res);
			})
	} else {
		uni.$u.toast('请先安装抖音App')
	}
}

let openDouyinAppUserCenter = (uid) => {
	console.log('uid: ', uid);
	if (plus.runtime.isApplicationExist({
			pname: 'com.ss.android.ugc.aweme',
			action: 'snssdk1128://'
		})) {
		plus.runtime.openURL(
			`snssdk1128://user/profile/${uid}`, (
				res) => {
				console.log(res);
			})
	} else {
		uni.$u.toast('请先安装抖音App')
	}
}

let goMeiTuanAppOrMini = (shopId) => {
	console.log(shopId);
	// #ifdef APP-PLUS
	if (plus.os.name === 'Android') {
		if (plus.runtime.isApplicationExist({
				pname: 'com.sankuai.meituan.takeoutnew'
			})) {
			console.log('已安装美团外卖App');
			plus.runtime.openURL(
				`meituanwaimai://waimai.meituan.com/menu?restaurant_id=${shopId}&spu_id=0`, (
					res) => {
					console.log(res);
				})
		} else {
			console.log('未安装美团外卖App');
			plus.share.getServices(platforms => {
				console.log(platforms);
				let sweixin = null;
				for (let i = 0; i < platforms.length; i++) {
					if (platforms[i].id === 'weixin') {
						sweixin = platforms[i];
						break;
					}
				}
				if (sweixin) {
					sweixin.launchMiniProgram({
						id: 'gh_72a4eb2d4324',
						path: `pages/restaurant/restaurant?poi_id=${shopId}`
					})
				} else {
					uni.$u.toast('请先安装微信')
				}
			})
		}
	} else {
		plus.share.getServices(platforms => {
			console.log(platforms);
			let sweixin = null;
			for (let i = 0; i < platforms.length; i++) {
				if (platforms[i].id === 'weixin') {
					sweixin = platforms[i];
					break;
				}
			}
			if (sweixin) {
				sweixin.launchMiniProgram({
					id: 'gh_72a4eb2d4324',
					path: `pages/restaurant/restaurant?poi_id=${shopId}`
				})
			} else {
				uni.$u.toast('请先安装微信')
			}
		})
	}
	// #endif
	// #ifdef MP-WEIXIN
	uni.navigateToMiniProgram({
		appId: 'wx2c348cf579062e56',
		path: `pages/restaurant/restaurant?poi_id=${shopId}`,
		envVersion: 'release',
		success: (e) => {
			console.log(e);
		},
		fail: (error) => {
			console.log(error);
		}
	})
	// #endif

}

let openMiniCouponCenter = (path, appId, originalId) => {
	console.log(path);
	console.log(appId);
	console.log(originalId);
	if (path.trim().length === 0) {
		return uni.showToast({
			title: '暂未配置跳转链接',
			icon: 'none'
		})
	}
	// #ifdef MP-WEIXIN
	uni.navigateToMiniProgram({
		appId: appId,
		path: path,
		envVersion: 'release',
		success: (e) => {
			console.log(e);
		},
		fail: (error) => {
			console.log(error);
		}
	})
	// #endif
	// #ifdef APP-PLUS
	plus.share.getServices(platforms => {
		let sweixin = null;
		for (let i = 0; i < platforms.length; i++) {
			if (platforms[i].id === 'weixin') {
				sweixin = platforms[i];
				break;
			}
		}
		if (sweixin) {
			sweixin.launchMiniProgram({
				id: originalId,
				path: path
			})
		} else {
			uni.$u.toast('请先安装微信')
		}
	})
	// #endif
}

let openWechatServer = () => {
	uni.share({
		provider: 'weixin',
		scene: 'WXSceneSession',
		openCustomerServiceChat: true,
		corpid: 'wwef0708802d6f526c',
		customerUrl: 'https://work.weixin.qq.com/kfid/kfc65b94a0a45799e77',
		success: (e) => {
			console.log(e);
		},
		fail: (error) => {
			console.log(error);
		}
	})

	// return uni.$u.toast('微信客服尚未配置')
}

let openCompanyWechatGroup = () => {
	plus.share.getServices(platforms => {
		console.log(platforms);
		let sweixin = null;
		for (let i = 0; i < platforms.length; i++) {
			if (platforms[i].id === 'weixin') {
				sweixin = platforms[i];
				break;
			}
		}
		if (sweixin) {
			sweixin.launchMiniProgram({
				id: uni.$u.http.config.originMiniId,
				path: `pages/group/pages/store-add-group/store-add-group`
			})
		} else {
			uni.$u.toast('请先安装微信')
		}
	})
}

let timestampToDate = (timestamp) => {
	const date = new Date(timestamp);
	const year = date.getFullYear();
	// 月份+1，因为从0开始计算，并且确保两位数
	const month = ('0' + (date.getMonth() + 1)).slice(-2);
	// 日期确保两位数
	const day = ('0' + date.getDate()).slice(-2);
	return `${year}-${month}-${day}`;
}

let askLogin = () => {
	uni.showModal({
		title: '暂未登录',
		content: '请先前往登录',
		showCancel: false,
		success: (e) => {
			if (e.confirm) {
				uni.navigateTo({
					url: '/pages/subpack/common/login/login'
				})
			}
		}
	})
}

let checkPwdPattern = (text) => {
	console.log('text: ', text);
	const upperCaseRegex = /[A-Z]/
	const lowerCaseRegex = /[a-z]/
	const numberRegex = /[0-9]/
	const specialCharRegex = /[~!@#\$%\^&\*\(\)-\+=\{\}:;\|\[\]/.,<>/?']/
	if (!upperCaseRegex.test(text) || !lowerCaseRegex.test(text) || !numberRegex.test(text) || !
		specialCharRegex.test(text)) {
		return false
	} else {
		return true
	}
}

export {
	// calcAngle,
	// isDuringDate,
	// getImagePath,
	// compareObjArray,
	// getTimeState,
	// hidePhoneNumberOrName,
	// isDuringDateHour,
	// isToday,
	// todayOrNext,
	// getRange,
	// goMeiTuanAppOrMini,
	// goElemeAppOrMini,
	// openDouyinApp,
	// openWechatServer,
	// openMiniCouponCenter,
	// openCompanyWechatGroup,
	// getDayLater,
	// timestampToDate,
	// askLogin,
	// openDouyinAppUserCenter,
	// getTimeDiffYear,
	// isBeforeToday,
	checkPwdPattern,
	getWeekAndDay
}