package com.abbidot.tracker.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseView


/**
 *Created  by .yzq on 2024/10/16/016.
 * @link
 * @description:
 */
class MapDeviceNetView : BaseView {

    /**
     * 网络类型，0没有网络 1蜂窝网 2wifi
     */
    private var mNetType = NO_NET_TYPE

    //确定圆的左上角的坐标轴原点位置
    private var mOffsetX = 0
    private var mOffsetY = 0
    private var mWhiteCircleRadius = 0f

    companion object {
        const val NO_NET_TYPE = 0
        const val DATA_NET_TYPE = 1
        const val WIFI_NET_TYPE = 2
    }

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

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

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


    override fun init(context: Context, attrs: AttributeSet?) {
        super.init(context, attrs)
        mWhiteCircleRadius = AppUtils.dpToPx(8f)

        mPaint.apply {
            strokeWidth = mStrokeWidth
            strokeCap = Paint.Cap.ROUND
            //开启抗锯齿
            flags = Paint.ANTI_ALIAS_FLAG
        }
    }


    override fun startDraw(canvas: Canvas) {
//        return
        if (mNetType == NO_NET_TYPE) return
        canvas.apply {
            mPaint.apply {
                color = ContextCompat.getColor(mContext, R.color.white)
                style = Paint.Style.FILL
            }
            val bigCy = mOffsetY.toFloat() + mWhiteCircleRadius
            drawCircle(
                mOffsetX.toFloat() + mWhiteCircleRadius, bigCy, mWhiteCircleRadius, mPaint
            )
            mPaint.apply {
                color = ContextCompat.getColor(mContext, R.color.select_color)
                style = Paint.Style.FILL
            }
            if (mNetType == DATA_NET_TYPE) {
                val barNUm = 4
                val twoSpace = (mWhiteCircleRadius * 2 - mStrokeWidth * 6) / 2
                val stopY = mOffsetY.toFloat() + mWhiteCircleRadius + AppUtils.dpToPx(3f)
                for (i in 0 until barNUm) {
                    val startX = mOffsetX.toFloat() + twoSpace + mStrokeWidth * i + mStrokeWidth * i
                    val startY = if (i == 0) mOffsetY.toFloat() + mWhiteCircleRadius + 5
                    else mOffsetY.toFloat() + mWhiteCircleRadius - 5 * i + 2
                    drawLine(startX, startY, startX, stopY, mPaint)
                }
            } else if (mNetType == WIFI_NET_TYPE) {
                val cx = mOffsetX.toFloat() + mWhiteCircleRadius
                val cy = mOffsetY.toFloat() + mWhiteCircleRadius + mWhiteCircleRadius / 2.3f
                drawCircle(cx, cy, mStrokeWidth, mPaint)
                mPaint.style = Paint.Style.STROKE
                val insideRadius = mWhiteCircleRadius / 2
                val startAngle = -130f
                val sweepAngle = 80f
                drawArc(
                    cx - insideRadius,
                    cy - insideRadius,
                    cx + insideRadius,
                    cy + insideRadius,
                    startAngle,
                    sweepAngle,
                    false,
                    mPaint
                )
                val outsideRadius = mWhiteCircleRadius - mStrokeWidth
                drawArc(
                    cx - outsideRadius,
                    cy - outsideRadius,
                    cx + outsideRadius,
                    cy + outsideRadius,
                    startAngle,
                    sweepAngle,
                    false,
                    mPaint
                )
            }
        }
    }

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

    fun setShowNetType(type: Int) {
        mNetType = if (type <= NO_NET_TYPE) NO_NET_TYPE
        else if (type >= WIFI_NET_TYPE) WIFI_NET_TYPE
        else type
        invalidate()
    }
}