package com.zxbw.modulemain.adapter

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
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.zxbw.modulemain.R
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.ShopOrderEntity
import com.zxhl.cms.net.model.qy.RightsDetailEntity
import com.zxhl.cms.utils.Utils

//我的订单
class MyOrderAdapter : BaseRecyclerAdapter<ShopOrderEntity, RecyclerView.ViewHolder> {
    private var mContext: Activity? = null

    private val VIEW_TYPE_GOODS_VIEW = 101
    private val VIEW_TYPE_CARD_VIEW = 201
    private val VIEW_TYPE_VIP_VIEW = 301

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

    override fun getItemViewType(position: Int): Int {
        if (mList[position].orderType == "50" || mList[position].orderType == "8080") {
            return VIEW_TYPE_VIP_VIEW
        } else if (mList[position].orderType == "51") {
            return VIEW_TYPE_CARD_VIEW
        } else if (mList[position].orderType == "99") {
            return VIEW_TYPE_GOODS_VIEW
        } else {
            return VIEW_TYPE_GOODS_VIEW
        }
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        var bean = mList[position]
        //实物订单
        if (holder is GoodsOrderViewHolder) {
            holder.img_order_no_copy?.setOnClickListener {
                copy(holder.id_tv_order_no?.text.toString())
            }
            holder.id_tv_total_price?.text = "¥${bean.cash}"
            if (bean.orderType == "99") {
                holder.id_tv_pricedesc?.text =
                    "总价:｜"
            } else {
                holder.id_tv_pricedesc?.text =
                    "总价:¥${bean.totalPrice}｜优惠:¥${bean.discount}｜运费:¥${bean.freight}｜"
            }
            holder.id_tv_order_no?.text = bean.orderNo
            holder.id_tv_pay_time?.text = bean.payTime
            if (!bean.goodsList.isNullOrEmpty()) {
                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)
            }
        } else if (holder is VipRightsOrderViewHolder) {
            holder.img_vip_order_no_copy?.setOnClickListener {
                copy(holder.id_tv_vip_order_no?.text.toString())
            }
            holder.id_tv_vip_order_no?.text = bean.orderNo
            holder.id_tv_vip_pay_time?.text = "付款时间：${bean.payTime}"
            if (bean.orderType == "8080") {
                holder.id_tv_vip_phone_number?.text = "充值手机号：${bean.phone}"
                if (bean.equitCard.isNullOrEmpty()) {
                    val huafeiDetailEntity = RightsDetailEntity()
                    huafeiDetailEntity.salePrice = bean.cash
                    huafeiDetailEntity.goodsName = bean.orderNotes
                    bean.equitCard = listOf(huafeiDetailEntity)
                }
            } else {
                holder.id_tv_vip_phone_number?.text = "会员手机号：${bean.phone}"
            }
            if (!bean.equitCard.isNullOrEmpty()) {
                var mAdapter: ItemVipOrderAdapter? = ItemVipOrderAdapter(mContext, bean.orderType)
                holder.id_rl_vip_order_item?.layoutManager =
                    LinearLayoutManager(mContext)
                holder.id_rl_vip_order_item?.adapter = mAdapter
                mAdapter?.clear()
                mAdapter?.appendToList(bean.equitCard)
            }

        } else {

        }


    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        if (viewType == VIEW_TYPE_VIP_VIEW) {
            return VipRightsOrderViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.items_layout_vip_rights_order, parent, false)
            )
        } else if (viewType == VIEW_TYPE_CARD_VIEW) {
            return CardRightsOrderViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.items_layout_card_rights_order, parent, false)
            )
        } else if (viewType == VIEW_TYPE_GOODS_VIEW) {
            return GoodsOrderViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.items_layout_box_order, parent, false)
            )
        } else {
            return GoodsOrderViewHolder(
                LayoutInflater.from(AppContext.get())
                    .inflate(R.layout.items_layout_box_order, parent, false)
            )
        }


    }

    //实物商品类型
    class GoodsOrderViewHolder : 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: ImageView?
        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<ImageView>(R.id.img_order_no_copy)
            id_rl_order_item = itemView.findViewById<RecyclerView>(R.id.id_rl_order_item)

        }
    }

    //权益卡类型
    class VipRightsOrderViewHolder : RecyclerView.ViewHolder {
        var id_tv_vip_phone_number: TextView?
        var id_tv_vip_order_no: TextView?
        var id_tv_vip_pay_time: TextView?
        var img_vip_order_no_copy: ImageView?
        var id_rl_vip_order_item: RecyclerView?

        constructor(
            itemView: View
        ) : super(itemView) {
            id_tv_vip_phone_number = itemView.findViewById<TextView>(R.id.id_tv_vip_phone_number)
            id_tv_vip_order_no = itemView.findViewById<TextView>(R.id.id_tv_vip_order_no)
            id_tv_vip_pay_time = itemView.findViewById<TextView>(R.id.id_tv_vip_pay_time)
            img_vip_order_no_copy = itemView.findViewById<ImageView>(R.id.img_vip_order_no_copy)
            id_rl_vip_order_item = itemView.findViewById<RecyclerView>(R.id.id_rl_vip_order_item)
        }
    }

    //卡券类类型
    class CardRightsOrderViewHolder : RecyclerView.ViewHolder {

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

        }
    }


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