package cn.sagacloud.android.cadengine.items

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

/**
 * 空间item
 *
 * @author 付国宝
 */
class SpaceItem(private val data: Space, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    /** 画笔 */
    private val paint = Paint()
    /** 墙轮廓 */
    private var spacePath: Path? = null
    /** X坐标最小值  */
    private var minX = 0f
    /** X坐标最大值  */
    private var maxX = 0f
    /** Y坐标最小值  */
    private var minY = 0f
    /** Y坐标最大值  */
    private var maxY = 0f

    private var a=(1+Math.random()*(10-1+1))

    init {
        try {
            zOrder = 1f
            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()
                    if (spacePath == null) {
                        spacePath = path;
                    } else { // spacePath.b
                         }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        setColor(paint, a.toInt())
        paint.style = Paint.Style.FILL
    } // Constructor

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

    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */
    override fun onDraw(canvas : Canvas) {
        if (null != spacePath) {
            canvas.drawPath(spacePath!!, paint)

        }
    } // Function onDraw()

    fun setColor(pen:Paint,a:Int){
        when(a){
            1 ->{
                pen.color =  Color.argb(20,139,0,139)
            }
            2 ->{
                pen.color = Color.argb(20,220,20,60)
            }
            3 ->{
                pen.color = Color.argb(20,255,20,147)
            }
            4 ->{
                pen.color = Color.argb(20,75,0,130)
            }
            5 ->{
                pen.color = Color.argb(20,123,104,238)
            }
            6 ->{
                pen.color = Color.argb(20,0,0,205)
            }
            7 ->{
                pen.color = Color.argb(20,119,136,153)
            }
            8 ->{
                pen.color = Color.argb(20,	30,144,255)
            }
            9 ->{
                pen.color = Color.argb(20,255,182,193)
            }
            10 ->{
                pen.color = Color.argb(20,123,104,238)
            }
        }
    } // Fun

} // Class SpaceItem