package com.ys.bdtp.cad.items


import android.graphics.*
import android.util.Log
import android.view.MotionEvent
import com.ys.bdtp.cad.types.Point
import com.ys.bdtp.graph.SGraphyItem
import com.ys.bdtp.graph.SMotionEvent
import com.ys.bdtp.graph.enums.SGraphyItemFlag
import java.util.*

/**
 * Created by lihao.
 * Date: 2021/6/4
 */
class PointItem(val data: Point, parent: SGraphyItem? = null, defaultScale: Float? = null) :
    SGraphyItem(parent) {
    private lateinit var actionMove: ActionMove

    /** 新建 画笔 */
    val mPaint_inner = Paint()
    val mPaint_center = Paint()
    val mPaint_outer = Paint()

    /** 按时 画笔 */
    val not_overTime_inner = Paint()
    val not_overTime_outer = Paint()

    /** 逾期 画笔 */
    val overTime_inner = Paint()
    val overTime_outer = Paint()

    /** 承接查验复核中 */
    val checkReviewing_inner = Paint()
    val checkReviewing_outer = Paint()
    var mDefaultScale = 0f

    //var defaultScale = 1.0f
    //var mScale = 1f
    val outPaint = Paint()

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

    /** X坐标最小值  */
    private var minX = 0f

    /** X坐标最大值  */
    private var maxX = 0f

    /** Y坐标最小值  */
    private var minY = 0f

    /** Y坐标最大值  */
    private var maxY = 0f

    init {
        if (data.points.size != 1 && data.points.size != 0) {
            flags = EnumSet.of(
                SGraphyItemFlag.ItemIsMovable,
                SGraphyItemFlag.ItemIsSelectable,
                SGraphyItemFlag.ItemIsFocusable
            )
        }
        if (defaultScale != null) {
            mDefaultScale = defaultScale
        }
        /** 按时 */
        /** 按时 */
        not_overTime_inner.color = Color.parseColor("#ffffff")
        not_overTime_inner.isAntiAlias = true
        not_overTime_inner.style = Paint.Style.FILL

        not_overTime_outer.color = Color.parseColor("#04df97")//绿色
        not_overTime_outer.isAntiAlias = true
        not_overTime_outer.style = Paint.Style.FILL

        /** 逾期 */

        /** 逾期 */
        overTime_inner.color = Color.parseColor("#ffffff")
        overTime_inner.isAntiAlias = true
        overTime_inner.style = Paint.Style.FILL

        overTime_outer.color = Color.parseColor("#ff3b33")//红色
        overTime_outer.isAntiAlias = true
        overTime_outer.style = Paint.Style.FILL

        /** 承接查验复核中 */

        /** 承接查验复核中 */
        checkReviewing_inner.color = Color.parseColor("#ffffff")
        checkReviewing_inner.isAntiAlias = true
        checkReviewing_inner.style = Paint.Style.FILL

        checkReviewing_outer.color = Color.parseColor("#F7B500")//黄色
        checkReviewing_outer.isAntiAlias = true
        checkReviewing_outer.style = Paint.Style.FILL

        /** 新建 */

        /** 新建 */
        //中圈
        mPaint_center.color = Color.parseColor("#ffffff")
        mPaint_center.isAntiAlias = true
        mPaint_center.style = Paint.Style.FILL
        //内圈
        mPaint_inner.color = Color.parseColor("#0c90eb")
        mPaint_inner.isAntiAlias = true
        mPaint_inner.style = Paint.Style.FILL
        //外圈
        mPaint_outer.color = Color.parseColor("#0CC2EB")
        mPaint_outer.isAntiAlias = true
        mPaint_outer.style = Paint.Style.FILL


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

        try {
            zOrder = 1f

            minX = data.points!![0].x
            maxX = data.points!![0].x
            minY = data.points!![0].y
            maxY = data.points!![0].y
            val path = Path()
            path.moveTo(data.points!![0].x, -data.points!![0].y)
            for (line in data.points!!) {

                path.lineTo(line.x, -line.y)

                if (pipePath == null) {
                    pipePath = path;
                } else {
                    // spacePath.b
                }
            }

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

    override fun boundingRect(): RectF {
        return RectF(data.mX - 500f, data.mY - 500f, data.mX + 500f, data.mY + 500f)
    }

    override fun onSingleTapUp(e: SMotionEvent): Boolean {
        Log.e("TAG", "x=" + e.viewX.toString() + "，y=" + e.viewY.toString())
        return super.onSingleTapUp(e)
    }

    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */
    override fun onDraw(canvas: Canvas) {
        data.scalePoint = mDefaultScale / scene!!.view!!.scale
        if (data.points.size == 1 || data.points.size == 0) {
            canvas.drawCircle(data.mX, data.mY, 500f, mPaint_outer)
            canvas.drawCircle(data.mX, data.mY, 200f, mPaint_center)
            canvas.drawCircle(data.mX, data.mY, 100f, mPaint_inner)
        } else {
            canvas.drawPath(pipePath!!, outPaint)
            val size = data.points!!.size - 1
            val get = data.points!!.get(size)
            val get1 = data.points!!.get(0)
            canvas.drawCircle(get1.x, -get1.y, 60f, mPaint_inner)
            canvas.drawCircle(get.x, -get.y, 60f, mPaint_inner)
        }

        super.onDraw(canvas)
    } // Function onDraw()

    override fun onActionMove(event: MotionEvent) {
        super.onActionMove(event)
    }

    interface ActionMove {
        fun onActionMove(event: MotionEvent)
    }

    fun setOnActionMove(actionMove: ActionMove) {
        this.actionMove = actionMove
    }

}