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

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
import java.io.File

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

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

    val fileLiveData = MutableLiveData<List<UploadFileEntity>>()
    val fileList = ArrayList<UploadFileEntity>()

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

    fun updateBid(requestBody: RequestBody) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.updateBidInfo(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 uploadImages(context: Context, list: ArrayList<Photo>) {
        showDialog()
        list.forEachIndexed { index, photo ->
            if (index == 0) {
                uploadImg(context, photo) {
                    list.remove(photo)
                    if (list.isNotEmpty()) {
                        uploadImages(context, list)
                    } else {
                        imageLiveData.postValue(imgList)
                        dismissDialog()
                    }
                }
            }
        }
    }

    fun uploadFiles(list: ArrayList<File>) {
        showDialog()
        list.forEachIndexed { index, file ->
            if (index == 0) {
                uploadFile(file) {
                    list.remove(file)
                    if (list.size > 0) {
                        uploadFiles(list)
                    } else {
                        fileLiveData.postValue(fileList)
                        dismissDialog()
                    }
                }
            }
        }
    }

    private fun uploadImg(context: Context, photo: Photo, clickFun: () -> Unit) {
        BitmapUtils.compressImg(context, photo.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)
            })
        }
    }

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