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.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.PlaymateOrderListItemData
import com.adealink.weparty.order.databinding.LayoutPlaymateOrderListItemBinding
import com.adealink.weparty.order.util.getOrderAllCost
import com.adealink.weparty.util.TIME_FORMAT_DMY_HMS
import com.adealink.weparty.util.getGenderResource
import com.adealink.weparty.R as APP_R

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

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

        holder.binding.ivCategory.setImageUrl(item.data.categoryIcon)
        holder.binding.tvCategory.text = item.data.categoryName
        holder.binding.tvOrderId.text = item.data.orderId

        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.tvPrice.text = getOrderAllCost(
            item.data.price,
            item.data.purchaseQty,
            totalAmount,
            16.sp(),
            14.4f.dp(),
            R.string.order_all_cost
        )
        holder.binding.tvCount.text = "${item.data.unit} x${item.data.purchaseQty}"

        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.buyerUserNo)
                .start()
        }
        holder.binding.tvName.text = item.data.nickname
        holder.binding.ivGender.setImageResource(getGenderResource(item.data.gender))

        if (item.data.customerRemark.isNullOrEmpty()) {
            holder.binding.clRemark.gone()
        } else {
            holder.binding.clRemark.show()
            holder.binding.tvRemarkContent.text = item.data.customerRemark
        }

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

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


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

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

            status == OrderStatus.USER_CANCEL -> {
                holder.binding.clBottom.gone()
            }

            status == OrderStatus.PLAYMATE_REFUSE -> {
                holder.binding.clBottom.gone()
            }

            status == OrderStatus.USER_REFUND -> {
                holder.binding.clBottom.gone()
            }

            status == OrderStatus.PLAYMATE_END_SERVICE -> {
                holder.binding.clBottom.gone()
            }

            status == OrderStatus.COMPLETE -> {
                holder.binding.clBottom.gone()
            }
        }
    }

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

    interface OnPlaymateOrderListener {
        fun accept(item: PlaymateOrderListItemData, position: Int)

        fun refuse(item: PlaymateOrderListItemData, position: Int)

        fun startService(item: PlaymateOrderListItemData, position: Int)

        fun complete(item: PlaymateOrderListItemData, position: Int)

        fun submitEvidence(item: PlaymateOrderListItemData, position: Int)
    }

}