package com.quansu.heifengwuliu.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.quansu.heifengwuliu.config.MIntentAction;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

/**
 * 拍照/照片
 * Created by shihuiyun
 * on 2020/9/16
 */
public class ChosePhotoUtils {

    public static void getChosePhoto(Context context, int maxPics){

        new QMUIBottomSheet.BottomListSheetBuilder(context)
                .addItem("拍照")
                .addItem("照片")
                .setAllowDrag(true)
                .setGravityCenter(true)
                .setAddCancelBtn(true)
                .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                    @Override
                    public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {

                        if (position == 0) {
                            AndPermission.with(context)
                                    .runtime()
                                    .permission(
                                            Permission.CAMERA,
                                            Permission.WRITE_EXTERNAL_STORAGE,
                                            Permission.READ_EXTERNAL_STORAGE)
                                    .onGranted(permissions -> {

//                                        ImageSelectorUtils.showCameraAction((Activity) context, C.REQUEST_CODE_CAMERA);

                                        PictureSelector.create((Activity) context)
                                                .openCamera(PictureMimeType.ofImage())
                                                .imageEngine(LoadEngin.INSTANCE) // Please refer to the Demo GlideEngine.java
                                                .forResult(MIntentAction.REQUEST_CODE_CAMERA);

                                        dialog.dismiss();


                                    })
                                    .onDenied(permissions -> {
                                        dialog.dismiss();
                                    })
                                    .start();
                        } else {

                            AndPermission.with(context)
                                    .runtime()
                                    .permission(
                                            Permission.CAMERA,
                                            Permission.WRITE_EXTERNAL_STORAGE,
                                            Permission.READ_EXTERNAL_STORAGE)
                                    .onGranted(permissions -> {

                                        //照片
                                        PictureSelector.create((Activity) context)
                                        .openGallery(PictureMimeType.ofImage())
                                                .imageEngine( LoadEngin.INSTANCE)// 外部传入图片加载引擎，必传项
                                                .isMaxSelectEnabledMask(true)// 选择数到了最大阀值列表是否启用蒙层效果
                                                .maxSelectNum(maxPics)
                                                .selectionMode(PictureConfig.MULTIPLE)
                                                .isPreviewImage(true)// 是否可预览图片
                                                .isCompress(false)// 是否压缩
                                                .forResult(MIntentAction.REQUEST_CODE_IMG);
                                        dialog.dismiss();


                                    })
                                    .onDenied(permissions -> {
                                        dialog.dismiss();
                                    })
                                    .start();

                        }


                    }
                })
                .build()
                .show();



    }
}
