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

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RelativeLayout
import androidx.core.view.isVisible
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.target.Target
import com.sunny.app.soft.timberkeyboardnew.R

class KeyAdapter(
    private val context: Context,
    private var selectedPos: Int = 0,
    private val modelList: List<String>,
    private val listener: (Int) -> Unit
) : RecyclerView.Adapter<KeyAdapter.ItemViewHolder>() {




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

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

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

        val pd: ProgressBar = itemView.findViewById<ProgressBar>(R.id.pb)
        val viewSelected: ImageView = itemView.findViewById(R.id.view_selected)

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

            try {
                Glide
                    .with(context)
                    .load(url)
                    .thumbnail(0.5f)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .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
                        }
                    })
//                    .apply(
//                        RequestOptions().placeholder(R.drawable.png_loading)
//                    )
                    .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_key, parent, false)
        return ItemViewHolder(view)
    }


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

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val category = modelList[position]
        holder.viewSelected.isVisible = selectedPos == position
        var curPos = position
        holder.loadImg(context, category, holder.imgItemView)

        holder.rootItemLayout.setOnClickListener {
            selectedPos = curPos
            listener.invoke(selectedPos)
            notifyDataSetChanged()
        }
    }

}