package com.persagy.revit.services.json

import cn.sagacloud.server.algorithm.models.entities.*
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
import java.util.*
import kotlin.collections.HashMap

/**
 *
 *
 * @author jxing
 */
object EquipmentAnalyzer {

    /**
     * @Description: 第一版设备解析，后面可能会删除
     * 将设备的编码转换为标准编码
     */
    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) {
                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
    }

    /**
     * @Description: 第二版设备解析，正在使用的
     * 将设备的构建码转换为标准编码
     */
    fun analyze2(equipmentService: SObjectService<Equipment>, baseObj: BaseObj<AllComponents>, equipments: ArrayList<Equipment>?, bimIdPrefix: String, equipMap: HashMap<String, BaseComponent>, equipCompService: SObjectService<EquipComp>, infoPointCompService: SObjectService<InfoPointComp>): Boolean {
        if (equipments == null || equipments.isEmpty())
            return true


        val newEquipmnets = ArrayList<Equipment>()
        for (obj in equipments) {
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            var family: String? = null
            val familyName = obj.familyName
//            if (!flag && !familyName.isNullOrEmpty()) {
//                    family = familyName!!.substring(0, familyName.indexOf("-"))
//                    obj.family = family
//            }
            obj.bimId = "${bimIdPrefix}${obj.sourceId}"
            if (obj.revitId != null && obj.revitId!!.isNotEmpty())
                equipMap[obj.revitId!!] = obj
            if (obj.parameters != null) {
                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 (param.name.equals("系统分类")) {
                        obj.sysName = param.value
                    }


                    if (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 ?: ""
                        }
                    }
                }
                var equipComp: EquipComp? = null

                //bimtypeID转换,设备构建码没有抛弃，查不到抛弃
                if(! obj.bimTypeID.isNullOrEmpty()){
                    if(! obj.sysName.isNullOrEmpty()){
                        equipComp = equipCompService.select(SFilter.eq("bimTypeID",obj.bimTypeID!!), SFilter.eq("sysName",obj.sysName!!)).entity()
                        if(equipComp != null){
                            obj.family = equipComp.classCode
                        }
                    }else{
                        equipComp = equipCompService.select(SFilter.eq("bimTypeID",obj.bimTypeID!!)).entity()
                        if(equipComp != null){
                            obj.family = equipComp.classCode
                        }
                    }
                    if(!obj.family.isNullOrEmpty()){

                        //信息点编码转换
                        for (param in obj.parameters!!) {
                            if (param.name.equals("设备本地名称")) {

                            } else if (param.name.equals("设备本地编码")) {

                            } else if (param.name.equals("PropertyID", true)) {

                            } else if (param.name.equals("构件分类编码")) {

                            } else if (param.name.equals("系统分类")) {

                            }else{
                                //信息点编码转换
                                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 ?: ""
                                }
                            }

                        }

                    }else{
                        continue
                    }
                    obj.infos = infos
                    newEquipmnets.add(obj)
                }

            }
        }
        equipmentService.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<Equipment>()
        create.content = newEquipmnets
        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
    }

}