package com.base.browserwhite.ui.adapter

import android.content.ClipData.Item
import android.content.Context
import android.view.View
import android.view.ViewGroup
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.ItemDownloadTimeBinding
import com.base.browserwhite.utils.KotlinExt.toFormatSize
import com.base.browserwhite.utils.KotlinExt.toFormatTime
import com.base.browserwhite.utils.XmlEx.inflate
import com.bumptech.glide.Glide
import com.chad.library.adapter4.BaseQuickAdapter
import java.io.File

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

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

    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 {
            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()
        }
    }

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

    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DownloadViewHolder {
        val layout = if (viewType == 0) R.layout.item_download_time else R.layout.item_download
        return DownloadViewHolder(layout.inflate(parent))
    }

}