package com.casic.detector.vm

import androidx.lifecycle.MutableLiveData
import com.casic.detector.base.BaseApplication
import com.casic.detector.extensions.convertChinese
import com.casic.detector.extensions.separateResponseState
import com.casic.detector.extensions.toErrorMessage
import com.casic.detector.model.MarkerFileModel
import com.casic.detector.model.MarkerImageModel
import com.casic.detector.model.TaskModel
import com.casic.detector.retrofit.RetrofitServiceManager
import com.casic.detector.utils.DataBaseManager
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 markerImageResult = MutableLiveData<MarkerImageModel>()
    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 getLabelPicture(markerId: String) = launch({
        val response = RetrofitServiceManager.getLabelPicture(markerId)
        if (response.separateResponseState()) {
            markerImageResult.value = gson.fromJson<MarkerImageModel>(
                response, object : TypeToken<MarkerImageModel>() {}.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(
        companyId: String,
        recordType: String,
        markerObjectType: String,
        pipeMaterial: String,
        pipeDiameter: String,
        depth: String,
        belowType: String,
        belowMaterial: String,
        belowDiameter: String,
        belowDepth: String,
        layStyle: String,
        area: String,
        line: String,
        road: String,
        constructTime: String,
        ownerComp: String,
        markerObjectId: String,
        markerId: String,
        markerType: String,
        markerDepth: String,
        creator: String,
        createTime: String,
        longitude: String,
        latitude: String,
        colorType: String,
        memo: String
    ) = launch({
        val response = RetrofitServiceManager.installLabel(
            companyId,
            recordType,
            markerObjectType,
            pipeMaterial,
            pipeDiameter,
            depth,
            belowType,
            belowMaterial,
            belowDiameter,
            belowDepth,
            layStyle,
            area,
            line,
            road,
            constructTime,
            ownerComp,
            markerObjectId,
            markerId,
            markerType,
            markerDepth,
            creator,
            createTime,
            longitude,
            latitude,
            colorType,
            memo
        )
        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 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
            //删除本地数据库
            DataBaseManager.get.deleteTaskById(taskId)
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.get())
        }
    }, {
        loadState.value = LoadState.Fail
        it.convertChinese().show(BaseApplication.get())
    })
}