import { ActionSheet, Modal } from "@ant-design/react-native";
import { getDocumentAsync } from 'expo-document-picker';
import { launchCameraAsync, launchImageLibraryAsync, requestCameraPermissionsAsync, requestMediaLibraryPermissionsAsync } from 'expo-image-picker';
import { Platform } from "react-native";
import { openSystemSettings } from "./os";
const takePhoto = async () => {
    const permission = await requestCameraPermissionsAsync();

    if (!permission.granted) {
        Modal.alert("请允许相册权限", "如果点击“确认“按钮后没有跳转，请自己前往系统设置开启相关权限", [
            {
                text: "确认",
                onPress: openSystemSettings,
            }
        ]);
        // 跳转到 ios/android 相关设置页面
        return;
    }
    const result = await launchCameraAsync({
        allowsEditing: false, // 是否允许裁剪
        quality: 0.9,        // 照片质量
    });

    if (!result.canceled) {
        // 拿到拍照后的图片 URI
        alert(result.assets[0].uri);
    }
};

const picImg = async () => {
    const permission = await requestMediaLibraryPermissionsAsync();

    if (!permission.granted) {
        Modal.alert("请允许相册权限", "如果点击“确认“按钮后没有跳转，请自己前往系统设置开启相关权限", [
            {
                text: "确认",
                onPress: openSystemSettings,
            }
        ]);
        // 跳转到 ios/android 相关设置页面
        return;
    }

    let result = await launchImageLibraryAsync({
        mediaTypes: ['images'], // 只选图片
        allowsEditing: false,
        quality: .9, // 质量 0~1
    });

    if (!result.canceled) {
        alert(result.assets[0].uri);
    }
};

const pickDoc = async () => {
    try {
        let result = await getDocumentAsync({
            type: [
                // PDF
                'application/pdf',

                // Word 文档
                'application/msword',
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',

                // Excel 表格
                'application/vnd.ms-excel',
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',

                // WPS 文字 / WPS 表格
                'application/wps-office.doc',
                'application/wps-office.docx',
                'application/wps-office.xls',
                'application/wps-office.xlsx',
                'application/wps-office.et',
                'application/wps-office.wps',

                // 纯文本
                'text/plain',

                // HTML
                'text/html',

                // 全部图片（jpg/png/gif/webp 等）
                'image/*'
            ],
            copyToCacheDirectory: true, // 复制到应用缓存目录
        });

        if (!result.canceled) {
            alert(result.assets[0]);
        }
    } catch (err) {
        console.warn(err);
    }
};

export const selectCredit = async () => {
    ActionSheet.showActionSheetWithOptions({
        title: '文件来源',
        message: '以下列方式选择一个图片或文档立即提交分析',
        cancelButtonIndex: Platform.OS === 'ios' ? 3 : 2,
        options: Platform.OS === 'ios' ? ['拍照', '文件', '相册', '取消'] : ['拍照', '本地', '取消']
    }, (idx) => {
        if (idx === 0) {
            takePhoto();
        }
        if (idx === 1) {
            pickDoc();
        }
        if (idx === 2) {
            Platform.OS === 'ios' && picImg();
        }
    });
};
