/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2019.  斯伯坦机器人
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.sybotan.android.demo.graphy

import android.graphics.*
import android.util.Log
import com.sybotan.android.graphy.SGraphyItem

/**
 * 墙图元
 *
 * @author  庞利祥(sybotan@126.com)
 */
class GraphyWallItem(pointList: List<PointF>, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    companion object {
        private val TAG = GraphyWallItem::class.java.name
    } // companion object

    // 画笔
    private var pen = Paint()
    private var pointList: List<PointF> = ArrayList(0)
        set(value) {
            field = value
            if (field.size < 2) {
                bRect = RectF()
                return
            }
            bRect = RectF(field[0].x, field[0].y, field[0].x, field[0].y)
            for (i in 1 until field.size) {
                val p = field[i]
                if (bRect.left > p.x) {
                    bRect.left = p.x
                }
                if (bRect.right < p.x) {
                    bRect.right = p.x
                }
                if (bRect.top > p.y) {
                    bRect.top = p.y
                }
                if (bRect.bottom < p.y) {
                    bRect.bottom = p.y
                }
            }
            Log.d(TAG, "bRect=$bRect")
        }

    private var bRect = RectF()

    /**
     * 默认构造函数
     */
    init {
        pen.color = Color.RED
        pen.strokeWidth = 5f


        this.pointList = pointList
    } // init


    /**
     * Item对象边界区域
     */
    override fun boundingRect(): RectF {
        return RectF(bRect.left - 5, bRect.top - 5,
                bRect.right + 5, bRect.bottom + 5)
    } // Function boundingRect()

    /**
     * @param   canvas      画布对象
     * @param   rect        更新区域
     */
    override fun onDraw(canvas: Canvas, rect: RectF) {
        if (pointList.size < 2) {       // 如果少于2个点，则退出
            return
        }

        for (i in 1 until pointList.size) {
            val p1 = pointList[i - 1]
            val p2 = pointList[i]
            canvas.drawLine(p1.x, p1.y, p2.x, p2.y, pen)
        }

        val p1 = Path()
        val p2 = Path()
        
        return
    } // Function paint()
} // Class GraphyWallItem