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.LinearLayoutManager
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.main.R

//魔盒订单adapter
class BoxOrderAdapter : BaseRecyclerAdapter<ShopOrderEntity, BoxOrderAdapter.ViewHolder> {
    private var mContext: Context? = null

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var bean = mList[position]
        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())
        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}:$${bean.totalPrice}｜${discount}:$${bean.discount}｜${express_fee}:$${bean.freight}"
        holder.id_tv_order_no?.text = "${bean.orderNo}"
        holder.id_tv_pay_time?.text = bean.payTime
        var mAdapter: ItemOrderAdapter? = ItemOrderAdapter(mContext)
        holder.id_rl_order_item?.layoutManager =
            LinearLayoutManager(mContext)
        holder.id_rl_order_item?.adapter = mAdapter
        mAdapter?.clear()
        mAdapter?.appendToList(bean.goodsList)
        mAdapter?.notifyDataSetChanged()
    }


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

    class ViewHolder : RecyclerView.ViewHolder {


        var id_tv_total_price: TextView?
        var id_tv_pricedesc: TextView?
        var id_tv_order_no: TextView?
        var id_tv_pay_time: TextView?
        var img_order_no_copy: TextView?
        var id_rl_order_item: RecyclerView?

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


            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)
            img_order_no_copy = itemView.findViewById<TextView>(R.id.img_order_no_copy)
            id_rl_order_item = itemView.findViewById<RecyclerView>(R.id.id_rl_order_item)

        }
    }

    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, AppContext.get().getString(R.string.fun_copy))
    }
}