package bou.amine.apps.readerforselfossv2.android.adapters

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import bou.amine.apps.readerforselfossv2.android.R
import bou.amine.apps.readerforselfossv2.android.databinding.SourceListItemBinding
import bou.amine.apps.readerforselfossv2.android.model.toTextDrawableString
import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable
import bou.amine.apps.readerforselfossv2.model.SelfossModel
import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded
import bou.amine.apps.readerforselfossv2.utils.getIcon
import com.amulyakhare.textdrawable.TextDrawable
import com.amulyakhare.textdrawable.util.ColorGenerator
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.closestDI
import org.kodein.di.instance

class SourcesListAdapter(
    private val app: Activity,
    private val items: ArrayList<SelfossModel.Source>
) : RecyclerView.Adapter<SourcesListAdapter.ViewHolder>(), DIAware {
    private val c: Context = app.baseContext
    private val generator: ColorGenerator = ColorGenerator.MATERIAL
    private lateinit var binding: SourceListItemBinding

    override val di: DI by closestDI(app)
    private val repository : Repository by instance()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        binding = SourceListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding.root)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itm = items[position]

        if (itm.getIcon(repository.baseUrl).isEmpty()) {
            val color = generator.getColor(itm.title.getHtmlDecoded())

            val drawable =
                TextDrawable
                    .builder()
                    .round()
                    .build(itm.title.getHtmlDecoded().toTextDrawableString(), color)
            binding.itemImage.setImageDrawable(drawable)
        } else {
            c.circularBitmapDrawable(itm.getIcon(repository.baseUrl), binding.itemImage)
        }

        binding.sourceTitle.text = itm.title.getHtmlDecoded()
    }

    override fun getItemId(position: Int) = position.toLong()

    override fun getItemViewType(position: Int) = position

    override fun getItemCount(): Int = items.size

    inner class ViewHolder(private val mView: ConstraintLayout) : RecyclerView.ViewHolder(mView) {

        init {
            handleClickListeners()
        }

        private fun handleClickListeners() {

            val deleteBtn: Button = mView.findViewById(R.id.deleteBtn)

            deleteBtn.setOnClickListener {
                val (id) = items[bindingAdapterPosition]
                CoroutineScope(Dispatchers.IO).launch {
                    val successfullyDeletedSource = repository.deleteSource(id)
                    if (successfullyDeletedSource) {
                        items.removeAt(bindingAdapterPosition)
                        notifyItemRemoved(bindingAdapterPosition)
                        notifyItemRangeChanged(bindingAdapterPosition, itemCount)
                    } else {
                        Toast.makeText(
                            app,
                            R.string.can_delete_source,
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
            }
        }
    }
}
