package com.ys.bdtp.cad.items

import android.graphics.*
import com.ys.bdtp.cad.Opt
import com.ys.bdtp.cad.types.Door
import com.ys.bdtp.graph.SGraphyItem
import com.ys.bdtp.graph.enums.SGraphyItemFlag
import java.util.*
import kotlin.math.sqrt


class DoorItem(private val data: Door, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    /** 画笔 */
    private val paint1 = Paint()
    private val paint2 = Paint()

    /** 门长度 */
    private var d = 0f

    /** 角度  */
    private var angle = 0f

    /** 旋转点 */
    private var p = PointF(0f, 0f)

    /** 旋转起始角度 */
    private var startAng = 0f

    /**
     * 构造
     */
    init {
        flags = EnumSet.of(SGraphyItemFlag.ItemIsSelectable, SGraphyItemFlag.ItemIsFocusable)
        try {
            paint1.style = Paint.Style.STROKE
            paint1.color = Opt.doorColor
            paint1.strokeWidth = 100f
            paint2.style = Paint.Style.STROKE
            paint2.color = Opt.doorColor
            paint2.strokeWidth = 50f
            paint2.pathEffect = DashPathEffect(floatArrayOf(200f, 200f), 0f)

            zOrder = 2f
            if (data.outLine != null && data.outLine!!.size > 0 && data.outLine!![0].size >= 2) {
                // 取门的两个端点
                val p1 = PointF(data.outLine!![0][0].x, -data.outLine!![0][0].y)
                val p2 = PointF(data.outLine!![0][1].x, -data.outLine!![0][1].y)
                d = sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y))

                // 角度为
//                val fo = atan2(-data.faceDirection!!.y, data.faceDirection!!.x)
//                // 门朝向角度
//                angle =
//                        if (data.faceDirection!!.x > 0) {
//                            fo
//                        } else {
//                            fo + Math.PI.toFloat()
//                        }

////                val dir = data.faceDirection!!.x * (data.handDirection!!.y - data.faceDirection!!.y) - data.faceDirection!!.y * (data.handDirection!!.x - data.faceDirection!!.x)
////                if (dir < 0) {
////                    startAng = -90f
////                }

                // 计算旋转点
//                if (abs(data.handDirection!!.x) > abs(data.handDirection!!.y)) {
//                    p = if (data.handDirection!!.x > 0) {
//                        if (p1.x > p2.x) {
//                            p1
//                        } else {
//                            p2
//                        }
//                    } else {
//                        if (p1.x < p2.x) {
//                            p1
//                        } else {
//                            p2
//                        }
//                    }
//                } else {
//                    p = if (data.handDirection!!.y > 0) {
//                        if (p1.y > p2.y) {
//                            p1
//                        } else {
//                            p2
//                        }
//                    } else {
//                        if (p1.y < p2.y) {
//                            p1
//                        } else {
//                            p2
//                        }
//                    }
//                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } // Constructor

    override fun boundingRect(): RectF {
        return RectF(data.outLine!![0][0].x, data.outLine!![0][0].y, data.outLine!![0][1].x, data.outLine!![0][1].y)
    }

    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */
    override fun onDraw(canvas: Canvas) {
//        canvas.translate(p.x, p.y);
//        canvas.rotate(angle * 180f / Math.PI.toFloat())
//
//        canvas.drawLine(0f, 0f, d, 0f, paint1)
        paint1.color = if (this.focus) {
            Color.RED
        } else {
            Opt.doorColor
        }
        canvas.drawLine(data.outLine!![0][0].x, -data.outLine!![0][0].y, data.outLine!![0][1].x, -data.outLine!![0][1].y, paint1)
//        canvas.drawArc(-d, -d, d, d, startAng, 90f, false, paint2)
        super.onDraw(canvas)
    } // Function onDraw()
} // Class DoorItem