package com.sunny.app.soft.timberkeyboardnew.ui.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.sunny.app.soft.timberkeyboardnew.R
import com.sunny.app.soft.timberkeyboardnew.data.entity.BackgroundEntity
import com.sunny.app.soft.timberkeyboardnew.ui.listener.OnBgItemClickListener

class SelectAdapter(
    private val context: Context,

    private val listener: OnBgItemClickListener
) : RecyclerView.Adapter<SelectAdapter.ViewHolder>() {
    private var backgroundEntityList: List<BackgroundEntity> = emptyList()

    fun updateData(list:List<BackgroundEntity> ){
        backgroundEntityList = list
        notifyDataSetChanged()
    }
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val imgItemView: ImageView = itemView.findViewById(R.id.item_img)
        val rootItemLayout: CardView = itemView.findViewById<CardView>(R.id.item_root)

        val pd: ProgressBar = itemView.findViewById<ProgressBar>(R.id.select_pd)
        fun loadImg(context: Context, url: String, imgView: ImageView) {
            pd.visibility = View.VISIBLE
            try {
                Glide
                    .with(context)
                    .load(url)
                    .thumbnail(0.1f)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .listener(object : RequestListener<Drawable> {
                        override fun onLoadFailed(
                            e: GlideException?,
                            model: Any?,
                            target: Target<Drawable>,
                            isFirstResource: Boolean
                        ): Boolean {

                            pd.visibility = View.GONE
                            return false
                        }

                        override fun onResourceReady(
                            resource: Drawable,
                            model: Any,
                            target: Target<Drawable>?,
                            dataSource: DataSource,
                            isFirstResource: Boolean
                        ): Boolean {

                            pd.visibility = View.GONE

                            return false
                        }
                    })
//                    .apply(
//                        RequestOptions().placeholder(R.drawable.png_loading)
//                    )
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(imgView)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_background, parent, false)
        return ViewHolder(view)

    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val backgroundEntity = backgroundEntityList[position]
        holder.rootItemLayout.setOnClickListener {
            listener.onItemClick(position, backgroundEntity)
        }

        Log.d("-----------","--------------backgroundEntity.coverPath=${backgroundEntity.coverPath}")
        holder.loadImg(context, backgroundEntity.coverPath, holder.imgItemView)
    }
}