package com.persagy.server.syn.models.tools

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.persagy.base.extensions.toDouble
import com.persagy.base.extensions.toInt
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.assistant.PointPosition
import com.persagy.server.datacenter.models.entities.dictnew.DefFuncId
import com.persagy.server.datacenter.models.entities.objects.Floor
import com.persagy.server.syn.Opts
import com.persagy.service.SObjectService
import com.persagy.service.utils.SSpringContextUtil

/**
 * 楼层 - 中台转数据中心
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/19 15:01
 */
object FloorUtil {
    /** 信息点 */
    private val dictDefFuncIdService = SObjectService(SMybatisDao(DefFuncId::class.java))
    /** 配置 */
    private val opts: Opts by lazy {
        SSpringContextUtil.getBean(Opts::class.java) as Opts
    }

    /**
     * 楼层
     *
     * @param projectId     项目 id
     * @param groupCode     集团编码
     * @param mapObj        对象数据
     * @return 楼层对象
     */
    fun floorSyn(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): Floor {
        /** 查建楼层型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode", "floor")).exec()
        val mapEx = HashMap<String, Any?>()
        val mapCode = HashMap<String, Any?>()

        for (explainProject in dictDefFuncIdList) {
            mapEx[explainProject.code!!] = explainProject.firstName
            if (!explainProject.secondName.isNullOrEmpty()) {
                mapCode[explainProject.code!!] = explainProject.secondName
            }
        }

        /** 结构信息 */
        val structureInfo = HashMap<String, Any?>()
        /** 人员信息 */
        val peopleInfo = HashMap<String, Any?>()
        /** 工作历信息 */
        val workCalendar = HashMap<String, Any?>()

        val floor = Floor()
        floor.projectId = projectId
        /** id 是否存在 */
        val containsKeyId = mapObj.containsKey("id")
        if(containsKeyId){
            floor.id = mapObj["id"].toString()
        }
        /** name 是否存在 */
        val containsKeyName = mapObj.containsKey("name")
        if(containsKeyName){
            floor.name = mapObj["name"].toString()
        }

        for (key in mapObj.keys) {
            val containsKey = mapEx.containsKey(key)
            if (containsKey) {
                val value = mapEx[key]
                if (value == opts.StructureInfo) {
                    /** 结构信息 */
                    structureInfo[key] = mapObj[key]
                } else if (value == opts.PeopleInfo) {
                    /** 人员信息 */
                    peopleInfo[key] = mapObj[key]
                } else if (value == "工作历信息") {
                    workCalendar[key] = mapObj[key]
                } else {
                    when (key) {
                        "localId" -> floor.localId = mapObj[key].toString()
                        "localName" -> floor.localName = mapObj[key].toString()
                        "bimId" -> floor.bimId = mapObj[key].toString()
                        "defaultQRCode" -> floor.defaultQRCode = mapObj[key].toString()
                        "qRCodePic" -> floor.qRCodePic = mapObj[key].toString()
                        "bimIdPre"  -> floor.bimIdPre = mapObj[key].toString()
                        "modelId"  ->  floor.modelId = mapObj[key].toString()
                        "bimLocation" -> floor.bimLocation = mapObj[key].toString()
                        "permanentPeopleNum" -> floor.permanentPeopleNum = mapObj[key]?.toInt()
                        "floorIdentity" -> floor.floorIdentity = mapObj[key].toString()
                        "floorSequenceId" -> {
                            if (!mapObj[key].toString().isNullOrEmpty() && mapObj[key].toString() != "null") {
                                floor.floorSequenceId = mapObj[key]?.toString()?.toInt()
//                                logger.debug("FloorSequenceID =${infos[key]!!.toString().toInt()}")
                            }
                        }
                        "outline" -> {
                            try {
                                val type = object : TypeToken<ArrayList<PointPosition>>() {
                                }.getType()
                                var outline=  Gson().fromJson<ArrayList<PointPosition>>(mapObj[key].toString(),type)
                                floor.outline = outline
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                    }
                }
            }
        }
        if (structureInfo.size > 0) {
            floor.structureInfo = structureInfo
        }
        if (peopleInfo.size > 0) {
            floor.peopleInfo = peopleInfo
        }
        if (workCalendar.size > 0) {
            floor.workCalendar = workCalendar
        }

        return floor
    }

    /**
     * 楼层
     *
     * @param projectId     项目 id
     * @param groupCode     集团编码
     * @param mapObj        对象数据
     * @return 楼层对象
     */
    fun floorSyns(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): Floor {
        /** 查建楼层型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode", "floor")).exec()
        val mapEx = HashMap<String, Any?>()
        val mapCode = HashMap<String, Any?>()
        /** 遍历项目类型字典 */
        for (explainProject in dictDefFuncIdList) {
            /** 输入类型 */
            mapEx[explainProject.code!!] = explainProject.dataType
            mapCode[explainProject.code!!] = explainProject.category
        }

        val floor = Floor()
        floor.projectId = projectId
        for (key in mapObj.keys) {
            val containsKey = mapEx.containsKey(key)
            if (containsKey){
                if (key != "id"&& key!="name"&&key!="localId"&&key!="localName"&&key!="defaultQRCode"
                    &&key!="qRCodePic"&&key!="bimIdPre"&&key!="bimId"&&key!="modelId"&&key!="bimLocation"
                    &&key!="outline"&&key!="floorSequenceId"&&key!="permanentPeopleNum"&&key!="floorIdentity"
                ){
                    val containsKey1 = mapCode.containsKey(key)
                    if (containsKey1){
                        val any = mapCode[key].toString()
                        if (any == "STATIC"){
                            when(mapEx[key].toString()) {
                                "INTEGER" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toInt()
                                }
                                "DOUBLE" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString().toDouble()
                                }
                                "BOOLEAN" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString().toBoolean()
                                }
                                "STRING" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                                "DATETIME" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ATTACHMENT" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                                "OBJECT" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ENUM" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                                "MENUM" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                                else -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                            }
                        }else{
                            if (floor.infos.isNullOrEmpty()){
                                floor.infos = HashMap<String,Any?>()
                            }
                            floor.infos!![key] = mapObj[key]!!.toString()
                        }
                    }

                } else {
                    when(key) {
                        "id" -> floor.id =  mapObj[key]!!.toString()
                        "name" -> floor.name =  mapObj[key]!!.toString()
                        "localId" -> floor.localId =  mapObj[key]!!.toString()
                        "localName" -> floor.localName =  mapObj[key]!!.toString()
                        "defaultQRCode" -> floor.defaultQRCode =  mapObj[key]!!.toString()
                        "qRCodePic" -> floor.qRCodePic =  mapObj[key]!!.toString()
                        "bimIpPre" -> floor.bimIdPre =  mapObj[key]!!.toString()
                        "bimId" -> floor.bimId =  mapObj[key]!!.toString()
                        "modelId" -> floor.modelId =  mapObj[key]!!.toString()
                        "bimLocation" -> floor.bimLocation =  mapObj[key]!!.toString()
                        "floorSequenceId" -> floor.floorSequenceId =  mapObj[key]!!.toString().toInt()
                        "permanentPeopleNum" -> floor.permanentPeopleNum =  mapObj[key]!!.toString().toInt()
                        "floorIdentity" -> floor.floorIdentity =  mapObj[key]!!.toString()
                        "outline" -> {
                            val replace1 = mapObj[key].toString().replace("X", "x")
                            val replace2 = replace1.replace("Y", "y")
                            val replace3 = replace2.replace("Z", "z")
//                                logger.debug("坐标= ${replace3}")
                            val type = object : TypeToken<ArrayList<PointPosition>>() {
                            }.getType()
                            var outline=  Gson().fromJson<ArrayList<PointPosition>>(replace3,type)
                            floor.outline = outline
                        }
                    }
                }

            } else {
                //                if (zoneSpace.infos.isNullOrEmpty()){
//                    zoneSpace.infos = HashMap<String,Any?>()
//                }
//                zoneSpace.infos!![key] =  mapObj[key]!!.toString()
            }
        }

        return floor
    }

    /**
     * 楼层工具
     *
     * @param projectId     项目 id
     * @param floor         楼层对象
     * @return 返回结果
     */
    fun floorTools(projectId: String,floor: Floor): ArrayList<HashMap<String,Any?>> {
        var list = ArrayList<HashMap<String,Any?>>()
        val hashMap = HashMap<String, Any?>()
        hashMap["id"] = floor.id
        hashMap["name"] = floor.name
        hashMap["projectId"] = projectId
        hashMap["objType"] = "floor"
        hashMap["classCode"] = "floor"
        hashMap["floorSequenceId"] = floor.floorSequenceId
        if (!floor.localName.isNullOrEmpty()){
            hashMap["localName"] = floor.localName
        }
        if (!floor.localId.isNullOrEmpty()){
            hashMap["localId"] = floor.localId
        }
        if (!floor.defaultQRCode.isNullOrEmpty()){
            hashMap["defaultQRCode"] = floor.defaultQRCode
        }
        if (!floor.qRCodePic.isNullOrEmpty()){
            hashMap["qRCodePic"] = floor.qRCodePic
        }
        if (!floor.bimIdPre.isNullOrEmpty()){
            hashMap["bimIdPre"] = floor.bimIdPre
        }
        if (!floor.modelId.isNullOrEmpty()){
            hashMap["modelId"] = floor.modelId
        }
        if (!floor.bimId.isNullOrEmpty()){
            hashMap["bimId"] = floor.bimId
        }
        if (!floor.bimLocation.isNullOrEmpty()){
            hashMap["bimLocation"] = floor.bimLocation
        }
        if (!floor.outline.isNullOrEmpty()){
            hashMap["outline"] = floor.outline.toString()
        }
        if (floor.permanentPeopleNum!=null&&floor.permanentPeopleNum!=0){
            hashMap["permanentPeopleNum"] = floor.permanentPeopleNum
        }
        if (!floor.floorIdentity.isNullOrEmpty()){
            hashMap["floorIdentity"] = floor.floorIdentity
        }
        if (!floor.infos.isNullOrEmpty()){
            for (key in floor.infos!!.keys){
                hashMap[key] = floor.infos!![key]
            }
        }

        list.add(hashMap)
        return list
    }


}