package com.medicdigital.jjpodcasts.presentation.podcasts.adapter

import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.medicdigital.jjpodcasts.R
import com.medicdigital.jjpodcasts.data.remote.responce.NodeItem
import com.medicdigital.jjpodcasts.presentation.extentions.getRoundedOutlineProvider
import com.medicdigital.jjpodcasts.presentation.podcasts.EpisodesClick
import com.medicdigital.jjpodcasts.presentation.utils.Constants
import com.medicdigital.jjpodcasts.presentation.utils.getNodeFeatureImage
import com.medicdigital.jjpodcasts.presentation.utils.getNodeImage
import com.medicdigital.jjpodcasts.presentation.utils.getTypeWithPriorityEx
import com.medicdigital.jjpodcasts.presentation.podcasts.adapter.databinding.ItemPodcastFeaturedBinding


class PodcastFeaturedListAdapter(private val itemClickListener: EpisodesClick?, private val itemWidth: Int, private val itemHeight: Int) :
       BaseQuickAdapter<NodeItem, BaseViewHolder>(R.layout.item_podcast_featured),
       View.OnClickListener {

    override fun convert(holder: BaseViewHolder, item: NodeItem) {
       val itemView = holder.itemView

       val ivPodcastFeatured = itemView.iv_podcast_featured_logo
       ivPodcastFeatured.layoutParams.height = itemHeight
       Glide.with(context)
             .load(getNodeFeatureImage(item?.assets ?: arrayListOf()) ?: getNodeImage(item?.assets ?: arrayListOf()))
             .placeholder(R.drawable.ic_microfon_gray)
             .apply(RequestOptions.fitCenterTransform())
             .into(ivPodcastFeatured)

       itemView.txt_podcast_featured.text = item?.name

       itemView.vg_podcast_featured_logo.apply {
          tag = item
          setOnClickListener(this@PodcastFeaturedListAdapter)
          outlineProvider = context.getRoundedOutlineProvider(alpha = .3f, cornerRadius = R.dimen.corner_4)
       }
    }

    override fun onClick(v: View?) {
       v ?: return
       val nodeItem = v.tag as? NodeItem?
       when (v.id) {
          R.id.vg_podcast_featured_logo -> {
             if (nodeItem?.type == Constants.TYPE_PODCAST_EPISODE.toString()) {
                when (getTypeWithPriorityEx(nodeItem, nodeItem.url, nodeItem.quiz)) {
                   Constants.TYPE_VIDEO -> {
                      itemClickListener?.onEpisodeClick(nodeItem, getTypeWithPriorityEx(nodeItem, nodeItem.url, nodeItem.quiz))
                   }
                   Constants.TYPE_AUDIO -> {
                      itemClickListener?.onEpisodeClick(nodeItem, getTypeWithPriorityEx(nodeItem, nodeItem.url, nodeItem.quiz))
                   }
                   Constants.TYPE_PDF -> {
                      itemClickListener?.onEpisodeClick(nodeItem, getTypeWithPriorityEx(nodeItem, nodeItem.url, nodeItem.quiz))
                   }
                   Constants.TYPE_GALLERY -> {
                      itemClickListener?.onEpisodeClick(nodeItem, getTypeWithPriorityEx(nodeItem, nodeItem.url, nodeItem.quiz))
                   }
                   Constants.EXTERNAL_LINK -> {
                      itemClickListener?.onEpisodeClick(nodeItem, getTypeWithPriorityEx(nodeItem, nodeItem.url, nodeItem.quiz))
                   }
                   Constants.QUIZ -> {
                      itemClickListener?.onEpisodeClick(nodeItem, getTypeWithPriorityEx(nodeItem, nodeItem.url, nodeItem.quiz))
                   }
                }
             } else {
                itemClickListener?.onNodeWithDetailInfoClick(nodeItem)
             }
          }
       }
    }
}