package com.cheng.blzb.ui.fragment.mine.feedback

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 FeedbackViewModel : BaseViewModel() {
    val feedbackLiveData = MutableLiveData<Any>()
    val uploadSuccessLiveData = MutableLiveData<List<UploadFileEntity>>()
    val imgList = ArrayList<UploadFileEntity>()

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

    fun delete(list: ArrayList<UploadFileEntity>) {
        list.forEachIndexed { index, photo ->
            if (index == 0) {
                delStoreImg(photo) {
                    list.remove(photo)
                    if (list.isNotEmpty()) {
                        delete(list)
                    }
                }
            }
        }
    }

    //删除文件
    fun delStoreImg(img: UploadFileEntity, delBack: (() -> Unit)?) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.delUserFile(img.id)
            if (response.status) {
                delBack?.invoke()
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            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 {
                        uploadSuccessLiveData.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, "feedback")
                if (response.status) {
                    imgList.add(response.data)
                    clickFun.invoke()
                } else {
                    toast(response.message, true)
                    dismissDialog()
                }
            }, {
                dismissDialog()
                L.d(it)
            })
        }
    }
}