package com.lukouguoji.module_base.util


import android.content.Context
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.luck.picture.lib.style.PictureSelectorStyle
import com.luck.picture.lib.style.SelectMainStyle
import com.luck.picture.lib.style.TitleBarStyle
import com.lukouguoji.module_base.http.net.Api
import com.lukouguoji.module_base.impl.GlideEngine
import com.lukouguoji.module_base.ktx.getActivity
import com.lukouguoji.module_base.ktx.loge
import java.util.ArrayList


/**
 * @author：孟凡华
 * @date：2021/12/10 20:05
 */
object MediaUtil {

    val TEXT_SIZE = 16
    val BAR_HEIGHT = 72

    //==========图片选择===========//
    fun pickImage(
        context: Context,
        //最多选择数
        maxNum: Int = 9,
        //是否有拍照按钮
        isCamera: Boolean = true,
        callBack: (ArrayList<LocalMedia>) -> Unit
    ) {
        val selectMain = SelectMainStyle().apply {
            adapterCameraTextSize = TEXT_SIZE
        }
        val titleBar = TitleBarStyle().apply {
            titleBarHeight = BAR_HEIGHT
            titleTextSize = TEXT_SIZE
            titleCancelTextSize = TEXT_SIZE
        }
        val  style = PictureSelectorStyle().apply {
            titleBarStyle = titleBar
            selectMainStyle = selectMain
        }

        // 预览图片、视频、音频
        PictureSelector.create(context.getActivity())
//            .openSystemGallery(SelectMimeType.ofImage())
            .openGallery(SelectMimeType.ofImage())
            .setImageEngine(GlideEngine.createGlideEngine())
            .setSelectionMode(if (maxNum>1) SelectModeConfig.MULTIPLE else SelectModeConfig.SINGLE)
            .setMaxSelectNum(maxNum)
//            .isDirectReturnSingle(true)
            .setSelectorUIStyle(style)
            .isDisplayCamera(isCamera)
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>?) {
                    result?.let(callBack)
                }

                override fun onCancel() {
                    loge("选择图片 onCancel")
                }
            })
    }

    fun fillUrl(fileName: String) = Api.BASE_URL + "file/getImg/${fileName}"
    fun removeUrl(fileName: String) = fileName.removePrefix(Api.BASE_URL + "file/getImg/")


}
