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

import cn.sagacloud.server.algorithm.models.entities.Wall
import cn.sagacloud.server.algorithm.models.entities.WindowDoorBase
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 DoorWindowAnalyzer {
    fun analyze(service: SObjectService<WindowDoorBase>, baseObj: BaseObj<AllComponents>, windowDoors: ArrayList<WindowDoorBase>?) : Boolean{
        if(windowDoors == null || windowDoors.isEmpty())
            return true
        for(dw in windowDoors){
            dw.id = CommonService.generateUUID(null, true)
            dw.modelId = baseObj.modelId
        }
        val walls = baseObj.elements!!.walls
        if(walls != null && !walls.isEmpty()){
            val wallMap = HashMap<String, Wall>()
            walls.stream().forEach { wall ->
                if(wall?.id != null && !wallMap.containsKey(wall.id!!))
                    wallMap[wall.revitId!!] = wall as Wall
            }
            for(dw in windowDoors){
                if(wallMap.containsKey(dw.owner))
                    dw.wallId = wallMap.get(dw.owner)!!.id
            }
        }
        service.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<WindowDoorBase>()
        create.content = windowDoors
        val result = service.createList(create)
        return result.result == SResponseType.success
    }
}