package com.ys.bdtp.cad.items

import android.annotation.SuppressLint
import android.graphics.*
import android.text.TextPaint
import com.ys.bdtp.cad.Opt
import com.ys.bdtp.cad.types.Grid
import com.ys.bdtp.graph.SGraphyItem
import com.ys.bdtp.graph.SGraphyView


/**
 * 墙item
 *
 * @author  郝建龙
 */
class GridItem(private val data: Grid, parent: SGraphyItem? = null, view: SGraphyView) : SGraphyItem(parent) {
    /** 画笔 */
    private val paint = Paint()

    /** 画笔 */
    private val paintText = TextPaint()

    /** 墙轮廓 */
    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
    lateinit var view: SGraphyView

    /**
     * 构造
     */
    init {
        try {
            this.view = view
            val path = Path()
            path.moveTo(data.location!!.points!![0].x, -data.location!!.points!![0].y)
            path.lineTo(data.location!!.points!![1].x, -data.location!!.points!![1].y)
            pathList.add(path)

            paintText.style = Paint.Style.FILL
            paintText.flags = Paint.ANTI_ALIAS_FLAG
            paintText.color = Opt.textColor
            paintText.textSize = 1500f
            val font: Typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)
            paintText.setTypeface(font)

//            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 (line in data.outLine!!) {
//                    if (line.size < 1) {
//                        continue
//                    }
//
//                    val path = Path()
//                    path.moveTo(line[0].x, -line[0].y)
//                    for (p in line) {
//                        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()
        }
    } // Constructor

    /**
     * Item对象边界区域
     *
     * @return  边界区域
     */
    override fun boundingRect(): RectF {
        return RectF(minX, minY, maxX, maxY)
    } // Function boundingRect()

    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */
    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        paint.color = Opt.engineRoomSpaceColor
        paint.style = Paint.Style.STROKE
        paint.strokeJoin = Paint.Join.ROUND
        paint.isAntiAlias = true
        paint.pathEffect = DashPathEffect(floatArrayOf(800f, 600f), 0f)
        for (path in pathList) {
            canvas.drawPath(path, paint)
        }


        var k = (data.location!!.points!![0].y - data.location!!.points!![1].y) / (data.location!!.points!![0].x - data.location!!.points!![1].x)
        if (data.location!!.points!![0].x - data.location!!.points!![1].x == 0f) {
            k = 1f
        }
        val b = data.location!!.points!![0].y - k * data.location!!.points!![0].x
        val mapFromScene = view.mapToScene(0f, 0f)
        var y = 0f
        var x = 0f
        if (k >= 1 || k <= -1) {
            y = mapFromScene.y + 2000
            if (mapFromScene.y < -data.location!!.points!![0].y) {
                y = -data.location!!.points!![0].y
            }
            x = data.location!!.points!![0].x
        } else {
            x = mapFromScene.x
            if (mapFromScene.x < data.location!!.points!![0].x) {
                x = data.location!!.points!![0].x
            }
            y = -(k * mapFromScene.x + b)
        }
        canvas.drawText(data.name!!, x, y, paintText)
    } // Function onDraw()
} // Class WallItem