package com.dahe.mylibrary.pop

import android.content.Context
import android.widget.Button
import com.dahe.mylibrary.R
import com.dahe.mylibrary.adapter.GridImageAdapter
import com.dahe.mylibrary.callback.OnPicResultListener
import com.dahe.mylibrary.weight.GlideEngine
import com.dahe.mylibrary.weight.ImageFileCompressEngine
import com.luck.picture.lib.basic.PictureSelectionCameraModel
import com.luck.picture.lib.basic.PictureSelectionModel
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.lxj.xpopup.core.BottomPopupView

/**
 * @ClassName PopBottomPic
 * @Author john
 * @Date 2024/1/31 11:11
 * @Description TODO
 */
class PopNorBottomPic(
    context: Context,
    onPicResultListener: OnPicResultListener
) : BottomPopupView(context) {


    private var listener: OnPicResultListener

    init {
        this.listener = onPicResultListener
    }


    override fun getImplLayoutId() = R.layout.choice_norcar_img

    override fun onCreate() {
        super.onCreate()
        var paizhao = findViewById<Button>(R.id.btn_paizhao)
        var xiangce = findViewById<Button>(R.id.btnXiangce)
        var btnQuxiao = findViewById<Button>(R.id.btn_quxiao)
        paizhao.setOnClickListener {

            // 单独拍照
            val cameraModel: PictureSelectionCameraModel = PictureSelector.create(context)
                .openCamera(SelectMimeType.TYPE_IMAGE)
//                .setCameraInterceptListener(getCustomCameraEvent())//自定义相机
//                .setRecordAudioInterceptListener(MainActivity.MeOnRecordAudioInterceptListener())//录音回调事件
//                .setCropEngine(ImageFileCropEngine())//裁剪
//                .setCompressEngine(ImageFileCompressEngine())//压缩

//                .setSelectLimitTipsListener(MainActivity.MeOnSelectLimitTipsListener())//拦截自定义提示
//                .setAddBitmapWatermarkListener(getAddBitmapWatermarkListener())//给图片添加水印
//                .setVideoThumbnailListener(getVideoThumbnailEventListener())//处理视频缩略图
//                .setCustomLoadingListener(getCustomLoadingListener())//自定义loading
//                .setSandboxFileEngine(MainActivity.MeSandboxFileEngine())//自定义沙盒文件处理
//                .isOriginalControl(cb_original.isChecked())//是否开启原图功能
//                .setPermissionDescriptionListener(getPermissionDescriptionListener())//权限说明
//                .setOutputAudioDir(getSandboxAudioOutputPath())//创建音频自定义输出目录
//                .setSelectedData(myAdapter.data)
            cameraModel.forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>?) {
                    if (listener != null) {
                        result?.let { it1 -> listener.onResult(it1) }
                    }
                    dismiss()
                }

                override fun onCancel() {
                    dismiss()
                }
            })

        }

        xiangce.setOnClickListener {

            // 进入相册
            val selectionModel: PictureSelectionModel = PictureSelector.create(context)
                .openGallery(SelectMimeType.TYPE_IMAGE)
                .isDisplayCamera(false)//关闭拍照
                .setImageEngine(GlideEngine.createGlideEngine())
                .setCompressEngine(ImageFileCompressEngine())
                .isPreviewImage(true)
                .isMaxSelectEnabledMask(true)
                .setMaxSelectNum(1)
                .setSelectionMode(SelectModeConfig.SINGLE)
                .isGif(false)
//                .isDirectReturnSingle(true)
//                .setSelectedData(myAdapter.data)
            selectionModel.forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>) {
                    if (listener != null) {
                        result?.let { it1 -> listener.onResult(it1) }
                    }
                    dismiss()
                }

                override fun onCancel() {
                    dismiss()
                }
            })
        }

        btnQuxiao.setOnClickListener {
            dismiss()
        }

    }

    private fun openCam() {
        PictureSelector.create(context)
            .openCamera(SelectMimeType.TYPE_IMAGE)
    }


}