# ScreeningsRowsHelper.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.screenings

    import dk.nsp.domain.dictionary.DictionaryEntry
    import dk.nsp.domain.journal.observations.JournalSectionRow
    import dk.nsp.domain.repository.DataCardRepository
    import dk.nsp.domain.repository.DictionaryRepository
    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.ui.main.journal.extensions.toTextModel
    import dk.nsp.util.textmodel.TextModel
    import kotlinx.coroutines.CoroutineScope

    class ScreeningsRowsHelper(
        private val dataCardRepository: DataCardRepository,
        private val dictionaryRepository: DictionaryRepository
    ) {
        private val missingDataTextModel = TextModel.from(R.string.common_missing_data)

        private fun getDictionaryEntryFor(scope: CoroutineScope, key: TextModel): DictionaryEntry? {
            return dictionaryRepository.getDictionaryEntryFor(scope, key)
        }

        fun getScreeningBloodTypeSection(scope: CoroutineScope): List<JournalSectionRow> {
            val screeningsSection = dataCardRepository.currentDataCard.value?.screeningsSection

            val bloodTakenKey = TextModel.from(R.string.journal_details_bloodtype_taken)
            val maternalRhesusBloodTypeKey =
                TextModel.from(R.string.journal_details_bloodtype_maternal_rhesus_type)
            val irregularBloodGroupAntibodyPresentKey =
                TextModel.from(R.string.journal_details_bloodtype_irregular_blood_group_antibody_present)
            val fetalRhesusTypeKey =
                TextModel.from(R.string.journal_details_bloodtype_fetal_rhesus_type)
            val maternalRhdAntibodyKey =
                TextModel.from(R.string.journal_details_bloodtype_maternal_rhd_antibody)
            val antiDImmunoglobinGivenKey =
                TextModel.from(R.string.journal_details_bloodtype_anti_d_immunoglobin_given)

            return listOf(
                JournalSectionRow(
                    bloodTakenKey,
                    screeningsSection?.maternalBloodtype?.bloodSampleTaken.toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, bloodTakenKey)
                ),
                JournalSectionRow(
                    maternalRhesusBloodTypeKey,
                    screeningsSection?.maternalRhD?.rhesusType.toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, maternalRhesusBloodTypeKey)
                ),
                JournalSectionRow(
                    irregularBloodGroupAntibodyPresentKey,
                    screeningsSection?.maternalAntibody?.irregularBloodGroupAntibodyPresent.toTextModel(),
                    null,
                    getDictionaryEntryFor(
                        scope,
                        irregularBloodGroupAntibodyPresentKey
                    )
                ),
                JournalSectionRow(
                    fetalRhesusTypeKey,
                    screeningsSection?.fetalRhDAntibody?.rhesusType.toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, fetalRhesusTypeKey)
                ),
                JournalSectionRow(
                    maternalRhdAntibodyKey,
                    screeningsSection?.hasMaternalAntibodyInWeek25IfRhesusNegative()
                        .toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, maternalRhdAntibodyKey)
                ),
                JournalSectionRow(
                    antiDImmunoglobinGivenKey,
                    screeningsSection?.antiDImmunoglobulinGiven?.value.toTextModel(),
                    screeningsSection?.antiDImmunoglobulinGiven?.date,
                    getDictionaryEntryFor(scope, antiDImmunoglobinGivenKey)
                )
            )
        }

        fun getScreeningDiabetesSection(scope: CoroutineScope): List<JournalSectionRow> {
            val screeningsSection = dataCardRepository.currentDataCard.value?.screeningsSection

            val week18to20Key = TextModel.from(R.string.journal_details_diabetes_week_18_to_20)
            val week28to30Key = TextModel.from(R.string.journal_details_diabetes_week_28_to_30)
            val glycorusiaKey = TextModel.from(R.string.journal_details_diabetes_glycorusia)

            return listOf(
                JournalSectionRow(
                    week18to20Key,
                    screeningsSection?.diabetesWeek18To20.toTextModel(),
                    screeningsSection?.diabetesWeek18To20?.date,
                    getDictionaryEntryFor(scope, week18to20Key)
                ),
                JournalSectionRow(
                    week28to30Key,
                    screeningsSection?.diabetesWeek28To30.toTextModel(),
                    screeningsSection?.diabetesWeek28To30?.date,
                    getDictionaryEntryFor(scope, week28to30Key)
                ),
                JournalSectionRow(
                    glycorusiaKey,
                    screeningsSection?.diabetesGlycosuria.toTextModel(),
                    screeningsSection?.diabetesGlycosuria?.date,
                    getDictionaryEntryFor(scope, glycorusiaKey)
                )
            )
        }

        fun getStreptococcusGroupBSection(scope: CoroutineScope): List<JournalSectionRow> {
            val screeningsSection = dataCardRepository.currentDataCard.value?.screeningsSection

            val foundKey = TextModel.from(R.string.journal_details_streptococcus_group_b_found)

            return listOf(
                JournalSectionRow(
                    foundKey,
                    screeningsSection?.getStreptococcusValue() ?: missingDataTextModel,
                    null,
                    getDictionaryEntryFor(scope, foundKey)
                )
            )
        }

        fun getVitaminDSection(scope: CoroutineScope): List<JournalSectionRow> {
            val dataCard = dataCardRepository.currentDataCard.value

            val vitaminDMeasureKey = TextModel.from(R.string.journal_details_vitamin_d_measurement)

            return listOf(
                JournalSectionRow(
                    vitaminDMeasureKey,
                    dataCard?.screeningsSection?.vitaminDConsumption.toTextModel(),
                    dataCard?.screeningsSection?.vitaminDConsumption?.date,
                    getDictionaryEntryFor(scope, vitaminDMeasureKey)
                )
            )
        }

        fun getOtherScreeningsSection(scope: CoroutineScope): List<JournalSectionRow> {
            val screeningsSection = dataCardRepository.currentDataCard.value?.screeningsSection

            val hepatitisBKey = TextModel.from(R.string.journal_details_other_screenings_hepatitis_b)
            val hivKey = TextModel.from(R.string.journal_details_other_screenings_hiv)
            val syphilisKey = TextModel.from(R.string.journal_details_other_screenings_syphilis)
            val chlamydiaKey = TextModel.from(R.string.journal_details_other_screenings_chlamydia)
            val gonorrheaKey = TextModel.from(R.string.journal_details_other_screenings_gonorrhea)
            val hemoglobinopathyKey =
                TextModel.from(R.string.journal_details_other_screenings_hemoglobinopathy)
            val mrsaSwapKey = TextModel.from(R.string.journal_details_other_screenings_mrsa_swap)

            return listOf(
                JournalSectionRow(
                    hepatitisBKey,
                    screeningsSection?.hepatitisB?.result.toTextModel(),
                    screeningsSection?.hepatitisB?.date,
                    getDictionaryEntryFor(scope, hepatitisBKey)
                ),
                JournalSectionRow(
                    hivKey,
                    screeningsSection?.hiv?.result.toTextModel(),
                    screeningsSection?.hiv?.date,
                    getDictionaryEntryFor(scope, hivKey)
                ),
                JournalSectionRow(
                    syphilisKey,
                    screeningsSection?.syphilis?.result.toTextModel(),
                    screeningsSection?.syphilis?.date,
                    getDictionaryEntryFor(scope, syphilisKey)
                ),
                JournalSectionRow(
                    chlamydiaKey,
                    screeningsSection?.chlamydia?.result.toTextModel(),
                    screeningsSection?.chlamydia?.date,
                    getDictionaryEntryFor(scope, chlamydiaKey)
                ),
                JournalSectionRow(
                    gonorrheaKey,
                    screeningsSection?.gonorrhea?.result.toTextModel(),
                    screeningsSection?.gonorrhea?.date,
                    getDictionaryEntryFor(scope, gonorrheaKey)
                ),
                JournalSectionRow(
                    hemoglobinopathyKey,
                    screeningsSection?.hemoglobinopathy?.result.toTextModel(),
                    screeningsSection?.hemoglobinopathy?.date,
                    getDictionaryEntryFor(scope, hemoglobinopathyKey)
                ),
                JournalSectionRow(
                    mrsaSwapKey,
                    screeningsSection?.mrsaSwap?.value.toTextModel(),
                    screeningsSection?.mrsaSwap?.date,
                    getDictionaryEntryFor(scope, mrsaSwapKey)
                )
            )
        }
    }