package cn.sagacloud.android.cadengine.items

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

/**
 * 柱子 Item
 *
 * @author 付国宝
 */
class ColumnItem(private val data: Column, 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 {
        paint.style = Paint.Style.FILL
        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 (out in data.outLine!!) {
                    if (out.size < 1) {
                        continue
                    }

                    val path = Path()
                    path.moveTo(out[0].x, -out[0].y)
                    for (p in out) {
                        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.style = Paint.Style.FILL
        paint.color = Opt.columnColor;
        for (path in pathList) {
            canvas.drawPath(path, paint);
        }
    } // Function onDraw()
} // Class ColumnItem