# AroundThePregnancyBll.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2020 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.sectiondetails.aroundthepregnancy

    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.util.extensions.toMediumFormat
    import dk.nsp.mingraviditet.util.extensions.toTextModelOrDefault
    import dk.nsp.models.*
    import dk.nsp.util.TextModel
    import dk.nsp.util.toTextModel

    fun ExpectedDateOfDeliverySectionAllOfExpectedDateOfDeliveryFromLastPeriod?.lastFirstDayOfCycleToTextModel(): TextModel =
        this?.lastPeriod.toMediumFormat().toTextModelOrDefault()

    fun ExpectedDateOfDeliverySectionAllOfExpectedDateOfDeliveryFromLastPeriodMenstrualCycle?.toTextModel(): TextModel {
        var string = ""
        this?.apply {
            min?.value?.let {
                string = "$it"
            }

            if (min != null && max != null) {
                string += " - "
            }

            max?.value?.let {
                string += "$it"
            }
        }
        return TextModel.from(string)
    }

    fun ExpectedDateOfDeliverySectionAllOfExpectedDateOfDeliveryFromLastPeriod?.getCalculatedExpectedDeliveryDate(): TextModel {
        return this?.expectedDateOfDelivery.toMediumFormat()?.let { TextModel.from(it) }
            ?: TextModel.from(R.string.common_missing_data)
    }

    fun WeeklyAlcoholUnitsConsumption?.getConsumption(): TextModel {
        val value = TextModel.from("${this?.consumption?.value ?: 0}")
        val unit = this?.consumption?.unit?.let { TextModel.from(it.value) }

        return if (unit == null) {
            TextModel.from(R.plurals.alcohol_units, this?.consumption?.value ?: 0, value)
        } else {
            TextModel.from(R.string.space_between_values, value, unit)
        }
    }

    fun WeeklyConsumptionDuringPregnancy?.getAverageConsumption(): TextModel {

        val value = TextModel.from("${this?.consumption?.value ?: 0}")
        val unit = this?.consumption?.unit?.let { TextModel.from(it.value) }

        return if (unit == null) {
            TextModel.from(R.plurals.alcohol_units, this?.consumption?.value ?: 0, value)
        } else {
            TextModel.from(R.string.space_between_values, value, unit)
        }
    }

    fun WeeklyConsumptionDuringPregnancy?.getExcessiveDrinkingCount(): TextModel {
        val excessiveDrinkingCount = this?.excessiveDrinkingCount?.toInt() ?: 0
        val value = TextModel.from("$excessiveDrinkingCount")

        return TextModel.from(R.plurals.excessive_drinking, excessiveDrinkingCount, value)
    }

    fun WeeklyConsumptionDuringPregnancy?.getExcessiveDrinkingWeeks(): TextModel {
        val excessiveDrinkingWeeksCommaSeparated =
            this?.excessiveDrinkingWeeks?.joinToString { it.toString() }

        return excessiveDrinkingWeeksCommaSeparated.toTextModelOrDefault()
    }

    fun WeeklyConsumptionDuringPregnancy?.getTweakPoints(): TextModel {
        return this?.tweakPoints?.ordinal?.let { TextModel.from(it.toString()) }
            ?: TextModel.from(R.string.common_missing_data)
    }

    fun WorkEnvironmentImpactSectionAllOfMotherExposureToWorkEnvironmentRisk?.getExposureDuration(): TextModel {
        val start = this?.start?.toMediumFormat()?.toTextModel()
        val end = this?.end?.toMediumFormat()?.toTextModel()

        return if (start != null && end != null) {
            TextModel.from(R.string.dash_separated_strings, start, end)
        } else if (start != null && end == null) {
            TextModel.from(R.string.dash_separated_strings, start, "".toTextModel())
        } else if (start == null && end != null) {
            TextModel.from(R.string.dash_separated_strings, "".toTextModel(), end)
        } else {
            TextModel.from(R.string.common_missing_data)
        }
    }