package com.adealink.weparty.imageselect

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import com.adealink.weparty.imageselect.clip.ClipParamData
import com.adealink.weparty.imageselect.clip.MesTipData
import com.adealink.weparty.imageselect.model.LocalMedia
import com.adealink.weparty.imageselect.model.MediaType

data class SelectImageRequest(
    val maxNum: Int = 1,
    val source: String = "",
    val clipParamData: ClipParamData? = null, //裁剪参数，为空则跳过图片裁剪
    val mediaType: MediaType = MediaType.IMAGE,
    val gifOpt: Boolean? = false,//是否需要选择GIF图片
    val mesTip: MesTipData? = null,//显示gif图片的文字信息,
    val coverExpireTime: Long? = null,
    val fromPosition: Int? = null, //列表点击来源的位置
    val selectedLocalMedia: List<LocalMedia>? = null, //已选择图片列表
    val videoMaxSecond: Int? = null, //视频最长秒数限制
    val imageMinSize: Int? = null, //图片最小尺寸限制,宽度高度
    val topTipsString: CharSequence? = null //顶部提示文字,支持富文本
) {
    companion object {
        const val SOURCE_EDIT_PROFILE_COVER = "3" //用户资料页封面
    }
}

data class SelectImageResult(
    val source: String,
    val resultCode: Int,
    val path: String?,
    val uri: String?,
    val mediaType: MediaType,
    val mediaInfos: List<LocalMedia>? = null,
    val clipParamData: ClipParamData? = null,
    val fromPosition: Int? = null //列表点击来源的位置
)

class SelectImageContract : ActivityResultContract<SelectImageRequest, SelectImageResult>() {


    override fun createIntent(context: Context, input: SelectImageRequest): Intent {
        return Intent(context, ImageSelectActivity::class.java).apply {
            putExtra(ImageSelectActivity.KEY_SOURCE, input.source)
            putExtra(ImageSelectActivity.KEY_MAX_NUM, input.maxNum)
            putExtra(ImageSelectActivity.KEY_CLIP_PARAM_DATA, input.clipParamData)
            putExtra(ImageSelectActivity.KEY_MEDIA_TYPE, input.mediaType)
            putExtra(ImageSelectActivity.KEY_GIF_OPTION, input.gifOpt)
            putExtra(ImageSelectActivity.SHOW_MES_TIP, input.mesTip)
            putExtra(ImageSelectActivity.COVER_EXPIRE_TIME, input.coverExpireTime)
            putExtra(ImageSelectActivity.KEY_FROM_POSITION, input.fromPosition)
            putParcelableArrayListExtra(ImageSelectActivity.KEY_SELECTED_MEDIA_LIST, input.selectedLocalMedia as? ArrayList)
            putExtra(ImageSelectActivity.KEY_VIDEO_MAX_SECOND, input.videoMaxSecond)
            putExtra(ImageSelectActivity.KEY_IMAGE_MIN_SIZE,input.imageMinSize)
            putExtra(ImageSelectActivity.TOP_TIPS_STRING, input.topTipsString)
        }
    }

    override fun parseResult(resultCode: Int, intent: Intent?): SelectImageResult {
        val source: String =
            intent?.getStringExtra(ImageSelectActivity.KEY_SOURCE) ?: ""
        val mediaType =
            (intent?.getSerializableExtra(ImageSelectActivity.KEY_MEDIA_TYPE) as? MediaType)
                ?: MediaType.ALL
        val selectedImagePath: String? =
            intent?.getStringExtra(ImageSelectActivity.SELECT_IMAGE_PATH)
        val selectedImageUri: String? =
            intent?.getStringExtra(ImageSelectActivity.SELECT_IMAGE_URI)
        val mediaInfos =
            intent?.getParcelableArrayListExtra<LocalMedia>(ImageSelectActivity.SELECT_IMAGES)
        val fromPosition = intent?.getIntExtra(ImageSelectActivity.KEY_FROM_POSITION, -1)
        return SelectImageResult(
            source,
            resultCode,
            if (resultCode == Activity.RESULT_OK) selectedImagePath else null,
            if (resultCode == Activity.RESULT_OK) selectedImageUri else null,
            mediaType,
            mediaInfos,
            fromPosition = fromPosition
        )
    }

}