package cn.sagacloud.android.cadengine.items

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

/**
 * Created by lihao.
 * Date: 2021/6/7
 */
class PipeLineItem(val data: PipeLine, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    /** 画笔 */
    val paint = Paint()
    val mPaint_inner = Paint()

    /** 管道轮廓 */
    private var pipePath: Path? = null

    /** 管道轮廓线坐标list  */
    private val pointArr = ArrayList<ArrayList<PointF>>()

    val outPaint = Paint()

    var mPoints = ArrayList<PointF>()

    init {
        try {
            zOrder = 1f


            for (line in data.points!!) {
                if (line.size < 1) {
                    continue
                }

                val path = Path()
                path.moveTo(line[0].x, -line[0].y)
                for (p in line) {
                    path.lineTo(p.x, -p.y)
                }
                if (pipePath == null) {
                    pipePath = path;
                } else {
                    // spacePath.b
                }
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }

        outPaint.style = Paint.Style.STROKE
        outPaint.strokeJoin = Paint.Join.ROUND
        outPaint.isAntiAlias = true
        outPaint.color = Color.rgb(0x66, 0x66, 0xff)
        outPaint.strokeWidth = 150f

        //内圈
        mPaint_inner.color = Color.parseColor("#0c90eb")
        mPaint_inner.isAntiAlias = true
        mPaint_inner.style = Paint.Style.FILL
    } // Constructor


    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */
    override fun onDraw(canvas: Canvas) {
        if (null != pipePath) {
//            paint.style = Paint.Style.FILL

//            canvas.drawPath(pipePath!!, paint)


            canvas.drawPath(pipePath!!, outPaint)
            val size = data.points!!.size - 1
            val get = data.points!!.get(size).get(data.points!!.get(size).size - 1)
            canvas.drawCircle(data.points!!.get(0).get(0).x, -data.points!!.get(0).get(0).y, 160f, mPaint_inner)
            canvas.drawCircle(get.x, -get.y, 160f, mPaint_inner)
        }
    } // Function onDraw()
}