package com.ys.bdtp.cad.items

import android.graphics.*
import com.ys.bdtp.cad.Opt
import com.ys.bdtp.cad.types.Space
import com.ys.bdtp.graph.SGraphyItem

/**
 * 空间item
 *
 * @author 付国宝
 */
class SpaceItem(val data: Space, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    /** 画笔 */
    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
    val outPaint = Paint()
    var mPoints = ArrayList<PointF>()

    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) {
                        mPoints.add(PointF(data.outLine!![0][0].x, data.outLine!![0][0].y))
                        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
                        }
                        mPoints.add(PointF(p.x, p.y))
                        path.lineTo(p.x, -p.y)
                    }
                    path.close()
                    if (spacePath == null) {
                        spacePath = path;
                    } else {
                        // spacePath.b
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        outPaint.style = Paint.Style.STROKE
        outPaint.color = Color.rgb(0xff, 0xff, 0xff)
        outPaint.strokeWidth = 20f
    } // Constructor

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

    /**
     * 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)


//            canvas.drawPath(spacePath!!, outPaint)
        }
    } // Function onDraw()

} // Class SpaceItem