package cn.sagacloud.server.algorithm.services.json

import cn.sagacloud.server.algorithm.models.entities.BaseComponent
import cn.sagacloud.server.algorithm.models.entities.Connector
import cn.sagacloud.server.algorithm.models.jsonAnalyzer.AllComponents
import cn.sagacloud.server.algorithm.models.jsonAnalyzer.BaseObj
import cn.sagacloud.server.algorithm.services.CommonService
import com.sybotan.database.SFilter
import com.sybotan.service.SObjectService
import com.sybotan.service.models.enums.SResponseType
import com.sybotan.service.models.requests.SCreateRequest

/**
 *
 *
 * @author jxing
 */
object RevitIdAnalyzer {
    fun analyze(service: SObjectService<BaseComponent>, baseObj: BaseObj<AllComponents>, arr: ArrayList<BaseComponent>?, revitIdMap: HashMap<String, BaseComponent>) : Boolean{
        if(arr == null || arr.isEmpty())
            return true
        for(obj in arr){
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            if(obj.revitId != null && obj.revitId!!.isNotEmpty())
                revitIdMap[obj.revitId!!] = obj
        }
        service.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<BaseComponent>()
        create.content = arr
        val result = service.createList(create)
        return SResponseType.success == result.result
    }

    fun analyzePipesDucts(service: SObjectService<BaseComponent>, baseObj: BaseObj<AllComponents>, arr: ArrayList<BaseComponent>?, revitIdMap: HashMap<String, BaseComponent>, elementType : String) : Boolean{
        if(arr.isNullOrEmpty())
            return true
        for(obj in arr){
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            if(obj.revitId != null && obj.revitId!!.isNotEmpty())
                revitIdMap[obj.revitId!!] = obj
        }
        service.delete(SFilter.and(SFilter.eq("modelId", baseObj.modelId!!), SFilter.eq("elementType", elementType)))
        val create = SCreateRequest<BaseComponent>()
        create.content = arr
        val result = service.createList(create)
        return SResponseType.success == result.result
    }

    fun analyzeConnector(connectorService: SObjectService<Connector>, baseObj: BaseObj<AllComponents>, connectors: ArrayList<Connector>?, bimIdPrefix: String, revitIdMap: HashMap<String, BaseComponent>) : Boolean{
        if(connectors == null || connectors.isEmpty())
            return true
        // revit id --> id
        val connectorMap = HashMap<String, String>()
        for(obj in connectors){
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            if(obj.owner != null && revitIdMap.containsKey(obj.owner!!)){
                val tmp = revitIdMap[obj.owner!!]
                obj.belong = tmp!!.id
                obj.belongType = tmp.elementType
                obj.belongBimId = "${bimIdPrefix}${tmp.sourceId}"
            }
            if(obj.revitId != null && obj.revitId!!.isNotEmpty())
                connectorMap[obj.revitId!!] = obj.id!!
        }
        for (obj in connectors){
            if(obj.connectedIds != null && obj.connectedIds!!.size > 0){
                val tmpArr = ArrayList<String>()
                for(tmpId in obj.connectedIds!!){
                    if(connectorMap.containsKey(tmpId)){
                        tmpArr.add(connectorMap[tmpId]!!)
                    }
                }
                if(obj.connectIdJson == null)
                    obj.connectIdJson = ArrayList()
                obj.connectIdJson!!.addAll(tmpArr)
            }
        }
        connectorService.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<Connector>()
        create.content = connectors
        val result = connectorService.createList(create)
        return SResponseType.success == result.result
    }
}