package com.yidianling.home

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydlcommon.modular.ModularServiceManager
import com.ydl.ydlcommon.utils.Utils
import com.yidianling.home.model.bean.MeditationModuleBean
import com.yidianling.home.router.HomeIn
import com.yidianling.user.api.service.IUserService
import kotlinx.android.synthetic.ydl.home_fragment.*

class MeditationTypeAdapter(
    private val context: Context,
    private val data: List<MeditationModuleBean.MeditationDetail>,
    private val moreLink: String,
    private val homeLink:String?=null,
    private val cateId:Long?=null,
    private val mark:Int?=null,
    private val meditationType: Int
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            CONTENT_TYPE -> {
                MeditationTypeViewHolder(
                    LayoutInflater.from(context)
                        .inflate(R.layout.layout_meditation_item, parent, false)
                )
            }
            else -> {
                MeditationTypeMoreViewHolder(
                    LayoutInflater.from(context)
                        .inflate(R.layout.layout_meditation_more_type, parent, false)
                )
            }
        }

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        when (holder) {
            is MeditationTypeViewHolder -> {
                holder.ivType?.let {
                    Glide.with(context)
                        .load(data[position].coverImageUrl)
                        .into(it)
                }

                holder.tvName?.let {
                    it.text = data[position].title
                }

                holder.cvLayout?.setOnClickListener {
                    if (meditationType == 0){
                        if (!ModularServiceManager.provide(IUserService::class.java).loginByOneKeyLogin(context,true)) {
                            return@setOnClickListener
                        }
                        ARouter.getInstance().build("/muse/play")
                            .withLong("MEDITATION_ID", data[position].meditationId)
                            .withInt("MEDITATION_TYPE", meditationType)
                            .withFloat("MEDIA_DURATION", data[position].duration?:0.0f)
                            .withString("MEDIA_URL", data[position].mediaIdCoverImageUrl)
                            .withString("MEDIA_COVER_URL", data[position].mediaIdCoverImageUrl)
                            .navigation()
                    }else{
                        if (Utils.isFastClick()) {
                            //防止连击
                            return@setOnClickListener
                        }
                        val homeLink = "${homeLink}mark=$mark&cateId=$cateId&meditationId=${data[position].meditationId}"
                        if (!homeLink.isNullOrBlank()) {
                            NewH5Activity.start(context, H5Params(homeLink, null))
                        }
                    }
                }
            }
            is MeditationTypeMoreViewHolder -> {
                holder.llMoreLayout?.setOnClickListener {
                    if (Utils.isFastClick()) {
                        //防止连击
                        return@setOnClickListener
                    }
                    val moreLink = "${moreLink}mark=$mark&cateId=$cateId"
                    if (!moreLink.isNullOrBlank()) {
                        NewH5Activity.start(context, H5Params(moreLink, null))
                    }
                }
            }
        }

    }

    override fun getItemCount(): Int {
        return if (data.isNotEmpty()) {
            data.size + 1
        } else {
            data.size
        }
    }

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

    override fun getItemViewType(position: Int): Int {
        return if (data.isNotEmpty() && position == data.size) {
            FOOTER_TYPE
        } else {
            CONTENT_TYPE
        }
    }

    inner class MeditationTypeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val cvLayout: CardView? = itemView.findViewById(R.id.card_view)
        val ivType: ImageView? = itemView.findViewById(R.id.iv_meditation_type)
        val tvName: TextView? = itemView.findViewById(R.id.tv_meditation_type)
    }

    inner class MeditationTypeMoreViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val llMoreLayout: LinearLayout? = itemView.findViewById(R.id.ll_more_type)
    }

    companion object {
        const val CONTENT_TYPE = 0
        const val FOOTER_TYPE = 1
    }


}