package com.adealink.weparty.order.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatString
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.R
import com.adealink.weparty.order.data.PlaymateOrderData
import com.adealink.weparty.order.data.PlaymateOrderListItemData
import com.adealink.weparty.order.databinding.LayoutPlaymateOrderListItemBinding
import com.adealink.weparty.order.util.setUserOrderStatusText
import com.adealink.weparty.util.formatTimeStr
import com.adealink.weparty.util.getGenderResource

class PlaymateListItemViewBinder(
    val listener: OnPlaymateOrderListener
) : ItemViewBinder<PlaymateOrderListItemData, BindingViewHolder<LayoutPlaymateOrderListItemBinding>>() {

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutPlaymateOrderListItemBinding>,
        item: PlaymateOrderListItemData,
    ) {
        holder.binding.tvTime.text = item.data.createTime.formatTimeStr()

        holder.binding.ivCategory.setImageUrl(item.data.categoryIcon)
        holder.binding.tvCategory.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.ivAvatar.setImageUrl(item.data.avatar)
        holder.binding.tvName.text = item.data.nickname
        holder.binding.ivGender.setImageResource(getGenderResource(item.data.gender))

        holder.binding.tvRequestContent.text = item.data.customerRemark

        val status = OrderStatus.map(item.data.status)
        setUserOrderStatusText(holder.binding.tvOrderStatus, status, item.data.refundApply)
        when {
            item.data.refundApply -> {
                holder.binding.btnFinish.gone()
                holder.binding.btnRefuse.gone()
                holder.binding.btnSubmitEvidence.show()
                holder.binding.btnSubmitEvidence.onClick {
                    listener.submitEvidence(item.data)
                }
            }

            status == OrderStatus.CREATE_ORDER
                    || status == OrderStatus.WAIT_FOR_ACCEPT -> {
                holder.binding.btnFinish.show()
                holder.binding.btnFinish.text = getCompatString(R.string.playmate_order_accept)
                holder.binding.btnFinish.onClick {
                    listener.accept(item.data)
                }
                holder.binding.btnRefuse.show()
                holder.binding.btnRefuse.onClick {
                    listener.refuse(item.data)
                }
                holder.binding.btnSubmitEvidence.gone()
            }


            status == OrderStatus.PLAYMATE_ACCEPT -> {
                holder.binding.btnFinish.show()
                holder.binding.btnFinish.text = getCompatString(R.string.playmate_order_start_service)
                holder.binding.btnFinish.onClick {
                    listener.startService(item.data)
                }
                holder.binding.btnRefuse.gone()
                holder.binding.btnSubmitEvidence.gone()
            }


            status == OrderStatus.USER_CANCEL -> {
                holder.binding.btnFinish.gone()
                holder.binding.btnRefuse.gone()
                holder.binding.btnSubmitEvidence.gone()
            }

            status == OrderStatus.PLAYMATE_IN_SERVICE -> {
                holder.binding.btnFinish.show()
                holder.binding.btnFinish.text = getCompatString(R.string.playmate_order_complete)
                holder.binding.btnFinish.onClick {
                    listener.complete(item.data)
                }
                holder.binding.btnRefuse.gone()
                holder.binding.btnSubmitEvidence.gone()
            }

            status == OrderStatus.PLAYMATE_REFUSE -> {
                holder.binding.btnFinish.gone()
                holder.binding.btnRefuse.gone()
                holder.binding.btnSubmitEvidence.gone()
            }

            status == OrderStatus.USER_REFUND -> {
                holder.binding.btnFinish.gone()
                holder.binding.btnRefuse.gone()
                holder.binding.btnSubmitEvidence.show()
                holder.binding.btnSubmitEvidence.onClick {
                    listener.submitEvidence(item.data)
                }
            }

            status == OrderStatus.PLAYMATE_END_SERVICE -> {
                holder.binding.btnFinish.gone()
                holder.binding.btnRefuse.gone()
                holder.binding.btnSubmitEvidence.gone()
            }

            status == OrderStatus.COMPLETE -> {
                holder.binding.btnFinish.gone()
                holder.binding.btnRefuse.gone()
                holder.binding.btnSubmitEvidence.gone()
            }
        }
    }

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

    interface OnPlaymateOrderListener {
        fun accept(data: PlaymateOrderData)

        fun refuse(data: PlaymateOrderData)

        fun startService(data: PlaymateOrderData)

        fun complete(data: PlaymateOrderData)

        fun submitEvidence(data: PlaymateOrderData)
    }

}