package com.casic.detector.retrofit

import com.casic.detector.utils.LocaleConstant
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.utils.RetrofitFactory
import com.pengxh.kt.lite.utils.SaveKeyValues
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody

object RetrofitServiceManager {
    private val api by lazy {
        val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String
        val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String
        val httpConfig = "http://${serverIp}:${serverPort}"
        RetrofitFactory.createRetrofit<RetrofitService>(httpConfig)
    }
    private val gson by lazy { Gson() }
    private val typeToken = object : TypeToken<Array<String>>() {}.type

    /**
     * 登录
     */
    suspend fun login(account: String, password: String): String {
        return api.login(account, password)
    }

    /**
     * 更新版本
     * */
    suspend fun getApplicationVersion(): String {
        return api.getApplicationVersion()
    }

    /**
     * 获取标识器信息文件
     */
    suspend fun getMarkerFile(userId: String, companyId: String): String {
        return api.getMarkerFile(userId, companyId)
    }

    /**
     * 获取标识器图片
     */
    suspend fun getLabelPicture(markerId: String): String {
        val param = JsonObject()
        param.addProperty("markerId", markerId)
        val requestBody = param.toString().toRequestBody(
            "application/json;charset=UTF-8".toMediaType()
        )
        return api.getLabelPicture(requestBody)
    }

    /**
     * 获取巡检工单
     */
    suspend fun getTask(userName: String): String {
        return api.getTask(userName)
    }

    /**
     * 提交工单
     */
    suspend fun uploadTask(userId: String, taskId: String?, state: String): String {
        return api.uploadTask(userId, taskId.toString(), state)
    }

    /**
     * 安装新标识器
     */
    suspend 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
    ): String {
        val param = JsonObject()
        param.addProperty("companyId", companyId)
        param.addProperty("recordType", recordType)
        param.addProperty("markerObjectType", markerObjectType)
        param.addProperty("pipeMaterial", pipeMaterial)
        param.addProperty("pipeDiameter", pipeDiameter)
        param.addProperty("depth", depth)
        param.addProperty("belowType", belowType)
        param.addProperty("belowMaterial", belowMaterial)
        param.addProperty("belowDiameter", belowDiameter)
        param.addProperty("belowDepth", belowDepth)
        param.addProperty("layStyle", layStyle)
        param.addProperty("area", area)
        param.addProperty("line", line)
        param.addProperty("road", road)
        param.addProperty("constructTime", constructTime)
        param.addProperty("ownerComp", ownerComp)
        param.addProperty("markerObjectId", markerObjectId)
        param.addProperty("markerId", markerId)
        param.addProperty("markerType", markerType)
        param.addProperty("markerDepth", markerDepth)
        param.addProperty("creator", creator)
        param.addProperty("createTime", createTime)
        param.addProperty("longitude", longitude)
        param.addProperty("latitude", latitude)
        param.addProperty("colorType", colorType)
        param.addProperty("memo", memo)
        val requestBody = param.toString().toRequestBody(
            "application/json;charset=UTF-8".toMediaType()
        )
        return api.installLabel(requestBody)
    }
}