import {
	PageAnimationTypeIn,
	PageAnimationTypeOut
} from "./PageAnimationType"
export const util = {

	/**
	 * 后退
	 * @param {Number} delta 后退步数, 默认:1
	 * @param {String} animationType 窗口关闭的动画效果, 默认:random随机
	 * @param {Number} animationDuration 窗口关闭动画的持续时间，单位为 ms, 默认:300
	 */
	back(delta = 1, animationType = 'random', animationDuration = 300) {
		if (!PageAnimationTypeOut.includes(animationType)) {
			animationType = PageAnimationTypeOut[util.random(0, PageAnimationTypeOut.length)];
		}
		return new Promise((success, fail) => {
			uni.navigateBack({
				delta,
				animationType,
				animationDuration,
				success,
				fail
			});
		});
	},

	/**
	 * 页面跳转
	 * @param {String} url 跳转到的页面 
	 * @param {*} events 页面间通信接口，用于监听被打开页面发送到当前页面的数据。
	 * @param {String} animationType 窗口关闭的动画效果, 默认:random随机
	 * @param {Number} animationDuration 窗口关闭动画的持续时间，单位为 ms, 默认:300
	 * @returns Promise
	 */
	to(url = "", events = {}, animationType = 'random', animationDuration = 300) {
		if (!PageAnimationTypeIn.includes(animationType)) {
			animationType = PageAnimationTypeIn[util.random(0, PageAnimationTypeIn.length)];
		}
		return new Promise((success, fail) => {
			uni.navigateTo({
				url,
				events,
				animationType,
				animationDuration,
				success,
				fail
			});
		});
	},

	/**
	 * 普通消息
	 * @param {String} title 
	 */
	msg(title) {
		uni.showToast({
			title: title,
			icon: 'none'
		});
	},

	/**
	 * 成功消息
	 * @param {String} title 
	 */
	ok(title) {
		uni.showToast({
			title: title,
			icon: 'success'
		});
	},


	/**
	 * 错误消息
	 * @param {String} title 
	 */
	err(title) {
		uni.showToast({
			title: title,
			icon: 'error'
		});
	},


	/**
	 * 隐藏消息框
	 */
	hide() {
		uni.hideToast();
	},

	/**
	 * 确认提示
	 * @param { String} content 内容
	 * @param { String} title 标题, 默认: 确认提示
	 * @returns Promise
	 */
	confirm(content,title="确认提示") {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title,
				content,
				showCancel:true,
				editable:false,
				success: function (res) {
					if (res.confirm) {
						resolve();
					} else if (res.cancel) {
						reject();
					}
				}
			});
		})
	},

	/**
	 * 系统提示
	 * @param { String} content 内容
	 * @param { String} title 标题,默认: 系统提示
	 * @returns Promise
	 */
	alert(content,title="系统提示") {
			return new Promise((resolve, reject) => {
				uni.showModal({
					title,
					content,
					showCancel:false,
					editable:false,
					success: function (res) {
						if (res.confirm) {
							resolve();
						} else if (res.cancel) {
							reject();
						}
					}
				});
			})
	},
	
	/**
	 * 输入提示
	 * @param { String} content 内容
	 * @param { String} title 标题, 默认: 输入提示
	 * @returns Promise
	 */
	prompt(content,title="输入提示") {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title,
				content,
				showCancel:true,
				editable:true,
				success: function (res) {
					if (res.confirm) {
						resolve(res.content);
					} else if (res.cancel) {
						reject();
					}
				}
			});
		})
	},

	/**
	 * 随机整数
	 * @param {Number} min 
	 * @param {Number} max 
	 * @returns Number
	 */
	random(min = 0, max = 10) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	},

	/**
	 * 将文本内容复制到剪贴板
	 * @param {String} text 
	 */
	copy(text) {
		if (!text) {
			return;
		}
		text = text.toString();
		uni.setClipboardData({
			data: text, //要被复制的内容
			success: () => { //复制成功的回调函数
				uni.$w.ok("复制成功");
			},
			fail: (e) => {
				console.warn(e);
				uni.$w.err("复制失败");
			}
		});
	},

	/**
	 * time毫秒后返回成功的Promise
	 * @param {Number} time 毫秒
	 * @returns 
	 */
	sleep(time) {
		return new Promise((resove, reject) => {
			setTimeout(() => {
				resove();
			}, time);
		});
	},

/**
 * 计算两个经纬度的距离,单位:米
 * @param {Number} lng1 经度1
 * @param {Number} lat1 纬度1
 * @param {Number} lng2 经度2
 * @param {Number} lat2 纬度2
 * @returns 
 */
	distance(lng1,lat1, lng2, lat2) {
		let radLat1 = lat1 * Math.PI / 180.0;
		let radLat2 = lat2 * Math.PI / 180.0;
		let a = radLat1 - radLat2;
		let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
		let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
			Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		return s * 6378137.0;
	},

	/**
	 * 将单位米的距离转换为常见的m,km
	 * @param {Number} num 
	 * @returns 
	 */
	distanceShow(num) {
		if (!num) {
			return "0.0 m"
		}
		if (num < 1000) {
			return num.toFixed(1) + "m"
		} else if (num > 1000) {
			return (num / 1000).toFixed(1) + "km"
		}
	}

};