package com.ys.bdtp.graph.items

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import com.ys.bdtp.graph.SGraphyItem
import com.ys.bdtp.graph.enums.SGraphyItemFlag
import java.util.*

/**
 * SGraphy图形引擎矩形框Item
 *
 * @author  庞利祥(sybotan@126.com)
 */
open class SGraphyRectItem(rect: RectF, parent: SGraphyItem? = null) : SGraphyItem(parent){
    /**
     * 类对象
     */
    companion object {
        private val TAG = SGraphyRectItem::class.java.name
    } // companion object

    /** 矩形区域 */
    var rect: RectF = rect
        set(value) {
            field = value
            invalidate()
        }
    /** 画笔 */
    private var pen = Paint()

    init {
        pen.color = Color.RED
        flags = EnumSet.of(SGraphyItemFlag.ItemIsMovable, SGraphyItemFlag.ItemIsSelectable, SGraphyItemFlag.ItemIsFocusable)
        isVisible = true
        this.rect = rect

    } // init()

    /**
     * 构造函数
     *
     * @param   left        矩形左上角的x坐标
     * @param   top         矩形左上角的y坐标
     * @param   right       矩形右下角的x坐标
     * @param   bottom      矩形右下角的y坐标
     * @param   parent      指向父节点
     */
    constructor(left: Float, top: Float, right: Float, bottom: Float, parent: SGraphyItem? = null) : this(RectF(left, top, right, bottom), parent)

    /**
     * Item对象边界区域
     *
     * @return  边界区域
     */
    override fun boundingRect(): RectF {
        return rect
    } // Function boundingRect()

    /**
     * Item绘制操作
     *
     * @param   canvas      画布对象
     * @param   rect        更新区域
     */
    override fun onDraw(canvas: Canvas) {
        pen.color = if (this.isSelected) {
            Color.RED
        } else {
            Color.BLUE
        }
        canvas.drawRect(rect, pen)
        return
    } // Function paint()
} // Class SGraphyRectItem()