package com.persagy.revit.services.json

import com.persagy.revit.models.entities.Unit
import com.persagy.revit.models.jsonAnalyzer.AllComponents
import com.persagy.revit.models.jsonAnalyzer.BaseObj
import com.persagy.revit.models.modelFile.ModelFile
import com.persagy.service.SObjectService

/**
 *
 *
 * @author jxing
 */
object ModelAnalyzer {

    fun analyze(modelService: SObjectService<ModelFile>, modelFile: ModelFile, baseObj: BaseObj<AllComponents>?) : Boolean {
        modelFile.planName = baseObj!!.planName
        if(baseObj.mbiName != null && baseObj.mbiName!!.startsWith("Pj") && baseObj.mbiName!!.length > 11){
            modelFile.mbiName = baseObj.mbiName
            modelFile.projectId = baseObj.mbiName!!.substring(0, 12)
        }
        val unit = findLengthUnit(baseObj)
        if(unit != null){
            modelFile.units = unit
        }
        modelFile.baseLevel = baseObj.baseLevel
        return modelService.update(modelFile)
    }

    private fun findLengthUnit(baseObj: BaseObj<AllComponents>) : Unit? {
        val units = baseObj.elements!!.units ?: return null
        for(item in units){
            if(item.name == null)
                continue
            if(item.name.toLowerCase() == "length"){
                return item
            }
        }
        return null
    }
}