package com.zj365.dc.adapter

import android.content.Context
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.xty.common.setBigImage

import com.xty.network.model.BannerBean
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.util.BannerUtils
import com.zj365.dc.R
import com.zj365.dc.adapter.holder.ImageHolder
import com.zj365.dc.adapter.holder.TitleHolder
import com.zj365.dc.adapter.holder.VideoHolder


/**
 * 自定义布局,多个不同UI切换
 */
class BannerCustomAdapter(var context: Context)
    : BannerAdapter<BannerBean, RecyclerView.ViewHolder?>(null) {
    private val mVHMap: SparseArray<RecyclerView.ViewHolder> = SparseArray()
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            1 -> return ImageHolder(BannerUtils.getView(parent, R.layout.banner_image))
            2 -> return VideoHolder(BannerUtils.getView(parent, R.layout.banner_video))
            3 -> return TitleHolder(BannerUtils.getView(parent, R.layout.banner_title))
        }
        return ImageHolder(BannerUtils.getView(parent, R.layout.banner_image))
    }

    override fun getItemViewType(position: Int): Int {
        //先取得真实的position,在获取实体
//        return getData(getRealPosition(position)).viewType;
        //直接获取真实的实体
        return getRealData(position).bannerType
        //或者自己直接去操作集合
//        return mDatas.get(getRealPosition(position)).viewType;
    }

    override fun onBindView(
        holder: RecyclerView.ViewHolder?,
        data: BannerBean?,
        position: Int,
        size: Int
    ) {
        holder?.itemViewType.apply {
            when (this) {
                1 -> {//图片
                    val imageHolder: ImageHolder = holder as ImageHolder
                    mVHMap.append(position, imageHolder)
                    context?.let { imageHolder.imageView.setBigImage(it, data?.imgUrl) }
                }
                15 -> {//视频
                    val videoHolder: VideoHolder = holder as VideoHolder
                    mVHMap.append(position, videoHolder)
                    videoHolder.player.setUp(data?.linkAddress, true, null)
                    videoHolder.player.backButton.visibility = View.GONE
                    videoHolder.player.fullscreenButton.visibility = View.GONE
                    //增加封面
                    val imageView = ImageView(context)
                    imageView.scaleType = ImageView.ScaleType.CENTER_CROP
                    context?.let { imageView.setBigImage(it, data?.imgUrl) }
                    videoHolder.player.thumbImageView = imageView
                }
                10 -> {//web
                    val imageHolder: ImageHolder = holder as ImageHolder
                    mVHMap.append(position, imageHolder)
                    context?.let { imageHolder.imageView.setBigImage(it, data?.imgUrl) }
                }
                5 -> {//标题
                    val imageHolder: ImageHolder = holder as ImageHolder
                    mVHMap.append(position, imageHolder)
                    context?.let { imageHolder.imageView.setBigImage(it, data?.imgUrl) }
                }

                20 ->{
                    val imageHolder: ImageHolder = holder as ImageHolder
                    mVHMap.append(position, imageHolder)
                    context?.let { imageHolder.imageView.setBigImage(it, data?.imgUrl) }
                }

                else ->{
                    val imageHolder: ImageHolder = holder as ImageHolder
                    mVHMap.append(position, imageHolder)
                    context?.let { imageHolder.imageView.setBigImage(it, data?.imgUrl) }
                }
            }
        }
    }

    val vHMap: SparseArray<RecyclerView.ViewHolder>
        get() = mVHMap
}