package com.base.browserwhite.utils

import android.app.DownloadManager
import android.app.DownloadManager.Request.NETWORK_WIFI
import android.content.Context
import android.net.Uri
import android.os.Environment
import com.base.browserwhite.R
import com.downloader.Error
import com.downloader.OnDownloadListener
import com.downloader.PRDownloader
import com.downloader.internal.DownloadTask
import java.io.File


object VideoDownloader {

    //https://assets.mixkit.co/videos/4702/4702-720.mp4
    fun downloadVideo(context: Context, uri: String, name: String): Pair<Long, File> {
        val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        var fileName = uri.split("/").last()
        if (name.isNotEmpty()) {
            fileName = name
        }


        // 创建一个DownloadManager.Request对象
        val request = DownloadManager.Request(Uri.parse(uri))

        // 设置下载的标题和描述
        request.setTitle(fileName)
        request.setDescription("Downloading $fileName")
        request.setAllowedNetworkTypes(NETWORK_WIFI)

        // 设置通知栏的可见性
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)

        // 设置下载的文件类型
        request.setMimeType("video/mp4")

        // 设置下载文件的存储位置
        val subPath = context.resources.getString(R.string.app_name) + "/$fileName"
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subPath)

        // 设置是否允许漫游
        request.setAllowedOverRoaming(false)

        // 将下载请求加入下载队列
        val downloadId = downloadManager.enqueue(request)

        val filePath = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), subPath)

        return Pair(downloadId, filePath)
    }

    fun downloadVideo2(
        context: Context,
        url: String,
        name: String,
        progressAction: (progress: Int) -> Unit,
        finishAction: () -> Unit
    ): Pair<Int, File> {
        var fileName = url.split("/").last()
        if (name.isNotEmpty()) {
            fileName = name
        }
        // 设置下载文件的存储位置
        val dirPath = File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
            context.resources.getString(R.string.app_name)
        ).absolutePath

        val downloadId = PRDownloader.download(url, dirPath, fileName)
            .build()
            .setOnStartOrResumeListener { }
            .setOnPauseListener { }
            .setOnCancelListener { }
            .setOnProgressListener {
                val percent = it.currentBytes * 100 / it.totalBytes
                progressAction.invoke(percent.toInt())
            }.start(object : OnDownloadListener {
                override fun onDownloadComplete() {
                    finishAction.invoke()
                }

                override fun onError(p0: Error?) {
                    finishAction.invoke()
                }
            })
        val filePath = File(dirPath, fileName)

        return Pair(downloadId, filePath)
    }
}