package cn.sagacloud.android.cadengine.items


import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import cn.sagacloud.android.cadengine.R
import cn.sagacloud.android.cadengine.types.Point
import com.sybotan.android.graphy.SGraphyItem
import com.sybotan.android.graphy.SMotionEvent
import com.sybotan.android.graphy.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 mBitmap: Bitmap

    init {
        if (defaultScale != null) {
            mDefaultScale = defaultScale
        }
        //1-蓝 2-绿 3-红
        if (data.equipState == 1) {
            mBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.equip_blue)
        } else if (data.equipState == 2) {
            mBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.equip_green)
        } else if (data.equipState == 3) {
            mBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.equip_red)
        } else {
            mBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.equip_blue)
        }
        /** 新建 */
        //内圈
        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)
        canvas.drawBitmap(mBitmap, 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()

}