package com.zxbw.modulemain.adapter

import android.app.Activity
import android.graphics.Paint
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.zhpan.indicator.IndicatorView
import com.zxbw.modulemain.R
import com.zxbw.modulemain.box.adapter.ItemBoxGoodsAdapter
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.utils.Utils
import com.zxhl.cms.widget.RecycleImageView

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

    private val VIEW_TYPE_QY_VIEW = 1
    private val VIEW_TYPE_ZERO_VIEW = 2
    private val VIEW_TYPE_GOODS_VIEW = 3
    private val VIEW_TYPE_BOX_VIEW = 4

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

    private var mIsShowQyView = false;
    fun IsShowQyView(boolean: Boolean) {
        mIsShowQyView = boolean
    }

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

    override fun getItemViewType(position: Int): Int {
        var realPos = position;
        if (mIsShowQyView) {
            realPos = position - 1;
        }
        if (position == 0 && mIsShowQyView) {
            return VIEW_TYPE_QY_VIEW
        } else if (mList.size > 1 && mList[realPos].bannerType == "blindBox") {
            return VIEW_TYPE_BOX_VIEW
        } else {
            return VIEW_TYPE_GOODS_VIEW
        }
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        var viewNum = 0;
        if (mIsShowQyView) {
            viewNum = 1;
        }
        if (holder is GoodsViewHolder) {
            val bean = mList[position - viewNum]
            holder.id_tv_goods_name?.text = bean.title
            if (mContext?.isFinishing == false) {
                if (bean.pict_url?.startsWith("//img") == true) {
                    holder.id_item_goods_img?.setLoadImageUrl("https:" + bean.pict_url, 0, 5)
                } else {
                    holder.id_item_goods_img?.setLoadImageUrl(bean.pict_url, 0, 5)
                }
            }
            holder.id_tv_tb_price_value?.text = "淘宝商城价 : ¥${bean.reserve_price}"
            if (bean.coupon_amount.isNullOrEmpty()) {
                holder.id_tv_yhq_value?.visibility = View.GONE
                holder.id_tv_goods_price?.text = "${bean.reserve_price}"
            } else {
                holder.id_tv_yhq_value?.visibility = View.VISIBLE
                holder.id_tv_yhq_value?.text = "${bean.coupon_amount}元券"
                var r_price = bean.reserve_price?.toDoubleOrNull() ?: 0 as Double
                var c_price = bean.coupon_amount?.toDoubleOrNull() ?: 0 as Double
                holder.id_tv_goods_price?.text = Utils.getNoMoreThanTwoDigits(
                    Utils.sub(
                        r_price,
                        c_price
                    )
                )
            }

            if (bean.coupon_info.isNullOrEmpty()) {
                holder.id_tv_manjian_value?.visibility = View.GONE
            } else {
                holder.id_tv_manjian_value?.visibility = View.VISIBLE
                holder.id_tv_manjian_value?.text = "${bean.coupon_info}"
            }


            holder.id_rl_goods_item_view?.setOnClickListener {
                listener.onItemClick(it, position, bean)
            }
        } else if (holder is QyViewHolder) {
            if (!mQyDataList.isNullOrEmpty()) {
                holder.qy_item_view?.visibility = View.VISIBLE
                var splitItemNum = 15;
                val result = ArrayList<List<JgqQyEntity>>()
                if (mQyDataList?.size!! <= splitItemNum) {
                    result.add(mQyDataList!!)
                } else {
                    //
                    val splitNum = if (mQyDataList?.size!! % splitItemNum == 0) {
                        mQyDataList?.size!! / splitItemNum
                    } else {
                        mQyDataList?.size!! / splitItemNum + 1
                    }
                    var value: List<JgqQyEntity>? = null
                    for (i in 0 until splitNum) {
                        if (i < splitNum - 1) {
                            value = mQyDataList?.subList(i * splitItemNum, (i + 1) * splitItemNum)
                        } else {
                            value = mQyDataList?.subList(i * splitItemNum, mQyDataList?.size!!)
                        }
                        if (value != null) {
                            result.add(value)
                        }
                    }
                }
                Log.d("wxw", "resultSize${result.size}")
                var mQyBannerAdapter = HomeQyBannerAdapter(result, mContext)
                holder.qy_vp?.adapter = mQyBannerAdapter;
                holder.qy_vp?.postDelayed(Runnable {
                    holder.qy_vp?.requestLayout()
                }, 100)
                holder.id_qy_indicator_view?.setSliderGap(40f)
                holder.id_qy_indicator_view?.setupWithViewPager(holder.qy_vp!!)
            } else {
                holder.qy_item_view?.visibility = View.GONE
            }
        } else if (holder is ZeroViewHolder) {

        } else if (holder is BoxViewHolder) {
            val bean = mList[position - viewNum]
            holder.box_main_img?.setLoadImageUrl(bean.mainImage, 0, 5)
            holder.box_name?.text =bean.boxName
            holder.box_price?.text =bean.onePriceStr
            holder.box_old_price?.text = "¥ ${bean.originalPrice}"
            holder.box_old_price?.paint?.flags = Paint.STRIKE_THRU_TEXT_FLAG
            var a_price = bean.originalPrice?.toDoubleOrNull() ?: 0 as Double
            var b_price = bean.onePriceStr?.toDoubleOrNull() ?: 0 as Double
            holder.box_discount_desc?.text = "已减${Utils.getNoMoreThanTwoDigits(Utils.sub(a_price,b_price))}元"

            var mAdapter: ItemBoxGoodsAdapter? = ItemBoxGoodsAdapter(mContext)
            holder.box_goods_list?.layoutManager = LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false)
            holder.box_goods_list?.adapter = mAdapter
            mAdapter?.clear()
            mAdapter?.appendToList(bean.goodsList)

            holder.box_item_view?.setOnClickListener {
                listener.onItemClick(it, position, bean)
            }

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == VIEW_TYPE_QY_VIEW) {
            return QyViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.item_layout_home_qy_view, parent, false)
            )
        } else if (viewType == VIEW_TYPE_ZERO_VIEW) {
            return ZeroViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.item_layout_home_zero_buy_view, parent, false)
            )
        } else if (viewType == VIEW_TYPE_BOX_VIEW) {
            return BoxViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.item_layout_box_goods_view, parent, false)
            )
        } else {
            return GoodsViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.item_layout_goods_view, parent, false)
            )
        }

    }

    //商品
    class GoodsViewHolder : RecyclerView.ViewHolder {
        var id_rl_goods_item_view: RelativeLayout?
        var id_item_goods_img: RecycleImageView?
        var id_tv_goods_name: TextView?
        var id_tv_yhq_value: TextView?
        var id_tv_manjian_value: TextView?
        var id_tv_tb_price_value: TextView?
        var id_tv_goods_price: TextView?

        constructor(
            itemView: View
        ) : super(itemView) {
            id_rl_goods_item_view =
                itemView.findViewById<RelativeLayout>(R.id.id_rl_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_yhq_value = itemView.findViewById<TextView>(R.id.id_tv_yhq_value)
            id_tv_manjian_value = itemView.findViewById<TextView>(R.id.id_tv_manjian_value)
            id_tv_tb_price_value = itemView.findViewById<TextView>(R.id.id_tv_tb_price_value)
            id_tv_goods_price = itemView.findViewById<TextView>(R.id.id_tv_goods_price)
        }
    }

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

        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<IndicatorView>(R.id.id_qy_indicator_view)
        }
    }

    //零元购
    class ZeroViewHolder : RecyclerView.ViewHolder {

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

        }
    }

    //盲盒
    class BoxViewHolder : RecyclerView.ViewHolder {
        var box_item_view: RelativeLayout?
        var box_main_img: RecycleImageView?
        var box_name: TextView?
        var box_discount_desc: TextView?
        var box_price: TextView?
        var box_old_price: TextView?
        var box_goods_list: RecyclerView?

        constructor(
            itemView: View
        ) : super(itemView) {
            box_item_view = itemView.findViewById<RelativeLayout>(R.id.id_rl_box_item_view)
            box_main_img = itemView.findViewById<RecycleImageView>(R.id.id_item_box_main_img)
            box_name = itemView.findViewById<TextView>(R.id.id_tv_box_name)
            box_discount_desc = itemView.findViewById<TextView>(R.id.id_tv_box_discount_desc)
            box_price = itemView.findViewById<TextView>(R.id.id_tv_box_price)
            box_old_price = itemView.findViewById<TextView>(R.id.id_tv_box_old_price)
            box_goods_list = itemView.findViewById<RecyclerView>(R.id.id_rcl_box_goods_list)
        }
    }
}