package com.adealink.weparty.oss.data

import com.adealink.frame.oss.data.UploadFile
import com.adealink.frame.util.ImageUtil
import com.adealink.frame.util.getFileSuffix
import com.google.gson.annotations.SerializedName
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

enum class OssType(val type: Int) {
    AVATAR(1),
    COVER(2),
    ATTACHMENT(3),
    LOG(5),
    VIDEO(6),
    VOICE(7),
    OTHER(8);

    companion object {

    }
}

data class OssUploadReq(
    @SerializedName("type") val type: Int, //类型
    @SerializedName("suffix") val suffix: String //文件后缀
)

data class OssUploadRes(
    @SerializedName("preSignUrl") val uploadUrl: String, //文件上传地址
    @SerializedName("fileUrl") val fileUrl: String //文件远端地址
)


suspend fun UploadFile.LocalFile.toUploadReq(): OssUploadReq {
    return suspendCancellableCoroutine { continuation ->
        when (this.fileType) {
            UploadFile.FileType.IMAGE -> {
                var suffix = fileType.suffix.ifEmpty { getFileSuffix(fileName) ?: "png" }
                suffix = addOssFileSuffix(this, suffix)
                continuation.resume(
                    OssUploadReq(
                        OssType.COVER.type,
                        suffix
                    )
                )
            }

            UploadFile.FileType.VIDEO -> {
                var suffix = fileType.suffix.ifEmpty { getFileSuffix(fileName) ?: "mp4" }
                suffix = addOssFileSuffix(this, suffix)
                continuation.resume(
                    OssUploadReq(
                        OssType.VIDEO.type,
                        suffix
                    )
                )
            }

            UploadFile.FileType.AUDIO -> {
                continuation.resume(
                    OssUploadReq(
                        OssType.VOICE.type,
                        fileType.suffix.ifEmpty { getFileSuffix(fileName) ?: "mp3" }
                    )
                )
            }

            UploadFile.FileType.AVATAR -> {
                var suffix = fileType.suffix.ifEmpty { getFileSuffix(fileName) ?: "png" }
                suffix = addOssFileSuffix(this, suffix)
                continuation.resume(
                    OssUploadReq(
                        OssType.AVATAR.type,
                        suffix
                    )
                )
            }

            UploadFile.FileType.ATTACHMENT -> {
                var suffix = fileType.suffix.ifEmpty { getFileSuffix(fileName) ?: "png" }
                suffix = addOssFileSuffix(this, suffix)
                continuation.resume(
                    OssUploadReq(
                        OssType.ATTACHMENT.type,
                        suffix
                    )
                )
            }

            UploadFile.FileType.LOG -> {
                continuation.resume(
                    OssUploadReq(
                        OssType.LOG.type,
                        fileType.suffix.ifEmpty { getFileSuffix(fileName) ?: "zip" }
                    )
                )
            }

            UploadFile.FileType.OTHER -> {
                var suffix = fileType.suffix.ifEmpty { getFileSuffix(fileName) ?: "png" }
                suffix = addOssFileSuffix(this, suffix)
                continuation.resume(
                    OssUploadReq(
                        OssType.OTHER.type,
                        suffix
                    )
                )
            }
        }
    }
}

private fun addOssFileSuffix(file: UploadFile.LocalFile, suffix: String): String {
    return if (UploadFile.FileType.isImage(suffix)) {
        ImageUtil.getPictureSize(file.path).let {
            "${it.first}x${it.second}.${suffix}"
        }

    } else if (UploadFile.FileType.isVideo(suffix)) {
        ImageUtil.getVideoSize(file.path).let {
            "${it.first}x${it.second}.${suffix}"
        }

    } else {
        suffix
    }
}