package com.zxhl.main.page.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Paint
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.HomeBoxListEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.widget.RecycleImageView
import com.zxhl.main.R

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

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<HomeBoxListEntity>
    ) : 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.item_tv_boxname?.text = bean.boxName
            holder.item_tv_description?.text = bean.description
            if (TextUtils.isEmpty(bean.newUserPrice)) {
                holder.id_rl_new_people?.visibility = View.GONE
                holder.id_tv_new_people_price?.visibility = View.GONE
            } else {
                holder.id_rl_new_people?.visibility = View.VISIBLE
                holder.id_tv_new_people_price?.visibility = View.VISIBLE
                holder.id_tv_new_people_price?.text = "$${bean.newUserPrice}"
                holder.id_tv_new_people_price?.getPaint()?.setFlags(Paint.STRIKE_THRU_TEXT_FLAG)
                holder.id_new_user_box_tips?.text = "New user $${bean.discount} discount"
            }
            holder.item_img_goos?.setLoadImageUrl(bean.mainImage, false)
//            holder.item_img_1?.setLoadImageUrl(bean.imageList2?.get(0), false)
//            holder.item_img_2?.setLoadImageUrl(bean.imageList2?.get(1), false)
//            holder.item_img_3?.setLoadImageUrl(bean.imageList2?.get(2), false)
//            holder.item_img_4?.setLoadImageUrl(bean.imageList2?.get(3), false)
//            holder.item_img_5?.setLoadImageUrl(bean.imageList2?.get(4), false)

            holder.item_tv_price?.text = bean.onePriceStr
            holder.item_tv_sales_volume?.text =
                AppContext.get().getString(R.string.open_nums, bean.numbersStr)
            //  holder.item_tv_min_max_price?.text = "${bean.minPriceStr}～${bean.maxPriceStr}元商品"
            holder.item_tv_min_max_price?.text = AppContext.get().getString(R.string.get_value)+"$${bean.minPrice}-${bean.maxPrice}"
            holder.item_ll?.setOnClickListener {
                listener.onItemClick(it, position, bean)
            }
            holder.id_tv_one_lottery?.setOnClickListener {
                listener.onItemClick(it, position, bean)
            }
            var mAdapter: ItemGoodsAdapter? = ItemGoodsAdapter(mContext,bean.boxId)
            holder.item_rl_goods?.layoutManager =
                LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false)
            holder.item_rl_goods?.adapter = mAdapter
            mAdapter?.clear()
            mAdapter?.appendToList(bean.goodsList)
            mAdapter?.notifyDataSetChanged()

        } 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_new_box, parent, false)
                )
            }
        } else {
            return ViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_layout_new_box, parent, false)
            )
        }


    }

    class ViewHolder : RecyclerView.ViewHolder {
        var item_tv_boxname: TextView?
        var item_tv_description: TextView?
        var item_tv_price: TextView?
        var item_tv_sales_volume: TextView?
        var item_tv_min_max_price: TextView?
        var item_img_goos: RecycleImageView?
        var item_ll: LinearLayout?
        var item_rl_goods: RecyclerView?
        var id_tv_one_lottery: TextView?
        var id_tv_new_people_price: TextView?
        var id_new_user_box_tips: TextView?
        var id_rl_new_people: RelativeLayout?

        constructor(
            itemView: View
        ) : super(itemView) {
            item_tv_boxname = itemView.findViewById<TextView>(R.id.id_tv_box_name)
            item_tv_description = itemView.findViewById<TextView>(R.id.id_tv_desc)
            item_tv_price = itemView.findViewById<TextView>(R.id.id_tv_price)
            item_tv_sales_volume = itemView.findViewById<TextView>(R.id.id_tv_sales_volume)
            item_tv_min_max_price = itemView.findViewById<TextView>(R.id.id_tv_min_max_price)
            id_tv_one_lottery = itemView.findViewById<TextView>(R.id.id_tv_one_lottery)
            id_tv_new_people_price = itemView.findViewById<TextView>(R.id.id_tv_new_people_price)
            id_new_user_box_tips = itemView.findViewById<TextView>(R.id.id_new_user_box_tips)
            id_rl_new_people = itemView.findViewById<RelativeLayout>(R.id.id_rl_new_people)
            item_img_goos = itemView.findViewById<RecycleImageView>(R.id.id_img_goods)
            item_rl_goods = itemView.findViewById<RecyclerView>(R.id.id_rl_goods)
            item_ll = itemView.findViewById<LinearLayout>(R.id.id_ll_item)
        }
    }

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

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