// 检查通讯录权限
import Permission from "@/js_sdk/wa-permission/permission";
import {nativeCommon} from "./nativeCommon.js";

// 通讯录拒绝后弹框
const onPermissionModal = function () {
    uni.showModal({
        title: '"多典花"想访问您的通讯录',
        content: '通过访问您的通讯录信息，添加联系人，提高授信通过率。',
        cancelText: '取消',
        confirmText: '去设置',
        success: function (res) {
            if (res.confirm) {
                Permission.gotoAppPermissionSetting()
            }
        }
    });
};

// 检测通讯录权限
export const CheckContactPermission = function (callback) {
    // 获取通讯录对象
    plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function (addressBook) {
        // 查找联系人
        addressBook.find(["displayName", "phoneNumbers"], function (contacts) {
			//此处的缓存是为了解决ios中的相关问题
			uni.setStorage({
			              key: "m-contact",
			              data: JSON.stringify(contacts),
			              success: function () {
			               console.log('缓存成功')
			              }
			})
            callback && callback(contacts)
        })
    }, function (e) {
        // 如果授权『不允许』就弹框
        onPermissionModal();
    });
}

// 通讯录选择&设置
export const SelectContact = async function (callback) {
    // 已经授权了
    const that = this;
    const platform = uni.getSystemInfoSync();
    if (platform.osName === 'ios') {
        uni.navigateTo({
            url: "/pages/contact/contact",
            events: {
                send(data) {
                    try {
                        const result = JSON.parse(data)
                        callback && callback(result)
                    } catch (err) {
                        console.log('处理通讯录选择的内容报错')
                    }
                },
            }
        })
    } else {
        try {
            const rContacts = await Permission.requestAndroidPermission('android.permission.READ_CONTACTS');
            let strStatus
            if (rContacts === 1) {
                nativeCommon.contacts.getContact(function (name, phoneNumber) {
                    const contact = {
                        linkerName: name,
                        linkerMobile: phoneNumber
                    }
                    callback && callback(contact)
                })
            } else if (rContacts === 0) {
                strStatus = "未获得授权"
            } else if (rContacts === -1) {
                onPermissionModal();
            }
        } catch (err) {
            console.log('校验通讯录权限异常')
        }
    }
}

// 检测位置权限
export const CheckLocationPermission = function (callback) {
    plus.geolocation.getCurrentPosition(function (res) {
		console.log(res,'iooo')
        callback && callback(res)
    }, function (err) {
        console.log(err, '走这里了？')
        uni.showModal({
            title: '"多典花"想要访问您的位置信息',
            content: '应用为您提供的服务，要访问您的位置信息，需要您提供权限，请允许。',
            success: function (res) {
                if (res.confirm) {
                    Permission.gotoAppPermissionSetting()
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
            }
        });
    })
}

// 位置选择
export const SelectLocation = async function (callback) {
    const platform = uni.getSystemInfoSync();
    if (platform.osName === 'ios') {
        if (Permission.judgeIosPermission('location')) {
            console.log('进入这里了')
            callback && callback()
        } else {
            CheckLocationPermission(callback)
        }
    } else {
        try {
            const rContacts = await Permission.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
            if (rContacts === 1) {
                callback && callback()
            } else if (rContacts === 0) {
                console.log("未获得授权")
            } else if (rContacts === -1) {
                uni.showModal({
                    title: '"多典花"想要访问您的位置信息',
                    content: '应用为您提供的服务，要访问您的位置信息，需要您提供权限，请允许。',
                    success: function (res) {
                        if (res.confirm) {
                            Permission.gotoAppPermissionSetting()
                        } else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                });
            }
        } catch (err) {
            console.log('校验手机位置权限异常')
        }
    }
}

