import Permission from "@/js_sdk/wa-permission/permission"; //权限工具类
import {SelectLocation} from "./authPerission.js"; //权限工具类
const LFv2 = uni.requireNativePlugin("AThree-LFv2");
// 隐藏tabbar
export const hideTabBar = () => {
	uni.hideTabBar();
};

// 显示tabbar
export const showTabBar = () => {
	uni.showTabBar();
};

/**
 *
 * @param {*} type navigateTo | redirectTo | reLaunch | switchTab
 * @param {*} url
 */
export const navigate = (type = "navigateTo", url) => {
	uni[type]({
		url,
	});
};
/**
 * 获取设备基础信息
 * @date 2022-01-06
 * @returns {object}
 */
export const loadDeviceInfo = async (lat, lng, address) => {
	// 网络状态
	const {networkType = ""} = await loadNetwork();
	return new Promise((resolve, reject) => {
		uni.getSystemInfo({
			success: async (result) => {
				const {
					platform,
					deviceId,
					osVersion,
					screenWidth,
					screenHeight,
					appName,
					osName,
					appWgtVersion,
					model,
					deviceBrand,
				} = result;
				const deviceInfo = {
					deviceId: deviceId, //设备指纹ID
					appName: "MMYX", //APP实例
					appCode:6,
					appSourceId: "1",
					device: osName, // 设备类型
					deviceName: platform, // 设备命名
					deviceBand: deviceBrand, // 设备品牌
					token: "", // 设备令牌(device_token)
					appVersion: appWgtVersion, //app版本
					osVersion: osVersion, //操作系统版本
					mobileType: model, // 手机型号
					idfa: "", // IOS设备则为:IDFA
					imei: "", //安卓设备为:imei
					mac: "", // mac
					wifiMac: "", // wifimac
					clientIp: "",
					clientProvince: "", //客户所在省份
					clientCity: "", // 客户所在城市
					lat: lat, // 纬度
					lng: lng, // 经度
					clientRegion: address || "", // 客户所在区域(GPRS或IP所在地信息)
					carrierOperator: "", //运营商
					pushId: "", //push推送ID
					pushType: "", //设备push接入类型(1. 小米PUSH,2. 极光PUSH)
					screenHeight: screenHeight, //屏幕高度
					screenWidth: screenWidth, //屏幕宽度
					resolution: `${screenWidth}*${screenHeight}`, //分辨率
					networkStatus: networkType, //网络
					tId: "", //同盾设备指纹ID
					electricQuantity: "", //电量
					totalStorage: "", //存储总空间
					availableStorage: "", //可用存储空间
				};

				uni.setStorage({
					key: "m-device-info",
					data: JSON.stringify(deviceInfo),
					success: (res) => {
						console.log("m-device-info存储成功");
					},
				});
				return resolve(deviceInfo);
			},
			fail: (err) => {
				console.log('获取系统参数报错')
				return reject(err);
			},
		});
	});
};

/**
 * 人脸验证
 */
export const Face_UP = (token = "", requestId = "", callback) => {
	try {
		LFv2.Face_UP({
				token,
				requestId,
			},
			(res) => {
				console.log("Face_UP", res);
				if (res.code == 1) {
					callback && callback(res);
				} else {
					uni.showToast({
						title: "验证失败",
						icon: "none",
					});
				}
			}
		);
	} catch (error) {
		console.error(error);
	}
};

// 获取common当中的网络信息
export const loadNetwork = () => {
	const that = this;
	return new Promise((resolve, reject) => {
		uni.getNetworkType({
			success: (res) => {
				console.log(res,'哈哈====')
				// 网络信息存储
				uni.setStorage({
					key: "m-network-info",
					data: JSON.stringify(res),
					success: function() {
						console.log("m-network-info存储成功");
					},
				});
				resolve(res);
			},
			fail: (err) => {
				uni.showToast({
					icon: "none",
					title: "获取网络信息失败",
				});
				reject(err);
			},
		});
	});
};

// 获取common当中的位置信息
export const loadLocation = () => {
	const that = this;
	console.log('调用获取定位信息的方法3')
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: "wgs84",
			geocode: true,
			success: async (res) => {
				console.log(res, "成功===");

				const locationInfo = {
					lat: String(res.latitude),
					lng: String(res.longitude),
					address: JSON.stringify(res.address),
				};
				
				// 位置信息存储
				uni.setStorage({
					key: "m-location-info",
					data: JSON.stringify(locationInfo),
					success: function() {
						console.log("m-location-info存储成功");
					},
					fail: (err) => {
						console.error("m-location-info存储失败");
					},
				});

				// 获取设备信息
				const device = await loadDeviceInfo(
					res.latitude,
					res.longitude,
					''
				);
				resolve(device);
			},
			fail: async (err) => {
				console.log(err, "报错信息");
				const hasShowModal = uni.getStorageSync('showModal');
				if (!hasShowModal) {
					uni.setStorageSync('showModal', 'true')
					let hasReject;

					// if (plus.os.name == "iOS") {
					// 	hasReject = Permission.judgeIosPermission('location')
					// } else {
					// 	hasReject = await Permission.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION')
					// }

					// console.log(hasReject, '定位权限返回内容')


					// 这里应该判断用户 拒绝之后 才弹框
					uni.showModal({
						title: '"花花有米"想要访问您的位置信息',
						content: "您手机系统的定位处于关闭状态,会导致您后续的授信不通过,授信需要访问您的位置信息,需要您提供权限,请开启手机的定位服务。",
						success: function(res) {
							if (res.confirm) {
								uni.removeStorageSync('hasRefresh')
								uni.removeStorageSync('showModal')
								Permission.gotoAppPermissionSetting();
							} else if (res.cancel) {
								console.log("用户点击取消");
								uni.removeStorageSync('showModal')
							}
						},
					});


				}
				// 获取设备信息
				reject(err);
			},
		});
	});
};

/**
 * 获取设备信息(https://www.html5plus.org/doc/zh_cn/device.html#plus.device.getInfo)
 * 包括国际移动设备身份码、国际移动用户识别码、设备的唯一标识等信息。
 * Android平台调用此方法需要申请访问设备标识(部分设备提示为拨打电话)权限,详情参考https://ask.dcloud.net.cn/article/36075。
 */
export const getDeviceInfo = () => {
	return new Promise((resolve, reject) => {
		plus.device.getInfo({
			success: (res) => {
				console.log("**************** 设备信息:" + JSON.stringify(res));
				resolve(res);
			},
			fail: (err) => {
				// console.log('**************** 获取设备信息失败: ' + JSON.stringify(err))
				reject(err);
			},
			complete: (e) => {
				// console.log('**************** 获取设备信息complete: ', e)
			},
		});
	});
};