package com.adealink.weparty.order.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.module.order.data.OrderStatus
import com.adealink.weparty.order.data.UserOrderData
import com.adealink.weparty.order.data.UserOrderListItemData
import com.adealink.weparty.order.databinding.LayoutUserOrderListItemBinding
import com.adealink.weparty.order.util.getOrderAllCost
import com.adealink.weparty.order.util.setUserOrderStatusText
import com.adealink.weparty.util.formatTimeStr

/**
 * 用户订单列表状态
 * https://mimolive.sg.larksuite.com/wiki/PX4Qw1N1viFjpSkt0qmlLeTGgqh
 */
class OrderListItemViewBinder(
    val listener: OrderListListener
) : ItemViewBinder<UserOrderListItemData, BindingViewHolder<LayoutUserOrderListItemBinding>>() {

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutUserOrderListItemBinding>,
        item: UserOrderListItemData,
    ) {
        holder.binding.root.onClick {
            listener.showOrderDetail(item.data)
        }

        holder.binding.tvTime.text = item.data.createTime.formatTimeStr()
        val status = OrderStatus.map(item.data.status)
        setUserOrderStatusText(holder.binding.tvOrderStatus, status, item.data.refundApply)

        holder.binding.ivAvatar.setImageUrl(item.data.avatar)
        holder.binding.tvName.text = item.data.nickname
        holder.binding.tvDesc.text = item.data.bizCategoryName
        holder.binding.tvPrice.text = item.data.price.toString()
        holder.binding.tvCount.text = "${item.data.unit} x${item.data.purchaseQty}"
        holder.binding.tvOrderAllCost.text = getOrderAllCost(item.data.price, item.data.purchaseQty)

        when {
            item.data.refundApply -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.CREATE_ORDER -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.onClick {
                    listener.cancelOrder(item.data)
                }
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.WAIT_FOR_ACCEPT -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.show()
                holder.binding.btnCancel.onClick {
                    listener.cancelOrder(item.data)
                }
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.USER_CANCEL -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.PLAYMATE_IN_SERVICE -> {
                holder.binding.btnComplete.show()
                holder.binding.btnComplete.onClick {
                    listener.completeOrder(item.data)
                }
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.PLAYMATE_REFUSE -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.PLAYMATE_ACCEPT -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.USER_REFUND -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.PLAYMATE_END_SERVICE -> {
                holder.binding.btnComplete.show()
                holder.binding.btnComplete.onClick{

                }
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }

            status == OrderStatus.COMPLETE -> {
                holder.binding.btnComplete.show()
                holder.binding.btnComplete.onClick{

                }
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.gone()
            }
        }
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): BindingViewHolder<LayoutUserOrderListItemBinding> {
        return BindingViewHolder(LayoutUserOrderListItemBinding.inflate(inflater, parent, false))
    }

    interface OrderListListener {
        fun showOrderDetail(item: UserOrderData)

        fun cancelOrder(item: UserOrderData)

        fun completeOrder(item: UserOrderData)

        fun evaluateOrder(item: UserOrderData)
    }
}