package cn.sagacloud.android.cadengine.items

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


/**
 * 墙item
 *
 * @author 付国宝
 */
class VirtualWallItem(private val data: VirtualWall, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    /** 画笔 */
    private val paint = Paint()
    /** 虚拟墙路径 */
    private val pathList = ArrayList<Path>()

    /** X坐标最小值  */
    private var minX = 0f
    /** X坐标最大值  */
    private var maxX = 0f
    /** Y坐标最小值  */
    private var minY = 0f
    /** Y坐标最大值  */
    private var maxY = 0f

    /**
     * 构造
     */
    init {
        try {
            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()
                    pathList.add(path)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } // Init

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

    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */
    override fun onDraw(canvas : Canvas) {
        paint.color = Opt.virtualWallColor
        paint.pathEffect = DashPathEffect(floatArrayOf(200f, 200f), 0f)
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 100f
        for (path in pathList) {
            canvas.drawPath(path, paint);
        }

    } // Function onDraw()
} // Class VirtualWallItem