package com.zxhl.main.page.adapter

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
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.box.DepotEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.RecycleImageView
import com.zxhl.main.R

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

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<DepotEntity>
    ) : 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_img_goods?.setLoadImageUrl(bean.products?.mainImage, false)
            holder.item_tv_good_name?.text = bean.products?.productName
            holder.item_tv_good_type?.text = bean.products?.cateName
            holder.item_tv_good_price?.text = bean.amountSpent
            if (TextUtils.isEmpty(bean.trackingNo)) {
                holder.item_tv_good_order_no?.text = "生成中…"
                holder.item_img_copy?.visibility=View.GONE
            } else {
                holder.item_tv_good_order_no?.text = bean.trackingNo
                holder.item_img_copy?.visibility=View.VISIBLE
            }
            holder.item_img_copy?.setOnClickListener {
                copy(holder.item_tv_good_order_no?.text.toString())
            }
            holder.item_rl?.setOnClickListener {
                listener.onItemClick(it, position, bean)
            }

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

    }
    fun copy(string: String){
        //获取剪贴板管理器：
        val cm: ClipboardManager =
            mContext?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建普通字符型ClipData

        val mClipData: ClipData =
            ClipData.newPlainText("Label", string)
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData)
        Utils.showToast(mContext, "已复制到剪切板")
    }
    private var isLoadMore = true
    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_receive, parent, false)
                )
            }
        } else {
            return ViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_layout_receive, parent, false)
            )
        }


    }

    class ViewHolder : RecyclerView.ViewHolder {


        var item_rl: RelativeLayout?
        var item_img_goods: RecycleImageView?
        var item_tv_good_type: TextView?
        var item_tv_good_name: TextView?
        var item_tv_good_price: TextView?
        var item_tv_good_order_no: TextView?
        var item_img_copy: ImageView?


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

            item_tv_good_type = itemView.findViewById<TextView>(R.id.id_goods_type)
            item_tv_good_name = itemView.findViewById<TextView>(R.id.id_goods_name)
            item_tv_good_price = itemView.findViewById<TextView>(R.id.id_tv_good_price)
            item_rl = itemView.findViewById<RelativeLayout>(R.id.id_rl_item)
            item_img_goods = itemView.findViewById<RecycleImageView>(R.id.id_goods_img)
            item_tv_good_order_no = itemView.findViewById<TextView>(R.id.id_tv_order_no)
            item_img_copy = itemView.findViewById<ImageView>(R.id.id_img_copy)
        }
    }

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

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