package cpm.com.gskmtorange.adapter

import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import cpm.com.gskmtorange.dailyentry.WebViewActivity
import cpm.com.gskmtorange.databinding.ItemCategoryScoreBinding
import cpm.com.gskmtorange.databinding.ItemOverallScoreBinding
import cpm.com.gskmtorange.databinding.ItemPdImageBinding
import cpm.com.gskmtorange.utils.ScoreItem

class ScoreAdapter(private val items: List<ScoreItem>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val TYPE_OVERALL = 0
    private val TYPE_CATEGORY = 1
    private val TYPE_IMAGE = 2

    override fun getItemViewType(position: Int): Int {
        return when (items[position]) {
            is ScoreItem.OverallScore -> TYPE_OVERALL
            is ScoreItem.CategoryScore -> TYPE_CATEGORY
            is ScoreItem.ImageItem -> TYPE_IMAGE
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_OVERALL -> {
                val binding = ItemOverallScoreBinding.inflate(inflater, parent, false)
                OverallViewHolder(binding)
            }
            TYPE_CATEGORY -> {
                val binding = ItemCategoryScoreBinding.inflate(inflater, parent, false)
                CategoryViewHolder(binding)
            }
            TYPE_IMAGE -> {
                val binding = ItemPdImageBinding.inflate(inflater, parent, false)
                ImageViewHolder(binding)
            }
            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (val item = items[position]) {
            is ScoreItem.OverallScore -> (holder as OverallViewHolder).bind(item)
            is ScoreItem.CategoryScore -> (holder as CategoryViewHolder).bind(item)
            is ScoreItem.ImageItem -> (holder as ImageViewHolder).bind(item)
        }
    }

    override fun getItemCount(): Int = items.size

    class OverallViewHolder(private val binding: ItemOverallScoreBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: ScoreItem.OverallScore) {
            binding.tvOsa.text = "OSA: ${item.osa}"
            binding.tvMsl.text = "MSL: ${item.msl}"
            binding.tvPosm.text = "POSM: ${item.posm}"
            binding.tvSosSku.text = "SOS (SKU): ${item.sosSku}"
            binding.tvSosBrand.text = "SOS (Brand): ${item.sosBrand}"
        }
    }

    class CategoryViewHolder(private val binding: ItemCategoryScoreBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: ScoreItem.CategoryScore) {
            binding.tvCategory.text = item.categoryName
            binding.tvMsl.text = "MSL: ${item.msl}"
            binding.tvPosm.text = "POSM: ${item.posm}"
            binding.tvSosSku.text = "SOS (SKU): ${item.sosSku}"
            binding.tvSosBrand.text = "SOS (Brand): ${item.sosBrand}"

            binding.layoutBrands.removeAllViews()
            for (brand in item.brandDetails) {
                val tv = TextView(binding.root.context)
                tv.text = "${brand.first}: ${brand.second}"
                binding.layoutBrands.addView(tv)
            }
        }
    }

    class ImageViewHolder(private val binding: ItemPdImageBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: ScoreItem.ImageItem) {
            Log.d("itemuri",item.url)
           /* binding.btnViewImage.setOnClickListener {
                val context = binding.root.context
                val intent = Intent(context, WebViewActivity::class.java)
                intent.putExtra("url", item.url)
                context.startActivity(intent)
            }*/
            val actualUrl = if (item.url.contains("?url=")) {
                item.url.substringAfter("?url=") // extract the real image link
            } else item.url

            Glide.with(binding.ivRawImage.context)
                .load(actualUrl)
                //.placeholder(R.drawable.ic_placeholder) // optional
               // .error(R.drawable.ic_broken_image) // fallback if fail
                .into(binding.ivRawImage)
        }
    }
}
