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

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.CompanyEntity
import com.cheng.blzb.bean.UploadFileEntity
import com.cheng.blzb.bean.UserEntity
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 okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody

class CompanyAuthViewModel: BaseViewModel() {
    val userInfoLiveData = MutableLiveData<UserEntity>()
    val companyLiveData = MutableLiveData<CompanyEntity>()
    val addLiveData = MutableLiveData<Any>()
    val imageLiveData = MutableLiveData<UploadFileEntity>()

    fun userInfo() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.userInfo()
            if (response.status) {
                userInfoLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun getCompanyInfo() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getCompanyInfo()
            if (response.status) {
                companyLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

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

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

    fun uploadImage(context: Context, path: String) {
        showDialog()
        BitmapUtils.compressImg(context, 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, "company")
                if (response.status) {
                    imageLiveData.postValue(response.data)
                } else toast(response.message, true)
                dismissDialog()
            }, {
                dismissDialog()
                setError(it)
                L.d(it)
            })
        }
    }

    fun deleteImage(id: String) {
        launchOnUiTryCatch({
            ApiFactory.apiService.delUserFile(id)
        }, {
            L.d(it)
        })
    }
}