import React, { Component } from 'react';
import {
    PermissionsAndroid,
    NativeModules,
    Dimensions,
} from 'react-native';
import ImagePicker from "react-native-syan-image-picker"
import { isIos, toast } from '../../utils/base';

export interface State {
    imageList: Array<any>
}
export interface Props {
    // type: 1 | 2 | 3
}
import RNFS from 'react-native-fs';



//图片选择器

//图片选择器参数设置


export default class ImagePikcker {

    constructor() {
    }
    static openCaraimer = async (imageNum?: number) => {
        if (!isIos()) {
            await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA);
        };
        let imgList = []

        const options = {
            // imageCount: imageNum ? imageNum : 6,
            minimumCompressSize: 100 * 1024,
            title: '请选择图片来源',
            cancelButtonTitle: '取消',
            takePhotoButtonTitle: '拍照',
            chooseFromLibraryButtonTitle: '相册图片',
        };
        await ImagePicker.asyncOpenCamera(options).then(photos => {
            console.log("photos", photos)
            imgList = photos
        })
            .catch(err => {
                // 取消选择，err.message为"取消"
                console.log("取消选择", err)
                // console.log("现在的数据是：", this.state.imageList)
                // return []

            })
        return imgList


    }

    static chooseImage = async (imageNum?: number, isCrop?: boolean) => {
        if (!isIos()) {
            await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA);
        };
        let imgList = []
        const options = {
            minimumCompressSize: 100,
            compress: true,
            isGif: false,
            title: '请选择图片来源',
            imageCount: imageNum ? imageNum : 6,
            cancelButtonTitle: '取消',
            takePhotoButtonTitle: '拍照',
            chooseFromLibraryButtonTitle: '相册图片',
            showSelectedIndex: true,
            isCamera: true,            // 是否允许用户在内部拍照，默认true
            isCrop: isCrop ? isCrop : false,             // 是否允许裁剪，默认false, imageCount 为1才生效        
            freeStyleCropEnabled: true,
            showCropFrame: true,       // 是否显示裁剪区域，默认true
            // rotateEnabled: true,       // 裁剪是否可旋转图片
            scaleEnabled: true,        // 裁剪是否可放大缩小图片
            showCropGrid: true //裁剪显示网格
        };
        await ImagePicker.asyncShowImagePicker(options)
            .then(photos => {
                // console.log("photos", photos)
                imgList = photos
            })
            .catch(err => {
                // 取消选择，err.message为"取消"
                console.log("取消选择", err)
                // console.log("现在的数据是：", this.state.imageList)
                // return []

            })
        return imgList

    }

    static onPressVideo = async (callBack: Function) => {
        if (!isIos()) {
            await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA);
        };
        let imgList = []
        const options = {
            imageCount: 1,
            title: '请选择视频来源',
            cancelButtonTitle: '取消',
            takePhotoButtonTitle: '拍摄',
            chooseFromLibraryButtonTitle: '相册视频',
        };
        ImagePicker.openVideoPicker(options, (err, photos) => {
            if (err) {
                // 取消选择
                console.log("openVideoPicker:", err)
            } else {
                try {
                    imgList = photos
                    // console.log("imgList0:", imgList)
                } catch (error) {
                    console.log("openVideoPicker1", err)
                }
            }
            // console.log("imgList1:", imgList)
            return callBack(imgList)
        })

    }
    static onCreateVideo = async (callBack: Function) => {
        if (!isIos()) {
            await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA);
        };
        console.log("哈哈哈哈")
        let imgList = []
        const options = {
            imageCount: 1,
            videoMaximumDuration: 20,
            isCamera: true,
            title: '请选择视频来源',
            cancelButtonTitle: '取消',
            takePhotoButtonTitle: '拍摄',
            chooseFromLibraryButtonTitle: '录制视频',
        };
        // ImagePicker.openVideo(options, (err, photos) => {
        //     if (err) {
        //         // 取消选择
        //         console.log("openVideoPicker:", err)
        //     } else {
        //         try {
        //             imgList = photos
        //             // console.log("imgList0:", imgList)
        //         } catch (error) {
        //             console.log("openVideoPicker1", err)
        //         }
        //     }
        //     // console.log("imgList1:", imgList)
        //     return callBack(imgList)
        // })
        // await ImagePicker.asyncOpenCamera(options).then(photos => {
        //     console.log("photos", photos)
        //     imgList = photos
        // })
        //     .catch(err => {
        //         // 取消选择，err.message为"取消"
        //         console.log("取消选择", err)
        //         // console.log("现在的数据是：", this.state.imageList)
        //         // return []

        //     })
        // return imgList
    }
}

