package com.persagy.revit.services.json

import com.google.common.base.Strings
import com.persagy.database.SFilter
import com.persagy.revit.models.entities.Room
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
import java.util.*

/**
 *
 *
 * @author Cainga
 */
object RoomAnalyzer {
    fun analyze(roomService: SObjectService<Room>, baseObj: BaseObj<AllComponents>, rooms: ArrayList<Room>?) : Boolean{
        if(rooms == null || rooms.isEmpty())
            return true
        for(obj in rooms){
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            if(obj.parameters != null){
                var code = ""
                for (param in obj.parameters!!){
                    if(param.name.equals("名称")){
                        if(param.value.isNullOrBlank()){
                            obj.localName = obj.name
                        }else
                            obj.localName = param.value
                    }else if(param.name.equals("编号") && param.value != null){
                        code = param.value
                    }
                }
                if(obj.localName == null || obj.localName!!.isEmpty() ){
                    obj.localName = java.lang.String.join("", Strings.nullToEmpty(obj.localName), Strings.nullToEmpty(code))
                }
            }
        }
        roomService.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<Room>()
        create.content = rooms
        val result = roomService.createList(create)
        return SResponseType.success == result.result
    }
}