package com.persagy.revit.services.modelCheck

import com.persagy.database.SFilter
import com.persagy.database.enums.SDirection
import com.persagy.revit.models.entities.Column
import com.persagy.revit.models.entities.VirtualWall
import com.persagy.revit.models.entities.Wall
import com.persagy.revit.models.entities.WallColumnLog
import com.persagy.revit.models.log.SyncLogEntity
import com.persagy.revit.models.modelFile.ModelFile
import com.persagy.revit.services.BaseDataService
import org.springframework.stereotype.Service

@Service
class ModelLogService {

    /**
     * 获取当前模型的前一个合法模型
     */
    fun getPrevValidModel(currentModel : ModelFile) : String?{
        if(currentModel.floorModelId == null)
            return null
        val models = BaseDataService.modelFileService.select(
            SFilter.and(SFilter.eq("floorModelId", currentModel.floorModelId!!),
                SFilter.eq("removed", false), SFilter.gt("version", 0))).order("version", SDirection.DESC).exec()
        var found = false
        for (model in models){
            if(found){
                return model.id
            }
            if(currentModel.id.equals(model.id)){
                found = true
            }
        }
        return null
    }

    /**
     * 将(虚拟)墙,柱子增删日志转换为SyncLogEntity类型
     */
    fun convertToSyncLogEntityList(modelId : String, arr : ArrayList<WallColumnLog>): ArrayList<SyncLogEntity> {
        val arrayList = ArrayList<SyncLogEntity>()
        arr.forEach {
            val syncLogEntity = SyncLogEntity()
            syncLogEntity.modelId = modelId
            syncLogEntity.objId = it.sourceId
            syncLogEntity.operation = it.operation
            syncLogEntity.type = it.elementType
            arrayList.add(syncLogEntity)
        }
        return arrayList
    }

    /**
     * 获取当前模型的墙的增删日志
     */
    fun getWallAddDeleteLog(currentModelId : String, prevModelId : String?) : ArrayList<Wall>{
        val current = BaseDataService.wallService.select(SFilter.eq("modelId", currentModelId)).exec()
        if(prevModelId == null)
        {
            current.forEach{
                // 表示添加
                it.operation = 1
            }
            return current
        }
        val prev = BaseDataService.wallService.select(SFilter.eq("modelId", prevModelId)).exec()
        getAddDelete(prev as ArrayList<WallColumnLog>, current as ArrayList<WallColumnLog>)
        return current
    }

    /**
     * 获取增删结果
     */
    private fun getAddDelete(prev: ArrayList<WallColumnLog>, current: ArrayList<WallColumnLog>) {
        val prevRevitIdSet = HashSet<String>()
        prev.forEach {
            if (it.sourceId != null)
                prevRevitIdSet.add(it.sourceId!!)
        }
        var iterator = current.iterator()
        while (iterator.hasNext()) {
            val it = iterator.next()
            if (prevRevitIdSet.contains(it.sourceId)) {
                // revit id 跟上一个模型有一样的对象
                prevRevitIdSet.remove(it.sourceId)
                iterator.remove()
            } else {
                it.operation = 1
            }
        }
        prev.forEach {
            if (prevRevitIdSet.contains(it.sourceId)) {
                it.operation = 0
                current.add(it)
            }
        }
    }

    /**
     * 获取当前模型的虚拟墙的增删日志
     */
    fun getVirtualWallAddDeleteLog(currentModelId : String, prevModelId : String?) : ArrayList<VirtualWall>{
        val current = BaseDataService.virtualWallService.select(SFilter.eq("modelId", currentModelId)).exec()
        if(prevModelId == null)
        {
            current.forEach{
                // 表示添加
                it.operation = 1
            }
            return current
        }
        val prev = BaseDataService.virtualWallService.select(SFilter.eq("modelId", prevModelId)).exec()
        getAddDelete(prev as ArrayList<WallColumnLog>, current as ArrayList<WallColumnLog>)
        return current
    }

    /**
     * 获取当前模型的柱子的增删日志
     */
    fun getColumnAddDeleteLog(currentModelId : String, prevModelId : String?) : ArrayList<Column>{
        val current = BaseDataService.columnService.select(SFilter.eq("modelId", currentModelId)).exec()
        if(prevModelId == null)
        {
            current.forEach{
                // 表示添加
                it.operation = 1
            }
            return current
        }
        val prev = BaseDataService.columnService.select(SFilter.eq("modelId", prevModelId)).exec()
        getAddDelete(prev as ArrayList<WallColumnLog>, current as ArrayList<WallColumnLog>)
        return current
    }

}