package com.base.browserwhite.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.browserwhite.R
import com.base.browserwhite.bean.DownloadBean
import com.base.browserwhite.databinding.ItemDownloadBinding
import com.base.browserwhite.databinding.ItemDownloadCardBinding
import com.base.browserwhite.databinding.ItemDownloadTimeBinding
import com.base.browserwhite.utils.KotlinExt.toFormatSize
import com.base.browserwhite.utils.LogEx
import com.base.browserwhite.utils.VideoDownloader
import com.base.browserwhite.utils.XmlEx.inflate
import com.bumptech.glide.Glide
import com.chad.library.adapter4.BaseQuickAdapter
import com.downloader.PRDownloader
import com.downloader.Status
import com.downloader.internal.DownloadRequestQueue
import java.io.File


class DownloadAdapter : BaseQuickAdapter<DownloadBean, DownloadAdapter.DownloadViewHolder>() {

    private val TAG = "DownloadAdapter"

    inner class DownloadViewHolder(view: View) : ViewHolder(view)

    var downloadAction: ((item: DownloadBean) -> Unit)? = null

    override fun onBindViewHolder(holder: DownloadViewHolder, position: Int, item: DownloadBean?) {
        if (item == null) return
        val context = holder.itemView.context
        if (item.isTime) {
            val binding = ItemDownloadTimeBinding.bind(holder.itemView)
            binding.tvTime.text = item.time
        } else if (item.uiType == 1) {
            val binding = ItemDownloadBinding.bind(holder.itemView)
            val file = File(item.path)
            Glide.with(context).load(file.absoluteFile).centerCrop().into(binding.iv)
            binding.tvName.text = file.name
            binding.tvSize.text = file.length().toFormatSize()


        } else if (item.uiType == 2) {
            val binding = ItemDownloadCardBinding.bind(holder.itemView)
            Glide.with(context).load(item.uri).centerCrop().into(binding.iv)
            binding.tvName.text = if (item.name.isNotEmpty()) item.name else item.uri.split("/").last()

            binding.ivDownload.isVisible = item.downloadId == -1
            binding.flDownload.isVisible = item.state != Status.UNKNOWN
            binding.ivFinish.isVisible = false

            when (item.state) {
                Status.PAUSED -> {
                    binding.ivXiazaiZantin.setImageResource(R.mipmap.zanting_download)
                    binding.circularProgressBar.progress = item.progress.toFloat()
                }

                Status.RUNNING -> {
                    binding.ivXiazaiZantin.setImageResource(R.mipmap.xiazhaiz_download)
                    binding.circularProgressBar.progress = item.progress.toFloat()
                }

                Status.COMPLETED -> {
                    binding.flDownload.isVisible = false
                    binding.ivFinish.isVisible = true
                }

                else -> {

                }
            }

            binding.ivDownload.setOnClickListener {
                downloadAction?.invoke(item)
            }
            binding.flDownload.setOnClickListener {
                val status: Status = PRDownloader.getStatus(item.downloadId)
                if (status == Status.PAUSED) {
                    PRDownloader.resume(item.downloadId)
                }
                if (status == Status.RUNNING) {
                    PRDownloader.pause(item.downloadId)
                }
            }

        }
    }

    override fun getItemViewType(position: Int, list: List<DownloadBean>): Int {
        val bean = list[position]
        return if (bean.isTime) 0 else bean.uiType

    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DownloadViewHolder {
        val layout = when (viewType) {
            0 -> R.layout.item_download_time
            1 -> R.layout.item_download
            2 -> R.layout.item_download_card
            else -> 0
        }
        return DownloadViewHolder(layout.inflate(parent))
    }

    @SuppressLint("NotifyDataSetChanged")
    fun downloadItem(bean: DownloadBean) {
        val pair = VideoDownloader.downloadVideo2(
            context, bean.uri, bean.name,
            progressAction = { progress ->
                LogEx.logDebug(TAG, "progress=$progress")
                bean.progress = progress
                val status: Status = PRDownloader.getStatus(bean.downloadId)
                bean.state = status
                notifyDataSetChanged()
            },
            finishAction = {
                val status: Status = PRDownloader.getStatus(bean.downloadId)
                bean.state = status
                LogEx.logDebug(TAG, "status=$status")
                notifyDataSetChanged()
            }
        )
        bean.downloadId = pair.first
        bean.path = pair.second.absolutePath
    }

}