package rallyedto.dumpedto.editionedto

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.datarecovery.R
import com.base.datarecovery.databinding.WhisperededtoBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import rallyedto.battleedto.acresedto.Fireplaceedto.toFormatSize
import rallyedto.farewelledto.Strategicedto.inflate
import rallyedto.policeedto.huntedto.Deckedto
import rallyedto.talledto.blewedto.Treasureredto.dpToPx

class Bindedto(val click: (bean: Deckedto) -> Unit) : RecyclerView.Adapter<Bindedto.Whats>() {

    private val beanList = arrayListOf<Deckedto>()

    class Whats(view: View) : ViewHolder(view) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Whats {
        return Whats(R.layout.whisperededto.inflate(parent))
    }

    override fun getItemCount(): Int {
        return beanList.size
    }

    override fun onBindViewHolder(holder: Whats, position: Int) {
        val binding = WhisperededtoBinding.bind(holder.itemView)
        val context = holder.itemView.context
        val bean = beanList[position]
        binding.usefuledto.setImageDrawable(ContextCompat.getDrawable(context, bean.icon))
        binding.momentedto.text = bean.tittle
        if (bean.subFile.isEmpty()) {
            binding.damnedto.visibility = View.GONE
            binding.startedto.setTextColor(Color.parseColor("#999999"))
            binding.wantedto.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.survivaledto))
            binding.siteedto.visibility = View.VISIBLE
        } else {
            binding.damnedto.visibility = View.VISIBLE
            runCatching {
                val options = RequestOptions().transform(CenterCrop(), RoundedCorners(context.dpToPx(10)))
                val image1 = bean.subFile[0]
                Glide.with(context).load(image1).apply(options).into(binding.anniversaryedto)

                val image2 = bean.subFile[1]
                Glide.with(context).load(image2).apply(options).into(binding.recruitedto)

                val image3 = bean.subFile[2]
                Glide.with(context).load(image3).apply(options).into(binding.dribbleedto)
            }
            binding.startedto.setTextColor(Color.parseColor("#355BEA"))
            binding.startedto.text = bean.size.toFormatSize()
            binding.wantedto.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.areaedto))
            binding.siteedto.visibility = View.GONE
        }
        binding.root.setOnClickListener {
            if (bean.subFile.isEmpty()) {
                return@setOnClickListener
            }
            click.invoke(bean)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: List<Deckedto>) {
        beanList.clear()
        beanList.addAll(list)
        notifyDataSetChanged()
    }


}