package com.persagy.revit.services.json

import com.alibaba.fastjson.JSON
import com.persagy.base.extensions.toJson
import com.persagy.revit.services.CommonService
import com.persagy.database.SFilter
import com.persagy.revit.models.entities.*
import com.persagy.revit.models.jsonAnalyzer.AllComponents
import com.persagy.revit.models.jsonAnalyzer.BaseObj
import com.persagy.revit.models.jsonAnalyzer.Parameter
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SCreateRequest
import java.util.*
import kotlin.collections.HashMap

/**
 *
 *
 * @author jxing
 */
object EquipmentAnalyzer {
    fun analyze(equipmentService: SObjectService<Equipment>, baseObj: BaseObj<AllComponents>, equipments: ArrayList<Equipment>?, bimIdPrefix: String, equipMap: HashMap<String, BaseComponent>, flag: Boolean, dictCompService: SObjectService<DictComp>, infoPointCompService: SObjectService<InfoPointComp>): Boolean {
        if (equipments == null || equipments.isEmpty())
            return true
        for (obj in equipments) {
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            var family: String? = null
            val familyName = obj.familyName
            if (!familyName.isNullOrEmpty()) {
                family = familyName!!.substring(0, familyName.indexOf("-"))
                //设备编码转换
                if (flag) {
                    obj.initFamily = family
                    val dictComp = dictCompService.select(SFilter.eq("pFamily", family)).entity()
                    if (dictComp != null) {
                        obj.family = dictComp.family ?: family
                    } else {
                        obj.family = family
                    }
                } else {
                    obj.family = family
                }
            }
            obj.bimId = "${bimIdPrefix}${obj.sourceId}"
            if (obj.revitId != null && obj.revitId!!.isNotEmpty())
                equipMap[obj.revitId!!] = obj
            if (obj.parameters != null) {
//                //原始信息点存储
//                if (flag && null != family) {
//                    //拷贝源参数
//                    val json = obj.parameters!!.toJson();
//                    val parameters = JSON.parseObject(json,ArrayList<Parameter>().javaClass)
//                    obj.initParameters = parameters
//                }
                var infos = HashMap<String, String>()

                for (param in obj.parameters!!) {
                    if (param.name.equals("设备本地名称")) {
                        obj.localName = param.value
                    } else if (param.name.equals("设备本地编码")) {
                        obj.localId = param.value
                    } else if (param.name.equals("PropertyID", true)) {
                        obj.propertyId = param.value
                    } else if (param.name.equals("构件分类编码")) {
                        obj.bimTypeID = param.value
                    } else if (flag && null != family && !param.name.isNullOrEmpty()) {
                        //信息点编码转换
                        val infoPointComp = infoPointCompService.select(SFilter.eq("family", family), SFilter.eq("infoPointCode", param.name!!)).entity()
                        if (infoPointComp != null) {
                            infos[infoPointComp.sInfoPointCode!!] = param.value ?: ""
                        } else {
                            infos[param.name!!] = param.value ?: ""
                        }
                    }
                }

                obj.infos = infos
            }
        }
        equipmentService.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<Equipment>()
        create.content = equipments
        val result = equipmentService.createList(create)
        return SResponseType.success == result.result
    }

    fun analyzeComponent(componentService: SObjectService<EquipPart>, baseObj: BaseObj<AllComponents>, equipParts: ArrayList<EquipPart>?, bimIdPrefix: String, equipMap: HashMap<String, BaseComponent>): Boolean {
        if (equipParts == null || equipParts.isEmpty())
            return true
        for (obj in equipParts) {
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            if (obj.familyName != null && obj.familyName!!.length > 6) {
                obj.family = obj.familyName!!.substring(0, 6)
            }
            if (obj.owner != null && equipMap.containsKey(obj.owner!!)) {
                obj.ownerId = equipMap[obj.owner!!]!!.id
                obj.ownerSourceId = equipMap[obj.owner!!]!!.sourceId
            }
            obj.bimId = "${bimIdPrefix}${obj.sourceId}"
            if (obj.revitId != null && obj.revitId!!.isNotEmpty())
                equipMap[obj.revitId!!] = obj
            if (obj.parameters != null) {
                for (param in obj.parameters!!) {
                    if (param.name.equals("设备本地名称")) {
                        obj.localName = param.value
                    } else if (param.name.equals("设备本地编码")) {
                        obj.localId = param.value
                    } else if (param.name.equals("PropertyID", true)) {
                        obj.propertyId = param.value
                    } else if (param.name.equals("构件分类编码")) {
                        obj.bimTypeID = param.value
                    }
                }
            }
        }
        componentService.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<EquipPart>()
        create.content = equipParts
        val result = componentService.createList(create)
        return SResponseType.success == result.result
    }

}