package com.adealink.weparty.order.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.oss.data.UploadFile
import com.adealink.frame.util.getFileName
import com.adealink.weparty.App
import com.adealink.weparty.image.EVIDENCE_IMAGE_MAX_HEIGHT
import com.adealink.weparty.image.EVIDENCE_IMAGE_MAX_SIZE_KB
import com.adealink.weparty.image.EVIDENCE_IMAGE_MAX_WIDTH
import com.adealink.weparty.image.EVIDENCE_IMAGE_MIN_QUALITY
import com.adealink.weparty.module.image.data.PhotoData
import com.adealink.weparty.module.order.data.OrderAttachment
import com.adealink.weparty.module.order.data.OrderDetailData
import com.adealink.weparty.module.order.data.OrderDetailInfo
import com.adealink.weparty.module.order.data.PlaymateSubmitEvidenceReq
import com.adealink.weparty.order.datasource.remote.OrderHttpService
import com.adealink.weparty.order.refund.data.AddPhotoItemData
import com.adealink.weparty.order.refund.data.BasePhotoItemData
import com.adealink.weparty.order.refund.data.PhotoItemData
import com.adealink.weparty.util.uploadPhotos
import kotlinx.coroutines.launch

class PlaymateRefundViewModel : BaseViewModel() {

    companion object {
        private const val MAX_PICTURE = 6
    }

    private val orderHttpService by lazy {
        App.instance.networkService.getHttpService(OrderHttpService::class.java)
    }


    val orderDetailLD = MutableLiveData<OrderDetailData>()
    fun updateOrderDetail(detail: OrderDetailData?) {
        orderDetailLD.send(detail)
    }

    val pictureList = mutableListOf<PhotoData>()

    val pictureItemListLD = MutableLiveData<List<BasePhotoItemData>>()

    fun addPicture(path: String?, uri: String?) {
        viewModelScope.launch {
            pictureList.add(PhotoData(url = null, path = path, uri = uri))
            onPictureListChanged()
        }
    }

    fun removePicture(data: PhotoData) {
        viewModelScope.launch {
            pictureList.remove(data)
            onPictureListChanged()
        }
    }

    private fun onPictureListChanged() {
        val itemList = mutableListOf<BasePhotoItemData>()
        itemList.addAll(
            pictureList.map {
                PhotoItemData(it)
            }
        )
        //小于9张图片,可以继续添加图片
        if (pictureList.size < MAX_PICTURE) {
            itemList.add(AddPhotoItemData)
        }
        pictureItemListLD.send(itemList)
    }

    fun playmateRefundEvidence(
        order: OrderDetailInfo,
        reason: String?,
    ): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            //更新图片
            val uploadList = mutableListOf<PhotoData>()
            uploadList.addAll(pictureList)
            val attachments = mutableListOf<OrderAttachment>()
            if (uploadList.isNotEmpty()) {
                val rlt = uploadPhotos(
                    pictureList,
                    EVIDENCE_IMAGE_MAX_WIDTH,
                    EVIDENCE_IMAGE_MAX_HEIGHT,
                    EVIDENCE_IMAGE_MAX_SIZE_KB,
                    EVIDENCE_IMAGE_MIN_QUALITY,
                    UploadFile.FileType.ATTACHMENT
                )
                when (rlt) {
                    is Rlt.Failed -> {
                        liveData.send(rlt)
                        return@launch
                    }

                    is Rlt.Success -> {
                        for (data in rlt.data) {
                            attachments.add(
                                OrderAttachment(
                                    attachmentUrl = data.url,
                                    type = 0,
                                    fileName = getFileName(data.path ?: "")
                                )
                            )
                        }
                    }
                }
            }

            val rlt = orderHttpService.playmateSubmitRefundEvidence(
                PlaymateSubmitEvidenceReq(
                    order.orderId,
                    reason ,
                    attachments
                )
            )
            liveData.send(rlt)
        }
        return liveData
    }

}