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

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
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.CategoryEntity
import com.sunny.app.soft.timberkeyboardnew.ui.listener.OnItemClickListener

class HomeViewAdapter(
    private val context: Context,

    private val listener: OnItemClickListener
) : RecyclerView.Adapter<HomeViewAdapter.ItemViewHolder>() {

    private var modelList: List<CategoryEntity> = emptyList()

    fun updateData(list: List<CategoryEntity>) {
        modelList = list
        notifyDataSetChanged()
    }

    inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {


        val titleView: TextView = itemView.findViewById(R.id.item_title)

        val imgItemView: ImageView = itemView.findViewById(R.id.item_img)

        val rootItemLayout: LinearLayout = itemView.findViewById<LinearLayout>(R.id.item_root)

        val pd: ProgressBar = itemView.findViewById<ProgressBar>(R.id.pb)
        val view: View = itemView.findViewById(R.id.view_space)

        fun loadImg(context: Context, url: String, img: ImageView) {

            try {
                Glide
                    .with(context)
                    .asDrawable()
                    .load(url)
                    .thumbnail(0.1f)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .skipMemoryCache(true)
                    .listener(object : RequestListener<Drawable> {


                        override fun onLoadFailed(
                            e: GlideException?,
                            model: Any?,
                            target: Target<Drawable>,
                            isFirstResource: Boolean
                        ): Boolean {
                            pd.visibility = View.INVISIBLE
                            return false
                        }

                        override fun onResourceReady(
                            resource: Drawable,
                            model: Any,
                            target: Target<Drawable>?,
                            dataSource: DataSource,
                            isFirstResource: Boolean
                        ): Boolean {
                            pd.visibility = View.INVISIBLE

                            return false
                        }
                    })
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(img)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }


    }

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


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

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val category = modelList[position]
        holder.view.isVisible = position == modelList.size - 1

        holder.loadImg(context, category.coverPath, holder.imgItemView)

        holder.rootItemLayout.setOnClickListener {
            listener.onItemClick(position, category)
        }
        holder.titleView.text = category.name
    }

}