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.ShopOrderEntity
import com.zxhl.cms.utils.Utils
import com.zxhl.cms.widget.RecycleImageView
import com.zxhl.main.R

class ShopOrderAdapter : BaseRecyclerAdapter<ShopOrderEntity, ShopOrderAdapter.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.goodsList?.get(0)?.trackingNo)) {
            holder.id_tv_courier_no?.text = bean.goodsList?.get(0)?.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())
        }
        holder.img_order_no_copy?.setOnClickListener {
            copy(holder.id_tv_order_no?.text.toString())
        }
        holder.id_tv_total_price?.text = "$${bean.cash}"
        var mPrice = bean.cash?.toDouble()?.plus(bean.discount?.toDouble())
        //holder.id_tv_pricedesc?.text = "总价:$${mPrice}｜优惠:$${bean.discount}｜运费:$${bean.freight}｜"
        var total=AppContext.get().getString(R.string.total)
        var discount=AppContext.get().getString(R.string.discount)
        var express_fee=AppContext.get().getString(R.string.express_fee)
        holder.id_tv_pricedesc?.text = "${total}:$${mPrice}｜${discount}:$${bean.discount}｜${express_fee}:$${bean.freight}"
        if (bean.orderStatus == 0) {
            holder.id_tv_status?.text = AppContext.get().getString(R.string.unshipped)
        } else if (bean.orderStatus == 1) {
            holder.id_tv_status?.text = AppContext.get().getString(R.string.shipped)
        } else {
            //holder.id_tv_status?.text = "已签收"
        }
        holder.id_tv_goods_name?.text = bean.goodsList?.get(0)?.productName
        holder.id_img_goods?.setLoadImageUrl(bean.goodsList?.get(0)?.mainImage, false)
        holder.id_tv_nums?.text = "x ${bean.goodsList?.get(0)?.amount}"
        holder.id_tv_sale_price?.text = "$ ${bean.goodsList?.get(0)?.salePriceStr}"
        holder.id_tv_order_no?.text = "${bean.orderNo}"
        holder.id_tv_pay_time?.text = bean.payTime
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(AppContext.get())
                .inflate(R.layout.item_layout_shop_order, 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_tv_total_price: TextView?
        var id_tv_pricedesc: TextView?
        var id_tv_order_no: TextView?
        var id_tv_pay_time: TextView?
        var id_img_copy: TextView?
        var img_order_no_copy: TextView?


        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_tv_total_price = itemView.findViewById<TextView>(R.id.id_tv_total_price)
            id_tv_pricedesc = itemView.findViewById<TextView>(R.id.id_tv_pricedesc)
            id_tv_order_no = itemView.findViewById<TextView>(R.id.id_tv_order_no)
            id_tv_pay_time = itemView.findViewById<TextView>(R.id.id_tv_pay_time)
            id_img_copy = itemView.findViewById<TextView>(R.id.id_img_copy)
            img_order_no_copy = itemView.findViewById<TextView>(R.id.img_order_no_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, "已复制到剪切板")
    }
}