package com.adealink.weparty.call.comp

import android.annotation.SuppressLint
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.viewmodel.viewModels
import com.adealink.frame.util.formatTime
import com.adealink.weparty.call.databinding.LayoutCallOrderBinding
import com.adealink.weparty.call.viewmodel.CallOrderViewModel
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.order.data.OrderDetailInfo
import com.adealink.weparty.module.order.data.OrderStatus
import com.adealink.weparty.module.order.util.getPlaymateOrderStatusText
import com.adealink.weparty.module.order.util.getUserOrderStatusText
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.util.TIME_FORMAT_DMY_HMS
import com.tencent.qcloud.tuikit.tuicallkit.common.utils.CallUtils
import io.trtc.tuikit.atomicxcore.api.call.CallParticipantStatus

class CallOrderComp(
    lifecycleOwner: LifecycleOwner,
    private val binding: LayoutCallOrderBinding
) : BaseCallViewComponent(lifecycleOwner) {

    private val orderViewModel by viewModels<CallOrderViewModel>()
    private var orderDetailInfo: OrderDetailInfo? = null

    override fun onResume() {
        super.onResume()
        val peerUid = CallUtils.getPeerUid()
        if (peerUid.isNullOrEmpty()) {
            return
        }
        orderViewModel.loadTopInfo(peerUid)
    }

    override fun observeViewModel() {
        super.observeViewModel()
        orderViewModel.pendingOrderLd.observeWithoutCache(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                    this@CallOrderComp.orderDetailInfo = null
                    updateOrderInfo()
                }

                is Rlt.Success -> {
                    this@CallOrderComp.orderDetailInfo = rlt.data
                    updateOrderInfo()
                }
            }
        }
    }

    override fun onSelfCallStatusChanged(status: CallParticipantStatus) {
        super.onSelfCallStatusChanged(status)
        updateOrderInfo()
    }


    @SuppressLint("SetTextI18n")
    private fun updateOrderInfo() {
        if (callStatus == CallParticipantStatus.None) {
            binding.root.gone()
            return
        }
        val orderInfo = this@CallOrderComp.orderDetailInfo
        if (orderInfo == null) {
            binding.root.gone()
            return
        }
        binding.root.show()
        binding.ivIcon.setImageUrl(orderInfo.categoryIcon)
        binding.tvName.text = orderInfo.categoryName
        binding.tvTime.text = formatTime(orderInfo.createTime, TIME_FORMAT_DMY_HMS)
        binding.tvCount.text = "x${orderInfo.purchaseQty} ${orderInfo.unit}"
        binding.tvOrderStatus.text = if (orderInfo.buyerUserNo == ProfileModule.getMyUid()) {
            getUserOrderStatusText(OrderStatus.map(orderInfo.status), false)
        } else {
            getPlaymateOrderStatusText(OrderStatus.map(orderInfo.status), false)
        }
    }
}