package cn.sagacloud.android.cadengine.items

import android.graphics.*
import android.text.TextPaint
import cn.sagacloud.android.cadengine.Opt
import cn.sagacloud.android.cadengine.types.TunableSpace
import com.sybotan.android.graphy.SGraphyItem


/**
 * 可调节item
 *
 */
class TunableSpaceItem(val data: TunableSpace, parent: SGraphyItem? = null) : SGraphyItem(parent) {
    /** 画笔 */
    val paint = Paint()
    val centerPaint = Paint()
    val outPaint = Paint()
    private val paintText = TextPaint()

    /** 墙轮廓 */
    private var spacePath: Path? = null
    /** 空间轮廓线坐标list  */
    //private val pointArr = ArrayList<ArrayList<PointF>>()
    /** X坐标最小值  */
    private var minX = 0f

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

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

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

    var mPoints = ArrayList<PointF>()

    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) {
                        mPoints.add(PointF(data.outLine!![0][0].x, data.outLine!![0][0].y))
                        continue
                    }
                    var 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
                        }
                        mPoints.add(PointF(p.x, p.y))
                        path.lineTo(p.x, -p.y)
                    }
                    path.close()
                    if (spacePath == null) {
                        spacePath = path
                    } else {
                        // spacePath.b
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        paintText.style = Paint.Style.FILL
        paintText.flags = Paint.ANTI_ALIAS_FLAG
        paintText.color = Opt.textColor
        paintText.textSize = 800f

        paint.style = Paint.Style.FILL
        paint.flags = Paint.ANTI_ALIAS_FLAG
        paint.isAntiAlias = true
        paint.color = Color.parseColor("#007CDB")
        //paint.color = Opt.spaceColor

        outPaint.style = Paint.Style.STROKE
        outPaint.color = Color.parseColor("#7E96FF")
        outPaint.setAntiAlias(true)
        outPaint.strokeWidth = 50f

        centerPaint.style = Paint.Style.STROKE
        centerPaint.color = Color.parseColor("#7E96FF")
        centerPaint.setAntiAlias(true)
        centerPaint.strokeWidth = 200f

    } // Constructor

    /**
     * 计算任意多边形的面积
     * @param latLngLines 经纬度坐标点
     * @return
     */
    fun calculateArea(): Float {
        val pointFList: MutableList<DoubleArray> = ArrayList()
        for (i in mPoints.indices) {
            val latLng: PointF = mPoints[i]
            //经纬度转换成平面直角坐标系
            wGS2flat(latLng.x.toDouble(), latLng.y.toDouble())?.let { pointFList.add(it) }
        }
        var iCycle: Int
        val iCount: Int
        iCycle = 0
        var iArea = 0.0
        iCount = pointFList.size
        iCycle = 0
        while (iCycle < iCount) {
            iArea = iArea + (pointFList[iCycle][0] * pointFList[(iCycle + 1) % iCount][1] - pointFList[(iCycle + 1) % iCount][0] * pointFList[iCycle][1])
            iCycle++
        }
        return Math.abs(0.5 * iArea).toFloat()
    }

    // 地球长半轴
    val EARTH_RADIUS = 6378.137

    /**
     * 经纬度转换成以米为单位的平面直角坐标
     *
     * @param lon 经度
     * @param lat 纬度
     * @return 平面直角坐标double型数组，以米为单位
     */
    fun wGS2flat(lon: Double, lat: Double): DoubleArray? {
        val L = CalcRad(lon)
        val l = L - CalcRad(120.0)
        val B = CalcRad(lat)
        val cosb = Math.cos(B)
        val sinb = Math.sin(B)
        val a = EARTH_RADIUS * 1000
        // 地球短半轴
        val b = 6356752.3142451793
        val t = Math.tan(B)
        // double r = 3600 * 180 / Math.PI;
        val e2 = (Math.pow(a, 2.0) - Math.pow(b, 2.0)) / Math.pow(a, 2.0)
        val e12 = (Math.pow(a, 2.0) - Math.pow(b, 2.0)) / Math.pow(b, 2.0)
        val n2 = e12 * Math.pow(cosb, 2.0)
        val N = a / Math.sqrt(1 - e2 * Math.pow(sinb, 2.0))
        val x = 6367449.1458 * B - 32009.8185 * cosb * sinb - (133.9975
                * cosb * Math.pow(sinb, 3.0)) - 0.6975 * cosb * Math.pow(sinb, 5.0)
        val X = x + N / 2 * t * Math.pow(cosb, 2.0) * Math.pow(l, 2.0) + (N / 24 * t * Math.pow(cosb, 4.0)
                * (5 - Math.pow(t, 2.0) + 9 * n2 + 4 * Math.pow(n2, 2.0))
                * Math.pow(l, 4.0))
        val Y = N * cosb * l + (N / 6 * Math.pow(cosb, 3.0)
                * (1 - Math.pow(t, 2.0) + n2) * Math.pow(l, 3.0))
        return doubleArrayOf(X, Y)
    }

    /**
     * 计算弧度
     *
     * @param d 以度为单位的经纬度数值
     * @return 以弧度为单位的经纬度数值
     */
    fun CalcRad(d: Double): Double {
        return d * Math.PI / 180.0
    }

    /**
     * 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) {
            if (paint.color == Opt.spaceChoseColor) {
                paint.color = Opt.spaceChoseColor
            } else {
                if (data.jobStatus.equals("01") || data.jobStatus.equals("04")) {
                    paint.color = Color.parseColor("#FFFF78")
                } else if (data.jobStatus.equals("02")) {
                    paint.color = Color.parseColor("#FF6B6B")
                } else if (data.jobStatus.equals("03") || data.jobStatus.equals("05")) {
                    paint.color = Color.parseColor("#99FF99")
                }
            }
            outPaint.color = Color.parseColor("#7E96FF")
            canvas.drawPath(spacePath!!, outPaint)
            canvas.drawPath(spacePath!!, paint)
        }
        super.onDraw(canvas)
    } // Function onDraw()
} // Class SpaceItem