package com.zxhl.main.page.adapter

import android.content.Context
import android.graphics.Paint
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.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.shop.ShopListEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.widget.RecycleImageView
import com.zxhl.main.R
import kotlinx.android.synthetic.main.activity_layout_goods_receive.*

class ShopListAdapter : BaseRecyclerAdapter<ShopListEntity, RecyclerView.ViewHolder> {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<ShopListEntity>
    private val CONTENT = 1000
    private val LOADMORE = 1001

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

    override fun getItemCount(): Int {
        val size = mList.size
        if (size > 0) {
            return if (isLoadMore) {
                mList.size + 1
            } else {
                mList.size
            }
        } else {
            return size
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (isLoadMore) {
            if (position == mList.size) {
                return LOADMORE
            } else {
                return CONTENT
            }
        } else {
            return CONTENT
        }
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ViewHolder) {
            var bean = mList[position]
            holder.goodsName?.text = bean.productName
            holder.goodsPrice?.text = bean.salePriceStr
            holder.goodsImg?.setLoadImageUrl(bean.mainImage, false)
            holder.goodsOldPrice?.text="¥${bean.originalPrice}"
            holder.goodsOldPrice?.paint?.flags=Paint.STRIKE_THRU_TEXT_FLAG //中划线
            holder.sellNum?.text="已售:${bean.salesVolume}"



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

        } else if (holder is LoadMoreHolder) {
            Glide.with(AppContext.get()).asGif().load(R.drawable.load_more).into(holder.mImg)
        }

    }

    private var isLoadMore = false
    fun setLoadMore(isBoolean: Boolean) {
        isLoadMore = isBoolean
    }

    fun getLoadMore(): Boolean = isLoadMore

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (isLoadMore) {
            if (viewType == LOADMORE) {
                return LoadMoreHolder(
                    LayoutInflater.from(mContext).inflate(R.layout.item_load_more, parent, false)
                )
            } else {
                return ViewHolder(
                    LayoutInflater.from(mContext)
                        .inflate(R.layout.item_layout_shop_goods, parent, false)
                )
            }
        } else {
            return ViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_layout_box, parent, false)
            )
        }


    }

    class ViewHolder : RecyclerView.ViewHolder {
        var item_ll: LinearLayout?
        var goodsImg: RecycleImageView?
        var payIcon: ImageView?
        var goodsName: TextView?
        var goodsPrice: TextView?
        var goodsOldPrice: TextView?
        var sellNum: TextView?

        constructor(
            itemView: View
        ) : super(itemView) {
            item_ll = itemView.findViewById<LinearLayout>(R.id.id_ll_item)
            goodsImg = itemView.findViewById<RecycleImageView>(R.id.id_item_goods_img)
            payIcon = itemView.findViewById<ImageView>(R.id.id_goods_pay_icon)
            goodsName = itemView.findViewById<TextView>(R.id.id_item_goods_name)
            goodsPrice = itemView.findViewById<TextView>(R.id.id_item_goods_price)
            goodsOldPrice = itemView.findViewById<TextView>(R.id.id_item_goods_oldPrice)
            sellNum = itemView.findViewById<TextView>(R.id.id_item_goods_sell_num)

        }
    }

    inner class LoadMoreHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mImg: ImageView

        init {
            mImg = itemView.findViewById(R.id.id_item_more_img)
        }
    }
}