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.TextView
import androidx.recyclerview.widget.RecyclerView
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.GoodList
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.RecycleImageView
import com.zxhl.main.R

class ItemOrderAdapter : BaseRecyclerAdapter<GoodList, ItemOrderAdapter.ViewHolder> {
    private var mContext: Context? = null

    constructor(
        content: Context?
    ) : super() {
        mContext = content
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var bean = mList[position]
        if (!TextUtils.isEmpty(bean.trackingNo)) {
            holder.id_tv_courier_no?.text = bean.trackingNo
            holder.id_img_copy?.visibility = View.VISIBLE
        } else {
            holder.id_tv_courier_no?.text = "生成中…"
            holder.id_img_copy?.visibility = View.GONE
        }
        holder.id_img_copy?.setOnClickListener {
            copy(holder.id_tv_courier_no?.text.toString())
        }



        if (bean.orderStatus == 0) {
            holder.id_tv_status?.text = "待发货"
        } else if (bean.orderStatus == 1) {
            holder.id_tv_status?.text = "已发货"
        } else {
            holder.id_tv_status?.text = "已签收"
        }
        holder.id_tv_goods_name?.text = bean.productName
        holder.id_img_goods?.setLoadImageUrl(bean.mainImage, false)
        holder.id_tv_nums?.text = "x ${bean.amount}"
        holder.id_tv_sale_price?.text = "¥ ${bean.salePriceStr}"

    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(AppContext.get())
                .inflate(R.layout.items_order_layout, parent, false)
        )
    }

    class ViewHolder : RecyclerView.ViewHolder {

        var id_img_goods: RecycleImageView?
        var id_tv_goods_name: TextView?
        var id_tv_pay_price: TextView?
        var id_tv_status: TextView?
        var id_tv_courier_no: TextView?
        var id_tv_sale_price: TextView?
        var id_tv_nums: TextView?
        var id_img_copy: ImageView?



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

            id_tv_goods_name = itemView.findViewById<TextView>(R.id.id_tv_goods_name)
            id_tv_pay_price = itemView.findViewById<TextView>(R.id.id_tv_pay_price)
            id_tv_status = itemView.findViewById<TextView>(R.id.id_tv_status)
            id_tv_courier_no = itemView.findViewById<TextView>(R.id.id_tv_courier_no)
            id_tv_sale_price = itemView.findViewById<TextView>(R.id.id_tv_sale_price)
            id_tv_nums = itemView.findViewById<TextView>(R.id.id_tv_nums)
            id_img_copy = itemView.findViewById<ImageView>(R.id.id_img_copy)
            id_img_goods = itemView.findViewById<RecycleImageView>(R.id.id_img_goods)

        }
    }

    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, "已复制到剪切板")
    }
}