package com.persagy.server.utils


import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.persagy.base.extensions.eq
import com.persagy.server.dao.mappers.EquipmentZoneMapper
import com.persagy.server.datacenter.models.entities.assistant.PointD
import com.persagy.server.datacenter.models.entities.objects.Equipment
import com.persagy.server.datacenter.models.entities.objects.Property
import com.persagy.server.models.SimilarityDegree
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory
import java.lang.reflect.Type
import java.util.*
import kotlin.collections.HashSet

/**
 * 根据设备匹配资产
 *
 * @author 张维新
 */
object MatchingProperty {

    /** 日志 */
    private val logger = LoggerFactory.getLogger(MatchingProperty::class.java)

        /** 自定义查询 */
        private val equipZoneMapper by lazy {
            SSpringContextUtil.getBean(EquipmentZoneMapper::class.java) as EquipmentZoneMapper
        }



    /**
     * 计算设备匹配的资产
     */
    fun equipMatchingProperty(equipment: Equipment, list: ArrayList<Property>): ArrayList<Property> {

        var propertyList = ArrayList<Property>()

            /** CADID 图纸中的id 判断 */
            if (!equipment.cadId.isNullOrEmpty()) {
//                if (equipment.cadId == "AA-A1-1(1#)-1"){
////                    logger.debug("CADID = ${equipment.cadId}************************************************************")
//                }

                    /** 设计图纸重编码 */
                    if (propertyList.size == 0){
                        /** 设计图纸中编码 */
                        for (property in list) {

                            if (!equipment.cadId.isNullOrEmpty()&&!property.cadId.isNullOrEmpty()&&property.cadId == equipment.cadId ){
                                propertyList.add(property)
                                if (propertyList.size>3){
                                    break
                                }
                            }
                        }
//                        logger.debug("匹配到的资产 = ${propertyList.size}************************************************************")
                    }
                if (!equipment.localId.isNullOrEmpty()){
                    if (propertyList.size == 0){
                        /** 判断本地编码 */
                        for (property in list) {
                            if (!property.localId.isNullOrEmpty()&&equipment.localId == property.localId){
                                propertyList.add(property)
                                if (propertyList.size>3){
                                    break
                                }
                            }
                        }
                    }
                }

                if (!equipment.localName.isNullOrEmpty()){

                    if (propertyList.size==0){
                        /** 判断本地名称 */
                        for (property in list) {

                            if (!property.localName.isNullOrEmpty()&&property.localName == equipment.localName ){
                                propertyList.add(property)
                                if (propertyList.size>3){
                                    break
                                }
                            }
                        }
                    }

                }
//                if (propertyList.size == 0) {
                    if (equipment.locationJson != null && equipment.locationJson!!.x != null && !equipment.locationJson!!.x!!.eq(0.0)
                            && equipment.locationJson!!.y != null && !equipment.locationJson!!.y!!.eq(0.0)) {
                        /** 格局坐标计算  */
                        val pointXYList = pointXY(equipment, list, propertyList)
                        if (pointXYList.size > 0) {
                            propertyList = pointXYList
                        }

                    } else if (!equipment.localName.isNullOrEmpty()) {
                        /** 相似度 */
                        val similarity = similarity(equipment, list, propertyList)
                        if (similarity.size > 0) {
                            propertyList = similarity
                        }
                    }
//                }
                if (propertyList.size >0){
                    /** 删除已经筛选出来的资产在总的资产列表中 */
                    for (i in propertyList.indices) {
                        val iterator = list.iterator()
                        while (iterator.hasNext()) {
                            var propertyl = iterator.next()
                            if (propertyList[i].id ==propertyl.id ){
                                iterator.remove()
                            }
                        }
                    }
                    return propertyList
                }

            } else if (!equipment.localId.isNullOrEmpty()) {
//                logger.debug("localId = ${equipment.localId}************************************************************")
                /** 判断本地编码 */
                for (property in list) {
                    if (!property.localId.isNullOrEmpty()&&equipment.localId == property.localId){
                        propertyList.add(property)
                        if (propertyList.size>3){
                            break
                        }
                    }
                }

                if (!equipment.cadId.isNullOrEmpty()){
                    /** 设计图纸重编码 */
                    if (propertyList.size == 0){
                        /** 设计图纸中编码 */
                        for (property in list) {

                            if (!equipment.cadId.isNullOrEmpty()&&!property.cadId.isNullOrEmpty()&&property.cadId == equipment.cadId ){
                                propertyList.add(property)
                                if (propertyList.size>3){
                                    break
                                }
                            }
                        }

                    }
                }

                if (!equipment.localName.isNullOrEmpty()){

                    if (propertyList.size==0){
                        /** 判断本地名称 */
                        for (property in list) {

                            if (!property.localName.isNullOrEmpty()&&property.localName == equipment.localName ){
                                propertyList.add(property)
                                if (propertyList.size>3){
                                    break
                                }
                            }
                        }
                    }

                }
//                if (propertyList.size==0) {
                    if (equipment.locationJson != null && equipment.locationJson!!.x != null && !equipment.locationJson!!.x!!.eq(0.0)
                            && equipment.locationJson!!.y != null && !equipment.locationJson!!.y!!.eq(0.0)) {
                        /** 格局坐标计算  */
                        val pointXYList = pointXY(equipment, list, propertyList)
                        if (pointXYList.size > 0) {
                            propertyList = pointXYList
                        }

                    } else if (!equipment.localName.isNullOrEmpty()) {
                        /** 相似度 */
                        val similarity = similarity(equipment, list, propertyList)
                        if (similarity.size > 0) {
                            propertyList = similarity
                        }
                    }
//                }
                if (propertyList.size >0){
                    /** 删除已经筛选出来的资产在总的资产列表中 */
                    for (i in propertyList.indices) {
                        val iterator = list.iterator()
                        while (iterator.hasNext()) {
                            var propertyl = iterator.next()
                            if (propertyList[i].id ==propertyl.id ){
                                iterator.remove()
                            }
                        }
                    }
                    return propertyList
                }
            } else if (!equipment.localName.isNullOrEmpty()) {
//                logger.debug("本地名称匹配：${equipment.localName}********************************************************")
                /** 判断本地名称 */
                for (property in list) {

                    if (!property.localName.isNullOrEmpty()&&property.localName == equipment.localName ){
                        propertyList.add(property)
                        if (propertyList.size>3){
                            break
                        }
                    }
                }

                if (propertyList.size == 0){
                    /** 设计图纸中编码 */
                    for (property in list) {

                        if (!equipment.cadId.isNullOrEmpty()&&!property.cadId.isNullOrEmpty()&&property.cadId == equipment.cadId ){
                            propertyList.add(property)
                            if (propertyList.size>3){
                                break
                            }
                        }
                    }

                }

                if (propertyList.size == 0){
                    /** 本地编码 */
                    for (property in list) {

                        if (!equipment.localId.isNullOrEmpty()&&!property.localId.isNullOrEmpty()&&property.localId == equipment.localId ){
                            propertyList.add(property)
                            if (propertyList.size>3){
                                break
                            }
                        }
                    }
                }

//                    if (propertyList.size == 0){
                        if (equipment.locationJson != null && equipment.locationJson!!.x != null && !equipment.locationJson!!.x!!.eq(0.0)
                                && equipment.locationJson!!.y != null && !equipment.locationJson!!.y!!.eq(0.0)) {
                            /** 格局坐标计算  */
                            val pointXYList = pointXY(equipment, list, propertyList)
                            if (pointXYList.size > 0) {
                                propertyList = pointXYList
                            }

                        }else  if (!equipment.localName.isNullOrEmpty()) {
                            /** 相似度 */
                            val similarity = similarity(equipment, list, propertyList)
                            if (similarity.size>0){
                                propertyList = similarity
                            }
                        }
//                    }

                if (propertyList.size >0){
                    /** 删除已经筛选出来的资产在总的资产列表中 */
                    for (i in propertyList.indices) {
                        val iterator = list.iterator()
                        while (iterator.hasNext()) {
                            var propertyl = iterator.next()
                            if (propertyList[i].id ==propertyl.id ){
                                iterator.remove()
                            }
                        }
                    }

                }
                return propertyList
            } else if (equipment.locationJson != null && equipment.locationJson!!.x != null && !equipment.locationJson!!.x!!.eq(0.0)
                    && equipment.locationJson!!.y != null && !equipment.locationJson!!.y!!.eq(0.0)) {
                /** 格局坐标计算  */
                val pointXYList = pointXY(equipment, list, propertyList)


                /** 获取模型空间数据,判断设备所在的模型空间 */
                if (!equipment.floorId.isNullOrEmpty()){

                }

                if (pointXYList.size>0){
                    return pointXYList
                }else{
                    if (!equipment.localName.isNullOrEmpty()) {
                        /** 相似度 */
                        propertyList = similarity(equipment, list, propertyList)
                        return propertyList
                    }
                }

            } else if (!equipment.localName.isNullOrEmpty()) {
                /** 相似度 */
                propertyList = similarity(equipment, list, propertyList)
                return propertyList
            }

        return propertyList
    }

    /**
     * 相似度 算法
     */
    fun similarityDegree(equipNmae: String, propertyName: String): Boolean {

        return false
    }

    /**
     * 相似度方法
     */
    fun similarity(equipment: Equipment, list: ArrayList<Property>,propertyList: ArrayList<Property> ): ArrayList<Property> {
        /** 相似度 */
        for (property in list) {
            if (!property.localName.isNullOrEmpty()&& similarityDegree(equipment.localName!!,property.localName!!)){
                propertyList.add(property)
                if(propertyList.size==2){
                    break
                }
            }
        }
        val setName = HashSet<String>()
        for (property in list) {
            if (!property.localName.isNullOrEmpty()){
                setName.add(property.localName!!)
            }
        }

        /** 匹配相似度 90% */
        if (setName.size>0){
            val similarList = equipZoneMapper.similarTwo(equipment.localName!!, setName.toList(),setName.toList().size)
            val gson = Gson()
            val type: Type = object : TypeToken<List<SimilarityDegree>>() {}.getType()
            val lists: List<SimilarityDegree> = gson.fromJson(similarList, type)
            if (lists.size>0){
                val setName = HashSet<String>()
                for (similarit in lists){
                    if (similarit.value!! >0.9){
                        setName.add(similarit.key!!)
                    }
                }
                if (setName.size>0){
                    for (property in list) {
                        for (name in setName){
                         if (name == property.localName){
                             propertyList.add(property)
                         }
                        }
                    }
                }
            }
        }

        if (propertyList.size>0){
            for (i in propertyList.indices) {
                val iterator = list.iterator()
                while (iterator.hasNext()) {
                    var propertyl = iterator.next()
                    if (propertyList[i].id ==propertyl.id ){
                        iterator.remove()
                    }
                }
            }
//            for (i in propertyList.indices) {
//                for ( propertyL  in  list){
//                    if (propertyList[i].id ==propertyL.id ){
//                        list.remove(propertyL)
//                    }
//                }
//            }
        }
        return propertyList
    }


    /**
     * 根据坐标x,y
     */
    fun  pointXY(equipment: Equipment, list: ArrayList<Property>,propertyList: ArrayList<Property> ): ArrayList<Property> {
        val distancePropertyList = ArrayList<Property>()

        for (property in list) {
            if (property.locationJson!=null&&property.locationJson!!.x!=null&&!property.locationJson!!.x!!.eq(0.0)&&!property.locationJson!!.y!!.eq(0.0)){
                /**  计算两点间的距离  */
                val point = PointD(equipment.locationJson!!.x!!,equipment.locationJson!!.y!!)
                val distance = point.distance(property.locationJson!!.x!!, property.locationJson!!.y!!)
                property.distance = distance
                distancePropertyList.add(property)
            }
        }
        /** 排序 从小到大 */
        distancePropertyList.sortBy {
            it.distance
        }

        for (i in distancePropertyList.indices) {
            if (i<=2){
                propertyList.add(distancePropertyList[i])
            }
        }


        for (i in propertyList.indices) {
            val iterator = list.iterator()
            while (iterator.hasNext()) {
                var propertyl = iterator.next()
                if (propertyList[i].id ==propertyl.id ){
                    iterator.remove()
                }

            }
        }

//        for (i in propertyList.indices) {
//            for ( propertyL  in  list){
//                if (propertyList[i].id ==propertyL.id ){
//                    list.remove(propertyL)
//                }
//            }
//        }

        return propertyList
    }

} // Object MatchingProperty