package com.persagy.revit.services.json

import com.persagy.database.SFilter
import com.persagy.revit.models.entities.Wall
import com.persagy.revit.models.entities.WindowDoorBase
import com.persagy.revit.models.jsonAnalyzer.AllComponents
import com.persagy.revit.models.jsonAnalyzer.BaseObj
import com.persagy.revit.services.CommonService
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.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
    }
}