package com.adealink.weparty.commonui.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PointF
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.adealink.weparty.R

class CircleView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private var center = PointF(0f, 0f)
    private var radius = 0f
    private val drawRectF: RectF = RectF()
    private val paint = Paint()
    private val circleColor: Int

    init {
        val typedArray: TypedArray = context.obtainStyledAttributes(
            attrs,
            R.styleable.CircleView
        )
        circleColor = typedArray.getColor(R.styleable.CircleView_circleColor, Color.WHITE)
        typedArray.recycle()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        center = PointF(w.toFloat()/2, h.toFloat()/2)
        radius = ((width - paddingLeft - paddingRight) / 2).toFloat()
    }

    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        canvas.translate(center.x, center.y)
        drawRectF.set(-radius, -radius, radius, radius)
        paint.color = circleColor
        paint.style = Paint.Style.FILL
        paint.isAntiAlias = true
        canvas.drawArc(drawRectF, 0f, 360f, true, paint)
    }
}