# MeasurementsLineChartYAxisRenderer.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.journal.sections.measurements.chart.renderers

    import android.graphics.Canvas
    import android.graphics.DashPathEffect
    import android.graphics.Paint
    import com.github.mikephil.charting.components.YAxis
    import com.github.mikephil.charting.renderer.YAxisRenderer
    import com.github.mikephil.charting.utils.Transformer
    import com.github.mikephil.charting.utils.ViewPortHandler
    import dk.nsp.mingraviditet.ui.main.journal.sections.measurements.chart.MeasurementsLineChart

    class MeasurementsLineChartYAxisRenderer(
        viewPortHandler: ViewPortHandler?,
        yAxis: YAxis?,
        trans: Transformer?,
        private val gridLineColor: Int
    ) : YAxisRenderer(viewPortHandler, yAxis, trans) {

        private val gridLineSolidPaint = Paint().apply {
            color = gridLineColor
            strokeWidth = 2f
        }
        private val gridLineDashedPaint = Paint(gridLineSolidPaint).apply {
            pathEffect = DashPathEffect(floatArrayOf(16f, 10f), 0f)
        }

        override fun drawYLabels(
            canvas: Canvas?,
            fixedPosition: Float,
            positions: FloatArray?,
            offset: Float
        ) {
            canvas ?: return

            // Positions contains X & Y coordinates, so we want to filter out all X coordinates (Since they are the same)
            val yPositions =
                positions?.filter { it != positions.firstOrNull() } ?: return super.drawYLabels(
                    canvas,
                    fixedPosition,
                    positions,
                    offset
                )

            yPositions.forEachIndexed { index, yPos ->
                // Split strings on "\n"
                val labelSegments = mYAxis.getFormattedLabel(index).split('\n')

                // Draw each label segments offsetting on the vertically taking textSize into account
                labelSegments.forEachIndexed { i, segment ->
                    canvas.drawText(
                        segment,
                        fixedPosition,
                        yPos + (mAxisLabelPaint.textSize * i),
                        mAxisLabelPaint
                    )
                }
            }
        }

        override fun renderGridLines(c: Canvas?) {
            c ?: return
            mViewPortHandler ?: return

            val lineXYPositions = transformedPositions
            val startX = lineXYPositions.firstOrNull() ?: return

            // Since [transformedPositions] return a list of x followed by a y coordinate, we filter out all x coordinates
            val lineYPositions =
                lineXYPositions.filter { it != lineXYPositions.first() }

            val endX = mViewPortHandler.contentRight()

            lineYPositions.forEachIndexed { i, yPos ->
                // Skip first line since is on top of xAxis
                if (i == 0) {
                    return@forEachIndexed
                }

                val paintToUse = if (MeasurementsLineChart.isYAxisPositionSolidLine(i)) {
                    gridLineSolidPaint
                } else {
                    gridLineDashedPaint
                }

                c.drawLine(
                    startX,
                    yPos,
                    endX,
                    yPos,
                    paintToUse
                )
            }
        }
    }