package com.casic.dcms.utils;

import android.content.Context;
import android.graphics.Color;

import com.casic.dcms.utils.callback.ISelectorListener;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.widget.dialog.BottomActionSheet;

import java.util.ArrayList;
import java.util.Arrays;

public class PictureSelectorHelper {
    //选图
    public static void selectPicture(Context context, int maxNum, ISelectorListener selectorListener) {
        if (context == null) {
            return;
        }
        new BottomActionSheet.Builder()
                .setContext(context)
                .setItemTextColor(Color.BLUE)
                .setActionItemTitle(Arrays.asList("拍照", "相册"))
                .setOnActionSheetListener(new BottomActionSheet.OnActionSheetListener() {
                    @Override
                    public void onActionItemClick(int position) {
                        switch (position) {
                            case 0:
                                PictureSelector.create(context)
                                        .openCamera(SelectMimeType.ofImage())
                                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                                            @Override
                                            public void onResult(ArrayList<LocalMedia> result) {
                                                if (result == null) {
                                                    StringKit.show(context, "拍照保存失败，请重试");
                                                    return;
                                                }
                                                selectorListener.onSuccess(result);
                                            }

                                            @Override
                                            public void onCancel() {

                                            }
                                        });
                                break;
                            case 1:
                                PictureSelector.create(context)
                                        .openGallery(SelectMimeType.ofImage())
                                        .isGif(false)
                                        .isMaxSelectEnabledMask(true)
                                        .setFilterMinFileSize(100)
                                        .setMaxSelectNum(maxNum)
                                        .isDisplayCamera(false)
                                        .setImageEngine(GlideLoadEngine.createGlideEngine())
                                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                                            @Override
                                            public void onResult(ArrayList<LocalMedia> result) {
                                                if (result == null) {
                                                    StringKit.show(context, "选择照片失败，请重试");
                                                    return;
                                                }
                                                selectorListener.onSuccess(result);
                                            }

                                            @Override
                                            public void onCancel() {

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

    //拍照
    public static void takePicture(Context context, ISelectorListener selectorListener) {
        if (context == null) {
            return;
        }
        PictureSelector.create(context)
                .openCamera(SelectMimeType.ofImage())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        if (result == null) {
                            StringKit.show(context, "拍照保存失败，请重试");
                            return;
                        }
                        selectorListener.onSuccess(result);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    //拍照
    public static void takeVideo(Context context, ISelectorListener selectorListener) {
        if (context == null) {
            return;
        }
        PictureSelector.create(context)
                .openCamera(SelectMimeType.ofVideo())
                .setRecordVideoMaxSecond(15)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        if (result == null) {
                            StringKit.show(context, "拍照保存失败，请重试");
                            return;
                        }
                        selectorListener.onSuccess(result);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }
}
