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.router.Router
import com.adealink.frame.util.formatTime
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.getActivity
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.ext.sp
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.module.order.data.OrderDetailInfo
import com.adealink.weparty.module.order.data.OrderStatus
import com.adealink.weparty.module.order.util.setUserOrderStatusText
import com.adealink.weparty.module.profile.Profile
import com.adealink.weparty.order.R
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.util.TIME_FORMAT_DMY
import com.adealink.weparty.util.formatNumberStr

/**
 * 用户订单列表状态
 * https://mimolive.sg.larksuite.com/wiki/PX4Qw1N1viFjpSkt0qmlLeTGgqh
 */
class UserOrderListItemViewBinder(
    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 = formatTime(item.data.createTime, TIME_FORMAT_DMY)

        holder.binding.ivAvatar.setImageUrl(item.data.avatar)
        holder.binding.ivAvatar.onClick {
            val act = it.getActivity() ?: return@onClick
            Router.build(act, Profile.UserProfile.PATH)
                .putExtra(Profile.Common.EXTRA_UID, item.data.sellerUserNo)
                .start()
        }
        holder.binding.tvName.text = item.data.nickname
        holder.binding.tvDesc.text = item.data.categoryName
        holder.binding.tvPrice.text = formatNumberStr(item.data.price, false)
        holder.binding.tvCount.text = "${item.data.unit} x${item.data.purchaseQty}"

        val totalAmount = item.data.totalAmount
        val targetAmount = item.data.price * item.data.purchaseQty
        if (totalAmount != null && totalAmount > 0 && totalAmount < targetAmount) {
            holder.binding.vDiscount.show()
        } else {
            holder.binding.vDiscount.gone()
        }
        holder.binding.tvOrderAllCost.text = getOrderAllCost(
            item.data.price,
            item.data.purchaseQty,
            item.data.totalAmount,
            16.sp(),
            16.dp()
        )

        val status = OrderStatus.map(item.data.status)
        setUserOrderStatusText(holder.binding.tvOrderStatus, status, item.data.refundApply)
        when {
            item.data.refundApply == true -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.gone()
                holder.binding.btnEvaluate.gone()
                holder.binding.tvRefunded.show()
                holder.binding.tvRefunded.text =
                    getCompatString(R.string.order_refunded_pending_review)
            }

            status == OrderStatus.CREATE_ORDER -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.onClick {
                    listener.cancelOrder(item, holder.layoutPosition)
                }
                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, holder.layoutPosition)
                }
                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.gone()
                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.show()
                holder.binding.tvRefunded.text = getCompatString(R.string.order_refunded_success)
            }

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

            status == OrderStatus.COMPLETE -> {
                holder.binding.btnComplete.gone()
                holder.binding.btnCancel.gone()
                if (item.data.star == null || item.data.star == 0f) {
                    //没有评分
                    holder.binding.btnEvaluate.show()
                    holder.binding.btnEvaluate.onClick {
                        listener.evaluateOrder(item, holder.layoutPosition)
                    }
                } else {
                    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(data: OrderDetailInfo)

        fun cancelOrder(item: UserOrderListItemData, position: Int)

        fun completeOrder(item: UserOrderListItemData, position: Int)

        fun evaluateOrder(item: UserOrderListItemData, position: Int)
    }
}