# FetusProgressWidget.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.util.AttributeSet
    import android.view.Gravity
    import android.view.LayoutInflater
    import android.widget.ImageView
    import androidx.annotation.DrawableRes
    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.databinding.FetusProgressWidgetBinding
    import dk.nsp.mingraviditet.util.bindingadapters.setDrawableResSrc

    class FetusProgressWidget @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    ) : CustomCircularProgressBar(context, attrs, defStyleAttr) {

        private val binding =
            FetusProgressWidgetBinding.inflate(LayoutInflater.from(context), this, true)

        private val fetusLayoutParams by lazy {
            LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER
            )
        }

        private val targetMonths = 9.0
        private var monthsIntoPregnancy: Double? = null
            set(value) {
                field = value
                value?.let {
                    binding.progressBar.progressInPercentage = ((it / targetMonths) * 100).toInt()
                    updateFetusViews()
                }
            }
        private var numberOfFetuses: Int? = null
            set(value) {
                field = value
                updateFetusViews()
            }

        init {
            adjustFetusesContainer()
        }

        fun setup(monthsIntoPregnancy: Double?, numberOfFetuses: Int?) {
            this.monthsIntoPregnancy = monthsIntoPregnancy
            this.numberOfFetuses = numberOfFetuses
        }

        private fun adjustFetusesContainer() {
            (binding.fetusesContainer.layoutParams as? MarginLayoutParams)?.setMargins(
                binding.progressBar.contentOffset.toInt(),
                binding.progressBar.contentOffset.toInt(),
                binding.progressBar.contentOffset.toInt(),
                binding.progressBar.contentOffset.toInt()
            )
        }

        private fun updateFetusViews() {
            binding.fetusesContainer.removeAllViews()

            val fetuses = numberOfFetuses ?: return
            val fetusImageRes = currentCurrentImageRes ?: return

            for (fetus in (0..fetuses)) {
                val fetusImageView = ImageView(context).also {
                    it.setDrawableResSrc(fetusImageRes)
                    it.layoutParams = fetusLayoutParams
                }
                binding.fetusesContainer.addView(fetusImageView)
            }
        }

        private val currentCurrentImageRes: Int?
            @DrawableRes
            get() {
                return monthsIntoPregnancy?.let {
                    when (it) {
                        in (9.0..Double.MAX_VALUE) -> R.drawable.ic_9_0_months
                        in (8.5..9.0) -> R.drawable.ic_8_5_months
                        in (8.0..8.5) -> R.drawable.ic_8_0_months
                        in (7.5..8.0) -> R.drawable.ic_7_5_months
                        in (7.0..7.5) -> R.drawable.ic_7_0_months
                        in (6.5..7.0) -> R.drawable.ic_6_5_months
                        in (6.0..6.5) -> R.drawable.ic_6_0_months
                        in (5.5..6.0) -> R.drawable.ic_5_5_months
                        in (5.0..5.5) -> R.drawable.ic_5_0_months
                        in (4.5..5.0) -> R.drawable.ic_4_5_months
                        in (4.0..4.5) -> R.drawable.ic_4_0_months
                        in (3.5..4.0) -> R.drawable.ic_3_5_months
                        in (3.0..3.5) -> R.drawable.ic_3_0_months
                        in (2.5..3.0) -> R.drawable.ic_2_5_months
                        in (2.0..2.5) -> R.drawable.ic_2_0_months
                        in (1.5..2.0) -> R.drawable.ic_1_5_months
                        in (1.0..1.5) -> R.drawable.ic_1_0_month
                        in (0.0..1.0) -> R.drawable.ic_0_5_month
                        else -> null
                    }
                }
            }
    }