package com.rc.core.util

import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import java.io.File
import java.util.*

/**
 * info
 *
 * @author ReiChin_
 */
object MediaUtils {

    /**
     * 创建一条图片地址uri,用于保存拍照后的照片
     *
     * @param context
     * @param cameraFileName
     * @param mimeType
     * @return 图片的uri
     */
    fun createImageUri(
        context: Context,
        cameraFileName: String,
        mimeType: String? = "image/jpeg"
    ): Uri? {
        // ContentValues是我们希望这条记录被创建时包含的数据信息
        val values = ContentValues()

        val suffix = cameraFileName.substring(cameraFileName.lastIndexOf("."))
        val fileName = cameraFileName.replace(suffix, "")
        values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            values.put(MediaStore.Images.Media.DATE_TAKEN, "${System.currentTimeMillis()}")
        }
        values.put(MediaStore.Images.Media.MIME_TYPE, mimeType)

        // 判断是否有SD卡,优先使用SD卡存储,当没有SD卡时使用手机存储
        return if (Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                values.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/Camera")
            }
            context.applicationContext.contentResolver
                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
        } else {
            context.applicationContext.contentResolver
                .insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values)
        }
    }

    /**
     * 创建一条视频地址uri,用于保存录制的视频
     *
     * @param context
     * @param cameraFileName
     * @param mimeType
     * @return 视频的uri
     */
    fun createVideoUri(
        context: Context,
        cameraFileName: String,
        mimeType: String? = "video/mp4"
    ): Uri? {
        // ContentValues是我们希望这条记录被创建时包含的数据信息
        val values = ContentValues()

        val suffix = cameraFileName.substring(cameraFileName.lastIndexOf("."))
        val fileName = cameraFileName.replace(suffix, "")
        values.put(MediaStore.Video.Media.DISPLAY_NAME, fileName)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            values.put(MediaStore.Video.Media.DATE_TAKEN, "${System.currentTimeMillis()}")
        }
        values.put(MediaStore.Video.Media.MIME_TYPE, mimeType)

        // 判断是否有SD卡,优先使用SD卡存储,当没有SD卡时使用手机存储
        return if (Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                values.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_MOVIES)
            }
            context.applicationContext.contentResolver
                .insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
        } else {
            context.applicationContext.contentResolver
                .insert(MediaStore.Video.Media.INTERNAL_CONTENT_URI, values)
        }
    }

    /**
     * delete camera PATH
     *
     * @param context Context
     * @param cameraPath Camera url
     */
    fun deleteCamera(context: Context, cameraPath: String?) {
        try {
            if (null != cameraPath && cameraPath.startsWith("content://")) {
                context.contentResolver.delete(Uri.parse(cameraPath), null, null)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun deleteFile(filepath: String?) {
        if (!filepath.isNullOrEmpty()) {
            try {
                File(filepath).delete()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 创建文件
     *
     * @param context 上下文
     * @param fileName 文件名
     * @param outCameraDirectory 输出目录
     * @return
     */
    fun createCameraFile(
        context: Context,
        fileName: String,
        outCameraDirectory: String? = null
    ): File {
        val folderDir: File
        if (outCameraDirectory.isNullOrEmpty()) {
            // 外部没有自定义拍照存储路径使用默认
            val rootDir: File
            if (Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()) {
                rootDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                folderDir = File(rootDir.absolutePath + File.separator + "Camera" + File.separator)
            } else {
                rootDir =
                    context.applicationContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!
                folderDir = File(rootDir.absolutePath + File.separator)
            }
            if (!rootDir.exists()) {
                rootDir.mkdirs()
            }
        } else {
            // 自定义存储路径
            folderDir = File(outCameraDirectory)
            if (!Objects.requireNonNull(folderDir.parentFile).exists()) {
                folderDir.parentFile?.mkdirs()
            }
        }
        if (!folderDir.exists()) {
            folderDir.mkdirs()
        }

        return File(folderDir, fileName)
    }
}