package com.persagy.server.datacenter.services.relation_calc


import com.persagy.server.datacenter.models.entities.assistant.PointPosition
import com.persagy.server.datacenter.models.entities.graphtype.MPoint
import org.springframework.util.DigestUtils
import java.awt.geom.GeneralPath
import java.awt.geom.Point2D



/**
 *
 *
 * @author jxing
 */
object CommonService {


    /**
     * 获取MD5值
     */
    fun getMD5(btInput : ByteArray) : String {
        return DigestUtils.md5DigestAsHex(btInput)
    }

    /**
     * 获取文件服务器中的文件路径
     */
    fun getFilePathInFileService(uploadPath : String, modelId : String) : String{
        return "${uploadPath}${modelId}.rvt"
    }

    /**
     * 获取文件服务器中的key
     */
    fun getFileKeyInFileService(dir : String, modelId : String) : String{
        return "${dir}/${modelId}.rvt"
    }

    /**
     * 是否点在多边形内
     */
    fun isPointInPolygonCheckWithJdkGeneralPath(point: MPoint, polygon: ArrayList<PointPosition>): Boolean {
        if(polygon.isEmpty())
            return false
        val p = getGeneralPath(polygon)
        val newPoint = Point2D.Double()
        newPoint.x = point.x
        newPoint.y = point.y
        return p.contains(newPoint)
    }

    /**
     * 生成bimid前缀
     */
    fun generateBIMIDPrefix(projectId : String, folderId : String, floorName : String, hasColon : Boolean): String{
        if(hasColon){
            return "${projectId}${folderId}${floorName}:"
        }
        return "${projectId}${folderId}${floorName}"
    }


    /**
     * 根据点数组获取围成的GeneralPath对象
     */
    fun getGeneralPath(polygon : ArrayList<PointPosition>) : GeneralPath{
        val p = GeneralPath()
        val first = polygon[0]
        p.moveTo(first.x!!, first.y!!)
        for (index in polygon.indices) {
            if(index == 0)
                continue
            val tmpPoint = polygon[index]
            p.lineTo(tmpPoint.x!!, tmpPoint.y!!)
        }
        p.lineTo(first.x!!, first.y!!)
        p.closePath()
        return p
    }




    /**
     * 获取异常的堆栈信息
     */
    fun getStackTrace(e : Throwable): String? {
        return org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(e)
    }
}