package com.adealink.weparty.im.session.adapter.viewbinder

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.onClick
import com.adealink.weparty.commonui.commonSelectedDrawable
import com.adealink.weparty.commonui.ext.dpf
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.im.R
import com.adealink.weparty.im.databinding.LayoutSessionMessageCenterBaseBinding
import com.adealink.weparty.im.databinding.LayoutSessionMessagePlaymateOrderBinding
import com.adealink.weparty.im.session.mesasge.PlaymateOrderMessageBean
import com.adealink.weparty.module.order.Order
import com.adealink.weparty.module.order.data.OrderDetailInfo
import com.adealink.weparty.module.order.data.OrderStatus
import com.adealink.weparty.module.order.util.getPlaymateOrderStatusIcon
import com.adealink.weparty.module.order.util.getPlaymateOrderStatusText
import com.adealink.weparty.module.order.util.getUserOrderStatusIcon
import com.adealink.weparty.module.order.util.getUserOrderStatusText
import com.adealink.weparty.module.profile.ProfileModule
import com.tencent.qcloud.tuikit.timcommon.interfaces.OnItemClickListener
import com.adealink.weparty.R as APP_R

/**
 * 陪玩订单消息
 */
class PlaymateOrderMessageViewHolder(
    rootBinding: LayoutSessionMessageCenterBaseBinding,
    binding: LayoutSessionMessagePlaymateOrderBinding
) : CenterMessageViewHolder<PlaymateOrderMessageBean, LayoutSessionMessagePlaymateOrderBinding>(
    rootBinding,
    binding
) {

    private var onOrderClick: OnPlaymateOrderMessageClickListener? = null

    fun setOrderClickListener(onPlaymateClick: OnPlaymateOrderMessageClickListener) {
        this.onOrderClick = onPlaymateClick
    }

    override fun onBindMessage(
        binding: LayoutSessionMessagePlaymateOrderBinding,
        msg: PlaymateOrderMessageBean
    ) {
        binding.ivCategoryFrame.background = commonSelectedDrawable(100.dpf(), 1.dpf())
        if (msg.data?.buyerUserNo == ProfileModule.getMyUid()) {
            //我是下单买家客户
            updateBuyerOrder(binding, msg)
        } else {
            updatePlaymateOrder(binding, msg)
        }
        binding.ivCategory.setImageUrl(msg.data?.categoryIcon)
        binding.tvCategory.text = StringBuilder().apply {
            append(msg.data?.categoryName ?: "")
            if (!msg.data?.unit.isNullOrEmpty()) {
                append(" / ${msg.data?.unit ?: ""}")
            }
            if (msg.data?.purchaseQty != null) {
                append(" x${msg.data?.purchaseQty?.toString() ?: ""}")
            }
        }
    }

    private fun updateBuyerOrder(
        binding: LayoutSessionMessagePlaymateOrderBinding,
        msg: PlaymateOrderMessageBean
    ) {
        binding.root.onClick {
            val activity = it.context.getActivity() ?: return@onClick
            Router.build(activity, Order.List.PATH)
                .start()
        }
        val orderStatus = OrderStatus.map(msg.data?.status)
        binding.ivStatus.setImageResource(
            getUserOrderStatusIcon(
                orderStatus,
                msg.data?.refundApply
            )
        )
        binding.tvStatus.text = getUserOrderStatusText(
            orderStatus,
            msg.data?.refundApply
        )

        val orderDesc = if (msg.data?.refundApply == true) {
            getCompatString(R.string.im_user_order_apply_refund)
        } else {
            when (orderStatus) {
                OrderStatus.CREATE_ORDER -> {
                    getCompatString(R.string.im_user_order_wait_accept)
                }

                OrderStatus.WAIT_FOR_ACCEPT -> {
                    getCompatString(R.string.im_user_order_wait_accept)
                }

                OrderStatus.PLAYMATE_ACCEPT -> {
                    getCompatString(R.string.im_user_order_accepted)
                }

                OrderStatus.PLAYMATE_IN_SERVICE -> {
                    getCompatString(R.string.im_user_order_in_service)
                }

                OrderStatus.PLAYMATE_END_SERVICE -> {
                    getCompatString(R.string.im_user_order_end_service)
                }

                OrderStatus.PLAYMATE_REFUSE -> {
                    getCompatString(R.string.im_user_order_playmate_refuse)
                }

                OrderStatus.COMPLETE -> {
                    getCompatString(R.string.im_user_order_complete)
                }

                OrderStatus.USER_REFUND -> {
                    getCompatString(R.string.im_user_order_refund)
                }

                OrderStatus.USER_CANCEL -> {
                    getCompatString(R.string.im_user_order_cancel)
                }

                null -> {
                    null
                }
            }
        }
        if (orderDesc.isNullOrEmpty()) {
            binding.tvDesc.gone()
        } else {
            binding.tvDesc.show()
            binding.tvDesc.text = orderDesc
        }

        binding.btnComplete.gone()
    }

    private fun updatePlaymateOrder(
        binding: LayoutSessionMessagePlaymateOrderBinding,
        msg: PlaymateOrderMessageBean
    ) {
        binding.root.onClick {
            val activity = it.context.getActivity() ?: return@onClick
            Router.build(activity, Order.PlaymateList.PATH)
                .start()
        }
        val orderStatus = OrderStatus.map(msg.data?.status)
        binding.ivStatus.setImageResource(
            getPlaymateOrderStatusIcon(
                orderStatus,
                msg.data?.refundApply
            )
        )
        binding.tvStatus.text = getPlaymateOrderStatusText(
            orderStatus,
            msg.data?.refundApply
        )

        val orderDesc = if (msg.data?.refundApply == true) {
            getCompatString(R.string.im_playmate_order_apply_refund)
        } else {
            when (orderStatus) {
                OrderStatus.CREATE_ORDER -> {
                    msg.data?.customerRemark
                }

                OrderStatus.WAIT_FOR_ACCEPT -> {
                    msg.data?.customerRemark
                }

                OrderStatus.PLAYMATE_ACCEPT -> {
                    null
                }

                OrderStatus.PLAYMATE_IN_SERVICE -> {
                    getCompatString(R.string.im_playmate_order_in_service)
                }

                OrderStatus.PLAYMATE_END_SERVICE -> {
                    getCompatString(R.string.im_playmate_order_in_service)
                }

                OrderStatus.PLAYMATE_REFUSE -> {
                    getCompatString(R.string.im_playmate_order_playmate_refuse)
                }

                OrderStatus.COMPLETE -> {
                    getCompatString(R.string.im_playmate_order_complete)
                }

                OrderStatus.USER_REFUND -> {
                    getCompatString(R.string.im_playmate_order_refund)
                }

                OrderStatus.USER_CANCEL -> {
                    getCompatString(R.string.im_playmate_order_cancel)
                }

                null -> {
                    null
                }
            }
        }
        if (orderDesc.isNullOrEmpty()) {
            binding.tvDesc.gone()
        } else {
            binding.tvDesc.show()
            binding.tvDesc.text = orderDesc
        }

        if (msg.data?.refundApply == true) {
            binding.btnComplete.gone()
        } else {
            when (orderStatus) {
                OrderStatus.PLAYMATE_ACCEPT -> {
                    binding.btnComplete.show()
                    binding.btnComplete.text =
                        getCompatString(APP_R.string.playmate_order_start_service)
                    binding.btnComplete.onClick {
                        msg.data?.let { data ->
                            onOrderClick?.onPlaymateStartService(data)
                        }
                    }
                }

                OrderStatus.PLAYMATE_IN_SERVICE -> {
                    binding.btnComplete.show()
                    binding.btnComplete.text = getCompatString(APP_R.string.playmate_order_complete)
                    binding.btnComplete.onClick {
                        msg.data?.let { data ->
                            onOrderClick?.onPlaymateCompleteService(data)
                        }
                    }
                }

                OrderStatus.USER_REFUND,
                OrderStatus.PLAYMATE_END_SERVICE,
                OrderStatus.CREATE_ORDER,
                OrderStatus.PLAYMATE_REFUSE,
                OrderStatus.COMPLETE,
                OrderStatus.WAIT_FOR_ACCEPT,
                OrderStatus.USER_CANCEL,
                null -> {
                    binding.btnComplete.gone()
                }
            }
        }
    }

}

class PlaymateOrderMessageViewBinder(
    onItemClickListener: OnItemClickListener,
    val onPlaymateClick: OnPlaymateOrderMessageClickListener
) : BaseCenterMessageViewBinder<PlaymateOrderMessageBean, LayoutSessionMessagePlaymateOrderBinding, PlaymateOrderMessageViewHolder>(
    onItemClickListener
) {
    override fun onCreateMessageHolder(
        rootBinder: LayoutSessionMessageCenterBaseBinding,
        inflater: LayoutInflater,
        parent: ViewGroup
    ): PlaymateOrderMessageViewHolder {
        return PlaymateOrderMessageViewHolder(
            rootBinder,
            LayoutSessionMessagePlaymateOrderBinding.inflate(
                inflater,
                parent,
                true
            )
        ).apply {
            setOrderClickListener(onPlaymateClick)
        }
    }

}

interface OnPlaymateOrderMessageClickListener {
    fun onPlaymateStartService(data: OrderDetailInfo)

    fun onPlaymateCompleteService(data: OrderDetailInfo)
}