package com.casic.electric.detector.vm

import androidx.lifecycle.MutableLiveData
import com.casic.electric.detector.base.BaseApplication
import com.casic.electric.detector.extensions.convertChinese
import com.casic.electric.detector.extensions.separateResponseState
import com.casic.electric.detector.extensions.toErrorMessage
import com.casic.electric.detector.model.MarkerFileModel
import com.casic.electric.detector.retrofit.RetrofitServiceManager
import com.casic.electric.detector.utils.LabelDataClass
import com.casic.electric.detector.utils.SmallLabelDataClass
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.extensions.launch
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.vm.BaseViewModel
import com.pengxh.kt.lite.vm.LoadState

class TaskViewModel : BaseViewModel() {
    private val gson by lazy { Gson() }

    val markerFileResult = MutableLiveData<MarkerFileModel>()
//    val taskResult = MutableLiveData<TaskModel>()

    fun getMarkerFile(userId: String, companyId: String) = launch({
        val response = RetrofitServiceManager.getMarkerFile(userId, companyId)
        if (response.separateResponseState()) {
            markerFileResult.value = gson.fromJson<MarkerFileModel>(
                response, object : TypeToken<MarkerFileModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(BaseApplication.get())
        }
    }, {
        it.convertChinese().show(BaseApplication.get())
    })

//    fun getTask(userName: String) = launch({
//        val response = RetrofitServiceManager.getTask(userName)
//        if (response.separateResponseState()) {
//            taskResult.value = gson.fromJson<TaskModel>(
//                response, object : TypeToken<TaskModel>() {}.type
//            )
//        } else {
//            response.toErrorMessage().show(BaseApplication.get())
//        }
//    }, {
//        it.convertChinese().show(BaseApplication.get())
//    })

    fun installLabel(labelData: LabelDataClass) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.installLabel(labelData)
        if (response.separateResponseState()) {
            loadState.value = LoadState.Success
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.get())
        }
    }, {
        loadState.value = LoadState.Fail
        it.convertChinese().show(BaseApplication.get())
    })

    fun installSmallLabel(labelData: SmallLabelDataClass) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.installSmallLabel(labelData)
        if (response.separateResponseState()) {
            loadState.value = LoadState.Success
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.get())
        }
    }, {
        loadState.value = LoadState.Fail
        it.convertChinese().show(BaseApplication.get())
    })

//    fun uploadTask(userId: String, taskId: String?, state: String) = launch({
//        loadState.value = LoadState.Loading
//        val response = RetrofitServiceManager.uploadTask(userId, taskId, state)
//        if (response.separateResponseState()) {
//            loadState.value = LoadState.Success
//            if (state == "2") {
//                "工单${taskId}已提交！".show(BaseApplication.get())
//                //删除本地数据库
//                DataBaseManager.get.deleteTaskById(taskId)
//                SaveKeyValues.putValue(LocaleConstant.TASK_ID, "")
//            }
//        } else {
//            loadState.value = LoadState.Fail
//            response.toErrorMessage().show(BaseApplication.get())
//        }
//    }, {
//        loadState.value = LoadState.Fail
//        it.convertChinese().show(BaseApplication.get())
//    })

    fun uploadEvent(taskId: String, event: String, realPaths: ArrayList<String>) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.uploadEvent(taskId, event, realPaths)
        if (response.separateResponseState()) {
            loadState.value = LoadState.Success
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.get())
        }
    }, {
        loadState.value = LoadState.Fail
        it.convertChinese().show(BaseApplication.get())
    })

    fun uploadMarker(userName: String, taskDetailId: String) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.uploadMarker(userName, taskDetailId)
        if (response.separateResponseState()) {
            loadState.value = LoadState.Success
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.get())
        }
    }, {
        loadState.value = LoadState.Fail
        it.convertChinese().show(BaseApplication.get())
    })
}