package com.casic.detector.vm

import androidx.lifecycle.MutableLiveData
import com.casic.detector.base.BaseApplication
import com.casic.detector.extensions.getResponseMessage
import com.casic.detector.extensions.getResponseState
import com.casic.detector.model.MarkerFileModel
import com.casic.detector.model.TaskModel
import com.casic.detector.retrofit.RetrofitServiceManager
import com.casic.detector.utils.DataBaseManager
import com.casic.detector.utils.LocaleConstant
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.base.BaseViewModel
import com.pengxh.kt.lite.extensions.launch
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.utils.LoadState
import com.pengxh.kt.lite.utils.SaveKeyValues

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

    val markerFileResult = MutableLiveData<MarkerFileModel>()
    val taskResult = MutableLiveData<TaskModel>()
    val freeTaskResult = MutableLiveData<String>()
    val uploadTaskMarkerResult = MutableLiveData<String>()

    fun createFreeTask(patrollerId: String, description: String) = launch({
        val response = RetrofitServiceManager.createFreeTask(patrollerId, description)
        if (response.getResponseState()) {
            val element = JsonParser.parseString(response)
            val jsonObject = element.asJsonObject
            freeTaskResult.value = jsonObject.get("taskId").asInt.toString()
        } else {
            response.getResponseMessage().show(BaseApplication.get())
        }
    }, {
        it.printStackTrace()
    })

    fun uploadTaskMarker(taskId: String?, models: ArrayList<String>) = launch({
        val response = RetrofitServiceManager.uploadTaskMarker(taskId, models)
        if (response.getResponseState()) {
            val element = JsonParser.parseString(response)
            val jsonObject = element.asJsonObject
            uploadTaskMarkerResult.value = jsonObject.get("success").asString
        } else {
            response.getResponseMessage().show(BaseApplication.get())
        }
    }, {
        it.printStackTrace()
    })

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

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

    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,
        realPaths: ArrayList<String>
    ) = launch({
        loadState.value = LoadState.Loading
        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,
            realPaths
        )
        if (response.getResponseState()) {
            loadState.value = LoadState.Success
        } else {
            loadState.value = LoadState.Fail
            response.getResponseMessage().show(BaseApplication.get())
        }
    }, {
        it.printStackTrace()
        loadState.value = LoadState.Fail
    })

    fun uploadTask(userId: String, taskId: String?, state: String) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.uploadTask(userId, taskId, state)
        if (response.getResponseState()) {
            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.getResponseMessage().show(BaseApplication.get())
        }
    }, {
        it.printStackTrace()
        loadState.value = LoadState.Fail
    })

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

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