package app.furmeet.events.system.schedule.adapters

import android.content.Intent
import android.content.res.ColorStateList
import android.net.Uri
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import app.furmeet.events.system.helper.getDrawableCompat
import app.furmeet.events.bff.R
import app.furmeet.events.system.helper.layoutInflater
import app.furmeet.events.system.helper.solved
import app.furmeet.events.system.schedule.abstractDataTypes.convention.Convention
import app.furmeet.events.system.schedule.abstractDataTypes.convention.SocialMedia
import app.furmeet.events.system.schedule.viewHolders.RecyclerViewHolder
import app.furmeet.events.bff.databinding.FragmentSocialmediaItemBinding

class ConventionSocialMediasAdapter(private val convention: Convention) : RecyclerViewAdapter() {
    override fun getItemCount(): Int = convention.socialMedias.size
    override fun getItemViewType(position: Int): Int = 0
    override fun getItemId(position: Int): Long = position.toLong()
    private fun getItem(position: Int): SocialMedia = convention.socialMedias[position]

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder =
            RecyclerViewHolder(parent.context.layoutInflater.inflate(R.layout.fragment_socialmedia_item, parent, false))

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        val view = holder.itemView
        val data = getItem(position)
        FragmentSocialmediaItemBinding.bind(view).apply {
            icon.setImageDrawable(root.resources.getDrawableCompat(data.icon, null))
            icon.imageTintList = ColorStateList.valueOf(data.color)
            title.text = data.names.solved
            val url = data.url.solved
            description.text = url
            root.setOnClickListener {
                val i = Intent(Intent.ACTION_VIEW)
                i.data = Uri.parse(url)
                view.context.startActivity(i)
            }
        }
    }

}
