package cn.sagacloud.android.cadengine.items

import android.graphics.*
import cn.sagacloud.android.cadengine.Opt
import cn.sagacloud.android.cadengine.types.Space
import com.sybotan.android.graphy.SGraphyItem

/**
 * 空间item
 *
 * @author 付国宝
 */
class SpaceItem(private val data: Space, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    /** 画笔 */
    private val paint = Paint()
    /** 墙轮廓 */
    private var spacePath: Path? = null
    /** 空间轮廓线坐标list  */
    private val pointArr = ArrayList<ArrayList<PointF>>()
    /** X坐标最小值  */
    private var minX = 0f
    /** X坐标最大值  */
    private var maxX = 0f
    /** Y坐标最小值  */
    private var minY = 0f
    /** Y坐标最大值  */
    private var maxY = 0f

    init {
        try {
            zOrder = 1f
            if (data.outLine != null && data.outLine!!.size > 0 && data.outLine!![0].size > 0) {
                minX = data.outLine!![0][0].x
                maxX = data.outLine!![0][0].x
                minY = data.outLine!![0][0].y
                maxY = data.outLine!![0][0].y

                for (line in data.outLine!!) {
                    if (line.size < 1) {
                        continue
                    }

                    val path = Path()
                    path.moveTo(line[0].x, -line[0].y)
                    for (p in line) {
                        if (p.x < minX) {
                            minX = p.x
                        }
                        if (p.x > maxX) {
                            maxX = p.x
                        }
                        if (-p.y < minY) {
                            minY = -p.y
                        }
                        if (-p.y > maxY) {
                            maxY = -p.y
                        }
                        path.lineTo(p.x, -p.y)
                    }
                    path.close()
                    if (spacePath == null) {
                        spacePath = path;
                    } else {
                        // spacePath.b
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        //if (data.outLine!!.size > 0) {
//            val tempArr = this.data.OutLine
//            this.minX = tempArr[0][0].x
//            this.maxX = this.minX
//            this.minY = -(tempArr[0][0].y)
//            this.maxY = this.minY
            // 处理轮廓点数组,同时计算最大最小值
//            this.pointArr = tempArr.map(t => {
//                var tempArr = t.map(it => {
//                    var x = it.X,
//                    y = -it.Y
//                    if (x < this.minX) {
//                        this.minX = x
//                    }
//                    if (y < this.minY) {
//                        this.minY = y
//                    }
//                    if (x > this.maxX) {
//                        this.maxX = x
//                    }
//                    if (y > this.maxY) {
//                        this.maxY = y
//                    }
//                    return SPoint(x, y)
//                })
//                return tempArr
//            })
        //}
    } // Constructor

    /**
     * Item对象边界区域
     *
     * @return SRect
     */
    override fun boundingRect() : RectF {
        return RectF(minX, minY, maxX, maxY)
    } // Function boundingRect()

    /**
     * 判断点是否在区域内
     *
     * @param x
     * @param y
     */
    override fun contains(x: Float, y: Float): Boolean {
//        if (this.data?.OutLine!!.size > 0) {
//            var nCross: Double = 0.0
//            var point = ArrayList<String>()
//            point.add("x")
//            point.add("y")
//            var APoints = this.data?.OutLine!!.get(0)
//            var length = APoints.size
//            var p1: ArrayList<Point>? = null
//            var p2: ArrayList<Point>? = null
//            var i: Int? = null
//            var xinters: Double? = null
//            p1 = APoints.get(0)
//            i == 1
//            while (i!! <= length){
//                p2 = APoints.get((i % length))
//                if (point[0] > Math.min(p1.X, p2.X) && point[0] <= Math.max(p1.X, p2.X)) {
//                    if (point[1] <= Math.max(p1.Y, p2.Y)) {
//                        if (p1.X != p2.X) {
//                            //计算位置信息
//                            xinters = ((point[0] - p1.X) * (p2.Y - p1.Y)) / (p2.X - p1.X) + p1.Y
//                            if (p1.Y == p2.Y || point[1] <= xinters) {
//                                nCross++
//                            }
//                        }
//                    }
//                }
//                p1 = p2
//                i++
//            }
//            return nCross % 2 === 1
//        }
        return false
    } // Function contains()

    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */
    override fun onDraw(canvas : Canvas) {
        if (null != spacePath) {
            paint.style = Paint.Style.FILL
            paint.color = Opt.spaceColor;
            canvas.drawPath(spacePath!!, paint)

        }
    } // Function onDraw()

} // Class SpaceItem