package com.duben.microtribe.ui.adapter

import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.duben.library.utils.GlideUtils
import com.duben.microtribe.R
import com.duben.microtribe.mvp.model.*
import com.duben.microtribe.utils.UIUtils

class NewHotStyleAdapter :
    BaseMultiItemQuickAdapter<VideoMultiItemEntity3, BaseViewHolder>() {

    init {
        addItemType(MULTI_ITEM_1, R.layout.item_hot_style_root)
        addItemType(MULTI_ITEM_3, R.layout.item_express_ad)
    }

    override fun convert(holder: BaseViewHolder, item: VideoMultiItemEntity3) {
        when (holder.itemViewType) {
            MULTI_ITEM_1 -> {
                initHolder(holder, item.videos)
            }
            MULTI_ITEM_3 -> {
                initAdHolder(holder, item.adView)
            }
        }
    }

    private fun initAdHolder(holder: BaseViewHolder, adView: FrameLayout?) {
        adView?.let {
            UIUtils.removeFromParent(it)
            val fmAd = holder.getView<FrameLayout>(R.id.fl_ad)
            fmAd.removeAllViews()
            fmAd.addView(it)
        }
    }

    private fun initHolder(holder: BaseViewHolder, item: List<VedioBean>?) {
        item?.let {

            if (it.isEmpty()) {
                holder.getView<View>(R.id.item1).visibility = View.INVISIBLE
                holder.getView<View>(R.id.item2).visibility = View.INVISIBLE
                holder.getView<View>(R.id.item3).visibility = View.INVISIBLE
                return
            }

            holder.getView<View>(R.id.item1).visibility = View.VISIBLE
            holder.getView<View>(R.id.item1)
                .findViewById<TextView>(R.id.tvVedioItemCount)
                .text = "${if (it[0].completeStatus == 0) "已完结" else "更新至${it[0].vedioTotal}集"}"
            holder.getView<View>(R.id.item1)
                .findViewById<TextView>(R.id.tvVedioItemTitle)
                .text = "${it[0].title}"
            holder.getView<View>(R.id.item1)
                .findViewById<TextView>(R.id.tvVedioItemText)
                .text = "${it[0].vedioDesc}"
            GlideUtils.loadImageViewGifForCenterCrop(
                context,
                it[0].coverImage,
                holder.getView<View>(R.id.item1)
                    .findViewById(R.id.ivVedioItem)
            )
            holder.getView<View>(R.id.item1).setOnClickListener { it1 ->
                mOnCustomChildClickListener?.onCustomChildClick(it1, it[0])
            }

            if (it.size < 2) {
                holder.getView<View>(R.id.item2).visibility = View.INVISIBLE
                holder.getView<View>(R.id.item3).visibility = View.INVISIBLE
                return
            }

            holder.getView<View>(R.id.item2).visibility = View.VISIBLE
            holder.getView<View>(R.id.item2)
                .findViewById<TextView>(R.id.tvVedioItemCount)
                .text = "${if (it[1].completeStatus == 0) "已完结" else "更新至${it[1].vedioTotal}集"}"
            holder.getView<View>(R.id.item2)
                .findViewById<TextView>(R.id.tvVedioItemTitle)
                .text = "${it[1].title}"
            holder.getView<View>(R.id.item2)
                .findViewById<TextView>(R.id.tvVedioItemText)
                .text = "${it[1].vedioDesc}"
            GlideUtils.loadImageViewGifForCenterCrop(
                context,
                it[1].coverImage,
                holder.getView<View>(R.id.item2)
                    .findViewById(R.id.ivVedioItem)
            )
            holder.getView<View>(R.id.item2).setOnClickListener { it1 ->
                mOnCustomChildClickListener?.onCustomChildClick(it1, it[1])
            }

            if (it.size < 3) {
                holder.getView<View>(R.id.item3).visibility = View.INVISIBLE
                return
            }
            holder.getView<View>(R.id.item3).visibility = View.VISIBLE
            holder.getView<View>(R.id.item3)
                .findViewById<TextView>(R.id.tvVedioItemCount)
                .text = "${if (it[2].completeStatus == 0) "已完结" else "更新至${it[2].vedioTotal}集"}"
            holder.getView<View>(R.id.item3)
                .findViewById<TextView>(R.id.tvVedioItemTitle)
                .text = "${it[2].title}"
            holder.getView<View>(R.id.item3)
                .findViewById<TextView>(R.id.tvVedioItemText)
                .text = "${it[2].vedioDesc}"
            GlideUtils.loadImageViewGifForCenterCrop(
                context,
                it[2].coverImage,
                holder.getView<View>(R.id.item3)
                    .findViewById(R.id.ivVedioItem)
            )
            holder.getView<View>(R.id.item3).setOnClickListener { it1 ->
                mOnCustomChildClickListener?.onCustomChildClick(it1, it[2])
            }

//            for (vedio in it) {
//                val itemView =
//                    LayoutInflater.from(context).inflate(R.layout.item_hot_style, null)
//
//                itemView.findViewById<TextView>(R.id.tvVedioItemCount).text =
//                    "${if (vedio.completeStatus == 0) "已完结" else "更新至${vedio.vedioTotal}集"}"
//                itemView.findViewById<TextView>(R.id.tvVedioItemTitle).text = "${vedio.title}"
//                itemView.findViewById<TextView>(R.id.tvVedioItemText).text =
//                    "${vedio.vedioDesc}"
//                GlideUtils.loadImageViewGifForCenterCrop(
//                    context,
//                    vedio.coverImage,
//                    itemView.findViewById(R.id.ivVedioItem)
//                )
//                mOnCustomChildClickListener?.onCustomChildClick(itemView, vedio)
//                val layoutParams = LinearLayout.LayoutParams(
//                    LinearLayout.LayoutParams.MATCH_PARENT,
//                    LinearLayout.LayoutParams.MATCH_PARENT
//                )
//                layoutParams.weight = 1f
//                itemView.layoutParams = layoutParams
//                rootView.addView(itemView)
//            }
        }

//        item?.let {
//            holder.setText(
//                R.id.tvVedioItemCount,
//                "${if (it.completeStatus == 0) "已完结" else "更新至${it.vedioTotal}集"}"
//            )
//            holder.setText(
//                R.id.tvVedioItemTitle,
//                "${it.title}"
//            )
//            holder.setText(
//                R.id.tvVedioItemText,
//                "${it.vedioDesc}"
//            )
//            GlideUtils.loadImageViewGifForCenterCrop(
//                context,
//                it.coverImage,
//                holder.getView(R.id.ivVedioItem)
//            )
//        }
    }

    private var mOnCustomChildClickListener: OnCustomChildClickListener? = null

    fun setOnCustomChildClickListener(onCustomChildClickListener: OnCustomChildClickListener) {
        this.mOnCustomChildClickListener = onCustomChildClickListener
    }

    interface OnCustomChildClickListener {
        fun onCustomChildClick(view: View, vedioBean: VedioBean)
    }

}