package com.cheng.blzb.ui.fragment.bid.publish.goods

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.UploadFileEntity
import com.cheng.blzb.net.ApiFactory
import com.cheng.blzb.utils.BitmapUtils
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel
import com.huantansheng.easyphotos.models.album.entity.Photo
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody

class PublishGoodsMessageViewModel: BaseViewModel() {
    val addLiveData = MutableLiveData<Any>()

    val imageLiveData = MutableLiveData<List<UploadFileEntity>>()
    val imgList = ArrayList<UploadFileEntity>()

    fun addGoods(requestBody: RequestBody) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.addGoodsInfo(requestBody)
            if (response.status) {
                addLiveData.postValue(Any())
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun updateGoods(requestBody: RequestBody) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.updateGoodsInfo(requestBody)
            if (response.status) {
                addLiveData.postValue(Any())
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    //删除文件
    fun deleteFile(id: String) {
        launchOnUiTryCatch({
            ApiFactory.apiService.delUserFile(id)
        }, {
            L.d(it)
        })
    }

    fun upload(context: Context, list: ArrayList<Photo>) {
        showDialog()
        list.forEachIndexed { index, photo ->
            if (index == 0) {
                uploadImg(context, photo) {
                    list.remove(photo)
                    if (list.isNotEmpty()) {
                        upload(context, list)
                    } else {
                        imageLiveData.postValue(imgList)
                        dismissDialog()
                    }
                }
            }
        }
    }

    private fun uploadImg(context: Context, item: Photo, clickFun: () -> Unit) {
        BitmapUtils.compressImg(context, item.path) {
            launchOnUiTryCatch({
                val requestFile = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), it)
                val filePart = MultipartBody.Part.createFormData("file", it.getName(), requestFile)
                val response = ApiFactory.apiService.upload(filePart, "supply")
                if (response.status) {
                    imgList.add(response.data)
                    clickFun.invoke()
                } else {
                    toast(response.message, true)
                    dismissDialog()
                }
            }, {
                dismissDialog()
                L.d(it)
            })
        }
    }
}