package com.persagy.revit.services.datacenter

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.TypeReference
import com.persagy.revit.backstage.model.*
import com.persagy.revit.models.config.MyProperties
import com.persagy.revit.models.request.BindFloorRequest
import com.persagy.revit.services.http.HttpService
import com.persagy.service.models.responses.SBaseResponse
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class DataCenterRequest{

    @Autowired
    lateinit var properties: MyProperties

    companion object{
        private val logger = LoggerFactory.getLogger(DataCenterRequest::class.java)
    }

    /**
     * 设备创建
     *
     */
    fun equipCreate(request : CreateRequest<DCEquipment>, projectId : String) : CreateResponse<DCEquipment>?{
        return try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.equipCreate, postBody, projectId)
            JSON.parseObject(resultStr, object : TypeReference<CreateResponse<DCEquipment>>() { })
        } catch (e: Exception) {
            null
        }
    }

    /**
     * 模型构件创建
     *
     */
    fun modelComponentCreate(request : CreateRequest<Any>, projectId : String, operator : DataCenterService.DataCenterOperator) : SBaseResponse?{
        return try {
            val postBody = JSON.toJSONString(request)
            var resultStr : String = ""
            when(operator){
                DataCenterService.DataCenterOperator.virtualWallCreate -> {
                    resultStr = HttpService.sendPostDataCenter(properties.datacenter.vwallCreate, postBody, projectId)
                }
                DataCenterService.DataCenterOperator.doorCreate -> {
                    resultStr = HttpService.sendPostDataCenter(properties.datacenter.doorCreate, postBody, projectId)
                }
                DataCenterService.DataCenterOperator.windowCreate -> {
                    resultStr = HttpService.sendPostDataCenter(properties.datacenter.windowCreate, postBody, projectId)
                }
            }
            JSON.parseObject(resultStr, object : TypeReference<SBaseResponse>() { })
        } catch (e: Exception) {
            null
        }
    }

    /**
     * 设备查询
     *
     */
    fun equipQuery(request : QueryRequest, projectId : String) : QueryResponse<DCEquipment>? {
        val postBody = JSON.toJSONString(request)
        val resultStr : String
        try {
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.equipQuery, postBody, projectId)
//            if(T::class.java == DCEquipment::class.java)
//                return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<DCEquipment>>() {})
//            else
                return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<DCEquipment>>() {})
        }catch (e : Exception){
            logger.error(e.message)
            return null
        }
    }

    /**
     * 设备查询
     *
     */
    fun equipQueryJSONObject(request : QueryRequest, projectId : String) : QueryResponse<JSONObject>? {
        val postBody = JSON.toJSONString(request)
        val resultStr : String
        try {
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.equipQuery, postBody, projectId)
//            if(T::class.java == DCEquipment::class.java)
//                return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<DCEquipment>>() {})
//            else
            return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<JSONObject>>() {})
        }catch (e : Exception){
            logger.error(e.message)
            return null
        }
    }

    /**
     * 设备删除
     *
     */
    fun equipDelete(request : ArrayList<DCEquipment>, projectId : String) : BaseResponse?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.equipDelete, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 设备更新
     *
     */
    fun equipUpdate(request : UpdateRequest<DCEquipment>, projectId : String) : BaseResponse?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.equipUpdate, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 元空间创建
     */
    fun iSpaceCreate(request : CreateRequest<DCISpace>, projectId : String) : CreateResponse<DCISpace>?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr = HttpService.sendPostDataCenter(properties.datacenter.ispaceCreate, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<CreateResponse<DCISpace>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 元空间查询
     */
    fun iSpaceQuery(request : QueryRequest, projectId : String) : QueryResponse<DCISpace>?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr = HttpService.sendPostDataCenter(properties.datacenter.ispaceQuery, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<DCISpace>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 元空间查询
     */
    fun iSpaceQueryJSONObject(request : QueryRequest, projectId : String) : QueryResponse<JSONObject>?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr = HttpService.sendPostDataCenter(properties.datacenter.ispaceQuery, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<JSONObject>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 元空间删除
     */
    fun iSpaceDelete(request : ArrayList<DCISpace>, projectId : String) : BaseResponse?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr = HttpService.sendPostDataCenter(properties.datacenter.ispaceDelete, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 元空间更新
     */
    fun iSpaceUpdate(request : UpdateRequest<DCISpace>, projectId : String) : BaseResponse?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr = HttpService.sendPostDataCenter(properties.datacenter.ispaceUpdate, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 楼层查询
     */
    fun floorQuery(request : QueryRequest, projectId : String) : QueryResponse<DCFloor>?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr = HttpService.sendPostDataCenter(properties.datacenter.floorQuery, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<DCFloor>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 建筑查询
     */
    fun buildingQuery(request : QueryRequest, projectId : String) : QueryResponse<DCBuilding>?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr = HttpService.sendPostDataCenter(properties.datacenter.floorQuery, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<DCBuilding>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 楼层更新
     */
    fun floorUpdate(request : UpdateRequest<DCFloor>, projectId : String) : BaseResponse?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr = HttpService.sendPostDataCenter(properties.datacenter.floorUpdate, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }


    /**
     * 部件创建
     * isEquipOrComponent true : Equip, false : Component
     */
    fun componentCreate(request : CreateRequest<DCComponent>, projectId : String) : CreateResponse<DCComponent>?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.componentCreate, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<CreateResponse<DCComponent>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 部件查询
     */
    fun componentQuery(request : QueryRequest, projectId : String) : QueryResponse<DCComponent>? {
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.componentQuery, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<DCComponent>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 资产查询
     */
    fun propertyQuery(request : QueryRequest, projectId : String) : QueryResponse<DCProperty>? {
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.propertyQuery, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<DCProperty>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 部件查询
     */
    fun componentQueryJSONObject(request : QueryRequest, projectId : String) : QueryResponse<JSONObject>? {
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.componentQuery, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<QueryResponse<JSONObject>>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 部件删除
     *
     */
    fun componentDelete(request : ArrayList<DCComponent>, projectId : String) : BaseResponse?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.componentDelete, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 部件更新
     *
     */
    fun componentUpdate(request : UpdateRequest<DCComponent>, projectId : String) : BaseResponse?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.componentUpdate, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 部件更新
     *
     */
    fun propertyUpdate(request : UpdateRequest<DCProperty>, projectId : String) : BaseResponse?{
        try {
            val postBody = JSON.toJSONString(request)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.propertyUpdate, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }

    /**
     * 绑定楼层接口
     */
    fun bindFloor(body: BindFloorRequest, projectId: String): BaseResponse? {
        try {
            val postBody = JSON.toJSONString(body)
            val resultStr : String
            resultStr = HttpService.sendPostDataCenter(properties.datacenter.floorBind, postBody, projectId)
            return JSON.parseObject(resultStr, object : TypeReference<BaseResponse>() { })
        } catch (e: Exception) {
        }
        return null
    }
}