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

import android.content.Context
import androidx.lifecycle.MutableLiveData
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 com.cheng.bole.net.ApiFactory
import com.cheng.bole.utils.BitmapUtils
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody

class FeedbackViewModel : BaseViewModel() {
    val feedbackLiveData = MutableLiveData<Any>()
    val uploadSuccessLiveData = MutableLiveData<List<com.cheng.bole.bean.UploadImgEntity>>()
    val imgList = ArrayList<com.cheng.bole.bean.UploadImgEntity>()

    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<com.cheng.bole.bean.UploadImgEntity>) {
        list.forEachIndexed { index, photo ->
            if (index == 0) {
                delStoreImg(photo) {
                    list.remove(photo)
                    if (list.size > 0) {
                        delete(list)
                    }
                }
            }
        }
    }

    //删除文件
    fun delStoreImg(img: com.cheng.bole.bean.UploadImgEntity, 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.size > 0) {
                        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)
            })
        }
    }
}