package com.ys.bdtp.cad.items


import android.content.Context
import android.graphics.*
import android.util.Log
import com.ys.bdtp.cad.types.Point
import com.ys.bdtp.cad.R
import com.ys.bdtp.graph.SGraphyItem
import com.ys.bdtp.graph.SMotionEvent
import com.ys.bdtp.graph.enums.SGraphyItemFlag
import java.util.*


/**
 * Created by lihao.
 * Date: 2021/6/4
 */
class EquipItem(
    var context: Context,
    val data: Point,
    parent: SGraphyItem? = null,
    defaultScale: Float? = null
) :
    SGraphyItem(parent) {
    /** 新建 画笔 */
    val mPaint_inner = Paint()
    val mPaint_center = Paint()
    var mDefaultScale = 0f
    var mRedBitmap: Bitmap
    var mPurpleBitmap: Bitmap
    var mBlueBitmap: Bitmap
    var mGreenBitmap: Bitmap
    var myellowBitmap: Bitmap
    var mRedBitmap2: Bitmap

    init {
        if (defaultScale != null) {
            mDefaultScale = defaultScale
        }
        //1-蓝 2-绿 3-红 4-黄

        mBlueBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.equip_blue)


        mGreenBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.equip_green)

        mRedBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.equip_red)
        mRedBitmap2 = BitmapFactory.decodeResource(context.resources, R.drawable.equip6)
        mPurpleBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.equip_notdone)
        myellowBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.yellow_equip)


        /** 新建 */
        //内圈
        mPaint_inner.color = Color.parseColor("#66272727")
        mPaint_inner.isAntiAlias = true
        mPaint_inner.style = Paint.Style.FILL
        //中圈
        mPaint_center.color = Color.parseColor("#ff00ff")
        mPaint_center.isAntiAlias = true
        mPaint_center.style = Paint.Style.FILL
        flags = EnumSet.of(
            SGraphyItemFlag.ItemIsMovable,
            SGraphyItemFlag.ItemIsSelectable,
            SGraphyItemFlag.ItemIsFocusable
        )
    }

    override fun onSingleTapUp(e: SMotionEvent): Boolean {
        Log.e("TAG", "x=" + e.viewX.toString() + "，y=" + e.viewY.toString())
        return super.onSingleTapUp(e)
    }

    /**
     * Item绘制操作
     *
     * @param   canvas          画布
     * @param   rect            绘制区域
     */

    override fun onDraw(canvas: Canvas) {
        data.scalePoint = mDefaultScale / scene!!.view!!.scale
        val matrix = Matrix()
        matrix.postScale(3.0000007f, 3.0000007f)
        matrix.postTranslate(data.mX - 400f, data.mY - 400f)
        //1-蓝 2-绿 3-红
        if (data.equipState == 1) {
            canvas.drawBitmap(mBlueBitmap, matrix, mPaint_center)
        } else if (data.equipState == 2) {
            canvas.drawBitmap(mGreenBitmap, matrix, mPaint_center)
        } else if (data.equipState == 3) {
            canvas.drawBitmap(mRedBitmap, matrix, mPaint_center)
        } else if (data.equipState == 4) {
            canvas.drawBitmap(myellowBitmap, matrix, mPaint_center)
        }  else if (data.equipState == 5) {
            canvas.drawBitmap(mPurpleBitmap, matrix, mPaint_center)
        }  else if (data.equipState == 6) {
            canvas.drawBitmap(mRedBitmap2, matrix, mPaint_center)
        } else {
            canvas.drawBitmap(mBlueBitmap, matrix, mPaint_center)
        }

//        canvas.drawCircle(data.mX, data.mY, 500f, mPaint_center)
//        canvas.drawCircle(data.mX, -data.mY, 1500f, mPaint_inner)
        super.onDraw(canvas)
    } // Function onDraw()

}