package cn.sagacloud.android.cadengine.items

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.Typeface
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import cn.sagacloud.android.cadengine.Opt
import cn.sagacloud.android.cadengine.types.Space
import com.sybotan.android.graphy.SGraphyItem


/**
 * 空间名称item
 *
 * @author 付国宝re
 */
class SpaceNameItem(val data: Space, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    /** 画笔 */
    private val paintText = TextPaint()

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

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

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

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


    init {
        try {
            zOrder = 1f
            if (data.outLine != null && data.outLine!!.size > 0 && data.outLine!![0].size > 0) {
                minX = 9999999f
                maxX = -9999999f
                minY = 9999999f
                maxY = -9999999f

                for (line in data.outLine!!) {
                    if (line.size < 1) {
                        continue
                    }
                    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
                        }
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        paintText.style = Paint.Style.FILL
        paintText.flags = Paint.ANTI_ALIAS_FLAG
        paintText.color = Opt.textColor
        paintText.textSize = 700f
        val font: Typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)
        paintText.setTypeface(font)
//        paintText.textSkewX - 0.5f

//        textX = (minX + ((maxX - minX) / 2)) /*- 250*/
//        textY = (minY + ((maxY - minY) / 2)) - paintText.measureText(data.name!!) / 2
//
//        if (data.name!!.contains("走廊2")) {
//            textX -= 400f
//        } else if (data.name!!.contains("走廊1")) {
//            textY -= 5000f
//        } else if (data.name!!.contains("财务室旁开间办公区")) {
//            textY -= 2000f
//        }
        textX = (minX + ((maxX - minX) / 2)) - paintText.measureText(data.name!!) / 2 /*- 250*/
        textY = (minY + ((maxY - minY) / 2)) + 125f
    } // Constructor


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

    /**
     * 判断点是否在区域内
     *
     * @param x
     * @param y
     */
    override fun contains(x: Float, y: Float): Boolean {
        return false
    } // Function contains()

    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */
    override fun onDraw(canvas: Canvas) {
        canvas.save()
        canvas.rotate(-30f, textX, textY)
//        if (data.name!!.equals("张联庆旁办公室")) {
//            val layout = StaticLayout(data.name!!, paintText, 3500, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, true)
//            canvas.translate(textY + 1000, -textX - 1000)
//            layout.draw(canvas)
//        } else if (data.name!!.equals("会议室11") || data.name!!.equals("会议室12")) {
//            val layout = StaticLayout(data.name!!, paintText, 2300, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, true)
//            canvas.translate(textY + 500, -textX - 1000)
//            layout.draw(canvas)
//        } else {
//            canvas.drawText(data.name!!, textY, -textX, paintText)
//        }
//        canvas.rotate(-90f)
//        canvas.restore()
        var x = textX
        var y = textY * 0.95

        canvas.rotate(30f)
        canvas.restore()
        super.onDraw(canvas)
    } // Function onDraw()
} // Class SpaceItem