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

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
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.UpsertSourceActivity
import bou.amine.apps.readerforselfossv2.android.databinding.SourceListItemBinding
import bou.amine.apps.readerforselfossv2.android.utils.glide.circularDrawable
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 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.SourceDetail>,
) : RecyclerView.Adapter<SourcesListAdapter.ViewHolder>(), DIAware {
    private val c: Context = app.baseContext
    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]

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

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

        holder.mView.setOnClickListener {
            val source = items[position]

            repository.setSelectedSource(source)
            app.startActivity(Intent(app, UpsertSourceActivity::class.java))
        }

        if (itm.getIcon(repository.baseUrl).isEmpty()) {
            binding.itemImage.setBackgroundAndText(itm.title.getHtmlDecoded())
        } else {
            c.circularDrawable(itm.getIcon(repository.baseUrl), binding.itemImage)
        }

        if (!itm.error.isNullOrBlank()) {
            binding.errorText.visibility = View.VISIBLE
            binding.errorText.text = itm.error
        } else {
            binding.errorText.visibility = View.GONE
        }

        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(val mView: ConstraintLayout) : RecyclerView.ViewHolder(mView)
}
