package com.abbidot.tracker.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.qmuiteam.qmui.util.QMUIDisplayHelper

/**
 *Created  by .yzq on 2024/8/26/026.
 * @link
 * @description:
 */
class MapMarkerInfoView : View {

    private lateinit var mContext: Context
    private var mOffsetX = 0
    private var mOffsetY = 0

    //文字和边界的距离。三角形的直线长
    private var mPadding = 0

    private lateinit var mTextPaint: Paint
    private lateinit var mPaint: Paint

    //小三角形的path
    private lateinit var mTrianglePath: Path

    private var mTimeText = ""
    private var mAddressText = ""

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context)
    }

    private fun init(context: Context) {
        //禁用硬件加速,防止返回到界面多次执行onDraw
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        mContext = context
        mPaint = Paint().apply {
            color = ContextCompat.getColor(mContext, R.color.white)
        }
        //开启抗锯齿
        mTextPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            color = ContextCompat.getColor(mContext, R.color.black)
            textSize = QMUIDisplayHelper.sp2px(mContext, 12).toFloat()
        }
        mPadding = AppUtils.dpToPx(6)

        mTrianglePath = Path()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.apply {
            startDraw(this)
        }
    }

    private fun startDraw(canvas: Canvas) {
        if (TextUtils.isEmpty(mAddressText) && TextUtils.isEmpty(mTimeText)) return
        mTrianglePath.apply {
            reset()
            moveTo((mOffsetX - mPadding).toFloat(), (mOffsetY - mPadding).toFloat())
            lineTo(mOffsetX.toFloat(), mOffsetY.toFloat())
            lineTo((mOffsetX + mPadding).toFloat(), (mOffsetY - mPadding).toFloat())
            close()
        }
        canvas.apply {

            drawPath(mTrianglePath, mPaint)

            val fontMetrics = mTextPaint.fontMetrics
            //文字高度
            val textHeight = fontMetrics.bottom - fontMetrics.top
            //时间文字宽度
            val timeTextWith = mTextPaint.measureText(mTimeText)
            //地址文字宽度
            val addressTextWith = mTextPaint.measureText(mAddressText)
            val maxTextWith = maxOf(timeTextWith, addressTextWith)

            drawRoundRect(
                mOffsetX - maxTextWith / 2 - mPadding,
                mOffsetY - textHeight * 2 - mPadding * 2,
                mOffsetX + maxTextWith / 2 + mPadding,
                mOffsetY.toFloat() - mPadding,
                AppUtils.dpToPx(6f),
                AppUtils.dpToPx(6f),
                mPaint
            )
            drawText(
                mTimeText,
                mOffsetX - timeTextWith / 2,
                mOffsetY - textHeight / 2 - mPadding,
                mTextPaint
            )
            drawText(
                mAddressText,
                mOffsetX - addressTextWith / 2,
                mOffsetY - textHeight / 2 - textHeight - mPadding,
                mTextPaint
            )
        }
    }

    fun setShowText(time: String, address: String) {
        mTimeText = time
        mAddressText = address
        invalidate()
    }

    fun setOffsetXY(x: Int, y: Int) {
        mOffsetX = x
        mOffsetY = y
        invalidate()
    }
}