package com.adealink.weparty.order.refund.comp

import android.annotation.SuppressLint
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.util.formatTime
import com.adealink.weparty.commonui.ext.dp
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.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.module.image.data.PhotoData
import com.adealink.weparty.module.order.data.OrderDetailData
import com.adealink.weparty.order.databinding.LayoutOrderPlaymateRefundCustomerBinding
import com.adealink.weparty.order.refund.PlaymateEvidenceActivity.Companion.PICTURE_SPAN
import com.adealink.weparty.order.refund.adapter.PhotoItemViewBinder
import com.adealink.weparty.order.refund.data.BasePhotoItemData
import com.adealink.weparty.order.refund.data.PhotoItemData
import com.adealink.weparty.order.util.getOrderAllCost
import com.adealink.weparty.order.viewmodel.OrderViewModelFactory
import com.adealink.weparty.order.viewmodel.PlaymateRefundViewModel
import com.adealink.weparty.util.TIME_FORMAT_DMY_HMS
import com.adealink.weparty.util.goImagePreviewActivity

class PlaymateRefundCustomerComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutOrderPlaymateRefundCustomerBinding
) : ViewComponent(lifecycleOwner) {

    private val playmateRefundViewModel by activityViewModels<PlaymateRefundViewModel> { OrderViewModelFactory() }

    private val pictureAdapter by fastLazy { MultiTypeListAdapter<BasePhotoItemData>() }

    override fun onCreate() {
        super.onCreate()
        initView()
        observeViewModel()
    }

    private fun initView() {
        pictureAdapter.register(
            PhotoItemViewBinder(
                onItemClick = {
                    previewPicture(it.data)
                },
                onDeleteClick = {
                }
            ))
        binding.rvReasonPicture.adapter = pictureAdapter
        binding.rvReasonPicture.layoutManager = GridLayoutManager(context, PICTURE_SPAN)
        binding.rvReasonPicture.addItemDecoration(
            GridSpacingItemDecoration(PICTURE_SPAN, 4.dp(), 4.dp(), false)
        )
    }

    private fun observeViewModel() {
        playmateRefundViewModel.orderDetailLD.observe(viewLifecycleOwner) {
            updateOrderDetail(it)
        }
    }

    private fun previewPicture(data: PhotoData) {
        val uri = data.uri
        if (uri.isNullOrEmpty()) {
            return
        }
        val act = activity ?: return
        goImagePreviewActivity(
            act,
            arrayListOf(uri)
        )
    }

    @SuppressLint("SetTextI18n")
    private fun updateOrderDetail(data: OrderDetailData) {
        binding.ivOrderAvatar.setImageUrl(data.order.avatar)
        binding.tvOrderName.text = data.order.nickname

        binding.tvProductTypeDetail.text = data.order.categoryName
        binding.tvOrderCountDetail.text = "${data.order.purchaseQty} x ${data.order.unit}"
        binding.tvOrderIdDetail.text = data.order.orderId
        binding.tvOrderTimeDetail.text = formatTime(data.order.createTime, TIME_FORMAT_DMY_HMS)
        binding.tvTotalCostDetail.text = getOrderAllCost(
            data.order.price,
            data.order.purchaseQty,
            data.order.totalAmount,
            12.sp(),
            12.dp()
        )

        binding.tvReason.text = data.refundReason
        val imageItemList = data.refundAttachments?.map {
            PhotoItemData(
                PhotoData(url = it.attachmentUrl, path = null, uri = null),
                showDelete = false
            )
        }
        if (imageItemList.isNullOrEmpty()) {
            binding.tvReasonPictureTitle.gone()
            binding.rvReasonPicture.gone()
        } else {
            binding.tvReasonPictureTitle.show()
            binding.rvReasonPicture.show()
            pictureAdapter.submitList(imageItemList)
        }
    }
}