# InnerShadowCircleView.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.mingraviditet.ui.main.overview.widget

    import android.content.Context
    import android.graphics.BlurMaskFilter
    import android.graphics.Canvas
    import android.graphics.Paint
    import android.graphics.Path
    import android.util.AttributeSet
    import android.widget.FrameLayout
    import androidx.core.content.ContextCompat
    import dk.nsp.mingraviditet.R

    open class InnerShadowCircleView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    ) : FrameLayout(context, attrs, defStyleAttr) {

        private val defaultInnerShadowRadius = resources.getDimensionPixelOffset(R.dimen.base_inner_shadow_radius).toFloat()

        private var innerShadowRadius = defaultInnerShadowRadius

        val innerShadowContentOffset
            get() = innerShadowRadius * 1.2f

        init {
            // This forces android to draw the view
            setWillNotDraw(false)
        }

        //    private val innerShadowFilter = BlurMaskFilter(10f, BlurMaskFilter.Blur.OUTER)
        private val innerShadowFilter by lazy {
            BlurMaskFilter(
                resources.getDimensionPixelSize(R.dimen.base_inner_shadow_radius).toFloat(),
                BlurMaskFilter.Blur.OUTER
            )
        }

        private val innerShadowPaint by lazy {
            Paint().apply {
                color = ContextCompat.getColor(context, R.color.black)
                strokeWidth = 2f
                isAntiAlias = true
                style = Paint.Style.STROKE
                maskFilter = innerShadowFilter
            }
        }

        private val clippingPath by lazy {
            Path().also { p ->
                // Changes the clipping area to be the "outside" of the circle
                p.fillType = Path.FillType.EVEN_ODD
            }
        }

        private val mainDrawingPath by lazy {
            Path()
        }

        override fun onDraw(canvas: Canvas?) {
            // Setup the bounderies with a clipping path
            canvas?.clipPath(updatedMainPath(clippingPath))

            // Add the stroke with shadow (The outside is clipped by the previous call to .clipPath with the clippingPath)
            canvas?.drawPath(updatedMainPath(mainDrawingPath), innerShadowPaint)

            super.onDraw(canvas)
        }

        private fun updatedMainPath(p: Path): Path {
            p.reset()
            p.addCircle(
                viewCenterX,
                viewCenterY,
                viewRadius,
                Path.Direction.CW
            )
            return p
        }

        private val viewRadius
            get() = width / 2f

        private val viewCenterX
            get() = width / 2f

        private val viewCenterY
            get() = height / 2f
    }