package com.zxbw.modulemain.adapter

import android.app.Activity
import android.text.Spannable
import android.text.SpannableString
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.zxbw.modulemain.R
import com.zxbw.modulemain.view.CenterImageSpan
import com.zxbw.modulemain.view.UIndicator
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.qy.JgqQyEntity
import com.zxhl.cms.net.model.qy.TbGoodsItemEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.widget.RecycleImageView

/**
 * @author (wangXuewei)
 * @datetime 2022-05-26 21:05 GMT+8
 * @detail :
 */
class ShopRecommendAdapter : BaseRecyclerAdapter<TbGoodsItemEntity, RecyclerView.ViewHolder> {
    private var mContext: Activity? = null
    private val listener: OnRecycleItemClickListener<TbGoodsItemEntity>

    private val VIEW_TYPE_CATE_VIEW = 1
    private val VIEW_TYPE_ACTION_VIEW = 2
    private val VIEW_TYPE_GOODS_VIEW = 3

    constructor(
        content: Activity?,
        listener: OnRecycleItemClickListener<TbGoodsItemEntity>
    ) : super() {
        mContext = content
        this.listener = listener
    }

    private var mCateDataList: List<JgqQyEntity>? = null
    fun updateShopCategoryData(qyDataList: List<JgqQyEntity>) {
        mCateDataList = qyDataList
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        var realPos = position - 1;
        if (position == 0) {
            return VIEW_TYPE_CATE_VIEW
        } else if (position ==1) {
            return VIEW_TYPE_ACTION_VIEW
        } else {
            return VIEW_TYPE_GOODS_VIEW
        }
    }

    override fun getItemCount(): Int {
        return mList.size + 1
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        var viewNum = 1;
        if (holder is GoodsViewHolder) {
            val bean = mList[position - viewNum]
            setImgText(mContext, holder.id_tv_goods_name!!, bean.title)

            if (mContext?.isFinishing == false) {
                if (bean.pict_url?.startsWith("//img") == true) {
                    holder.id_item_goods_img?.setLoadImageUrl("https:" + bean.pict_url, R.drawable.img_goods_error, 10)
                } else {
                    holder.id_item_goods_img?.setLoadImageUrl(bean.pict_url, R.drawable.img_goods_error, 10)
                }
//                if (bean.pict_url?.startsWith("//img") == true) {
//                    holder.id_item_goods_img?.setLoadImageUrl(
//                        "https:" + bean.pict_url,
//                        R.drawable.img_goods_error,
//                        10,
//                        true,
//                        true,
//                        false,
//                        false
//                    )
//                } else {
//                    holder.id_item_goods_img?.setLoadImageUrl(
//                        bean.pict_url,
//                        R.drawable.img_goods_error,
//                        10,
//                        true,
//                        true,
//                        false,
//                        false
//                    )
//                }
            }

            holder.id_ll_goods_item_view?.setOnClickListener {
                listener.onItemClick(it, position, bean)
            }
        } else if (holder is QyViewHolder) {
            if (!mCateDataList.isNullOrEmpty()) {
                holder.qy_item_view?.visibility = View.VISIBLE
                var splitItemNum = 10;
                val result = ArrayList<List<JgqQyEntity>>()
                if (mCateDataList?.size!! <= splitItemNum) {
                    result.add(mCateDataList!!)
                } else {
                    //
                    val splitNum = if (mCateDataList?.size!! % splitItemNum == 0) {
                        mCateDataList?.size!! / splitItemNum
                    } else {
                        mCateDataList?.size!! / splitItemNum + 1
                    }
                    var value: List<JgqQyEntity>? = null
                    for (i in 0 until splitNum) {
                        if (i < splitNum - 1) {
                            value = mCateDataList?.subList(i * splitItemNum, (i + 1) * splitItemNum)
                        } else {
                            value = mCateDataList?.subList(i * splitItemNum, mCateDataList?.size!!)
                        }
                        if (value != null) {
                            result.add(value)
                        }
                    }
                }
                var mQyBannerAdapter = HomeQyBannerAdapter(result, mContext)
                holder.qy_vp?.adapter = mQyBannerAdapter;
                mQyBannerAdapter.notifyDataSetChanged()
                holder.id_qy_indicator_view?.setIndicatorCount(result.size)
                holder.qy_vp?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                    override fun onPageScrolled(
                        position: Int,
                        positionOffset: Float,
                        positionOffsetPixels: Int
                    ) {
                    }

                    override fun onPageSelected(position: Int) {
                        holder.id_qy_indicator_view?.setSelectPos(position)
                    }

                    override fun onPageScrollStateChanged(state: Int) {
                    }

                })
            } else {
                holder.qy_item_view?.visibility = View.GONE
            }
        } else if (holder is ActionGoodsViewHolder) {

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == VIEW_TYPE_CATE_VIEW) {
            return QyViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.item_layout_home_qy_view, parent, false)
            )
        } else if (viewType == VIEW_TYPE_ACTION_VIEW) {
            return ActionGoodsViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.item_layout_shop_action_goods_view, parent, false)
            )
        } else {
            return GoodsViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.item_layout_shop_goods_view, parent, false)
            )
        }

    }

    //商品
    class GoodsViewHolder : RecyclerView.ViewHolder {
        var id_ll_goods_item_view: LinearLayout?
        var id_item_goods_img: RecycleImageView?
        var id_tv_goods_name: TextView?
        var id_tv_original_price: TextView?
        var id_tv_vip_price: TextView?
        var id_tv_sale_num: TextView?

        constructor(
            itemView: View
        ) : super(itemView) {
            id_ll_goods_item_view =
                itemView.findViewById<LinearLayout>(R.id.id_ll_goods_item_view)
            id_item_goods_img = itemView.findViewById<RecycleImageView>(R.id.id_item_goods_img)
            id_tv_goods_name = itemView.findViewById<TextView>(R.id.id_tv_goods_name)
            id_tv_original_price = itemView.findViewById<TextView>(R.id.id_tv_goods_original_price)
            id_tv_vip_price = itemView.findViewById<TextView>(R.id.id_tv_goods_vip_price)
            id_tv_sale_num = itemView.findViewById<TextView>(R.id.id_tv_goods_sale_num)
        }
    }

    //权益
    class QyViewHolder : RecyclerView.ViewHolder {
        var qy_item_view: RelativeLayout?
        var qy_vp: ViewPager?
        var id_qy_indicator_view: UIndicator?

        constructor(
            itemView: View
        ) : super(itemView) {
            qy_item_view = itemView.findViewById<RelativeLayout>(R.id.id_rl_home_qy_item_view)
            qy_vp = itemView.findViewById<ViewPager>(R.id.id_vp_home_jgq_qy_list)
            id_qy_indicator_view = itemView.findViewById<UIndicator>(R.id.id_qy_indicator_view)
        }
    }

    //活动商品
    class ActionGoodsViewHolder : RecyclerView.ViewHolder {

        constructor(
            itemView: View
        ) : super(itemView) {

        }
    }


    private fun setImgText(content: Activity?, tv: TextView, str: String?) {
        tv.text = "";
        if (content == null) {
            tv.text = str;
        } else {
            val d = ContextCompat.getDrawable(content, R.drawable.icon_shop_goods_label)
            d?.setBounds(0, 0, d.minimumWidth, d.minimumHeight)
            val span = d?.let { CenterImageSpan(it) }
            val spanStr = SpannableString("custom$0")
            spanStr.setSpan(span, 0, spanStr.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
            tv.append(spanStr)
            tv.append(" ")
            tv.append(str)
        }

    }
}