# DataCardExtensions.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.extensions

    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.ui.main.overview.widget.DidGiveBirthCallback
    import dk.nsp.mingraviditet.ui.main.overview.widget.OverviewPregnancyState
    import dk.nsp.mingraviditet.ui.main.overview.widget.PresentDateOfBirthCallback
    import dk.nsp.shared.util.extensions.toMediumFormat
    import dk.nsp.models.DataCard
    import dk.nsp.util.textmodel.TextModel
    import dk.nsp.util.isEnded
    import dk.nsp.util.toTextModel
    import java.time.LocalDateTime

    val DataCard.userFullname: String?
        get() = recordTarget.name?.fullName

    val DataCard.trimester: Int?
        get() = expectedDateOfDeliverySection?.trimester

    val DataCard.week: Int?
        get() = expectedDateOfDeliverySection?.week

    val DataCard.dueDate: LocalDateTime?
        get() = expectedDateOfDeliverySection?.dateOfDelivery

    val DataCard.gestationAge: String?
        get() = expectedDateOfDeliverySection?.gestationAge

    val DataCard?.numberOfFetuses: Int
        get() = this?.multiplePregnancySection?.numberOfFetuses ?: 1

    val DataCard.changeDataCardTitle: TextModel
        get() {
            val dueDate = dueDate.toMediumFormat()
            val closeDateTime = closeDateTime.toMediumFormat()

            return when {
                isEnded
                        && closeDateTime != null
                -> TextModel.from(
                    R.string.overview_pick_pregnancy_ended_date,
                    closeDateTime.toTextModel()
                )
                !isEnded
                        && dueDate != null
                -> TextModel.from(
                    R.string.overview_pick_pregnancy_active_date,
                    dueDate.toTextModel()
                )
                else -> TextModel.Companion.from(R.string.common_missing_data)
            }
        }

    fun DataCard?.getOverviewPregnancyState(
        dateOfBirth: LocalDateTime?,
        onDidGiveBirthPressed: DidGiveBirthCallback,
        presentDateOfBirthCallback: PresentDateOfBirthCallback
    ): OverviewPregnancyState {
        return this?.let { it ->
            when {
                it.dueDate == null -> OverviewPregnancyState.None
                it.isEnded -> OverviewPregnancyState.PregnancyEnded(
                    it.numberOfFetuses
                )
                dateOfBirth != null -> {
                    OverviewPregnancyState.ActiveAndGaveBirth(
                        dateOfBirth,
                        it.numberOfFetuses,
                        presentDateOfBirthCallback
                    )
                }
                else -> OverviewPregnancyState.ActiveAndWaitingForBirth(
                    monthsIntoPregnancy = it.expectedDateOfDeliverySection?.monthsIntoPregnancy,
                    dueDate = it.dueDate,
                    week = it.week,
                    gestationAgeString = it.gestationAge,
                    trimester = it.trimester,
                    numberOfFetuses = it.numberOfFetuses,
                    onDidGiveBirthPressed
                )

            }
        } ?: OverviewPregnancyState.None
    }