# HealthcareContactsHelper.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.professionalcontacts

    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.extensions.fullName
    import dk.nsp.mingraviditet.ui.main.journal.extensions.nameToTextModel
    import dk.nsp.mingraviditet.ui.main.journal.extensions.preferredBirthPlaceName
    import dk.nsp.mingraviditet.ui.main.journal.extensions.toTextModel
    import dk.nsp.mingraviditet.util.extensions.toTextModelOrDefault
    import dk.nsp.mingraviditet.util.extensions.toYesNoTextModelOrDefault
    import dk.nsp.models.HealthInsuranceSection
    import dk.nsp.util.TextModel
    import kotlinx.coroutines.CoroutineScope
    class HealthcareContactsHelper(
        private val dataCardRepository: DataCardRepository,
        private val dictionaryRepository: DictionaryRepository
    ) {

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

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

            val generalPractitioner1: HealthInsuranceSection?
            val generalPractitioner2: HealthInsuranceSection?
            val activeSection = dataCard?.healthInsuranceSection

            when (activeSection?.insuranceGroup) {
                HealthInsuranceSection.InsuranceGroup._1 -> {
                    generalPractitioner1 = activeSection
                    generalPractitioner2 = null
                }
                HealthInsuranceSection.InsuranceGroup._2 -> {
                    generalPractitioner1 = null
                    generalPractitioner2 = activeSection
                }
                null -> {
                    generalPractitioner1 = null
                    generalPractitioner2 = null
                }
            }

            val gpNameKey = TextModel.from(R.string.journal_section_health_care_gp_1_name)
            val gpAddressKey = TextModel.from(R.string.journal_section_health_care_gp_address)
            val gpPostalCodeKey =
                TextModel.from(R.string.journal_section_health_care_gp_postal_code)
            val gpCityKey = TextModel.from(R.string.journal_section_health_care_gp_city)
            val phoneNumberKey = TextModel.from(R.string.journal_section_health_care_gp_phone_number)

            val group1 = listOf(
                JournalSectionRow(
                    gpNameKey,
                    generalPractitioner1?.generalPractitioner?.name?.fullName.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, gpNameKey)
                ),
                JournalSectionRow(
                    phoneNumberKey,
                    generalPractitioner1?.generalPractitionerTelephoneNumber.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, phoneNumberKey)
                ),
                JournalSectionRow(
                    gpAddressKey,
                    generalPractitioner1?.generalPractitionerAddress?.streetAndNumber.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, gpAddressKey)
                ),
                JournalSectionRow(
                    gpPostalCodeKey,
                    generalPractitioner1?.generalPractitionerAddress?.zip.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, gpPostalCodeKey)
                ),
                JournalSectionRow(
                    gpCityKey,
                    generalPractitioner1?.generalPractitionerAddress?.city.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, gpCityKey)
                )
            )

            //Group 2
            val gp2NameKey = TextModel.from(R.string.journal_section_health_care_gp_2_name)

            val group2 = listOf(
                JournalSectionRow(
                    gp2NameKey,
                    generalPractitioner2?.generalPractitioner?.name?.fullName.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, gp2NameKey)
                ),
                JournalSectionRow(
                    phoneNumberKey,
                    generalPractitioner2?.generalPractitionerTelephoneNumber.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, phoneNumberKey)
                ),
                JournalSectionRow(
                    gpAddressKey,
                    generalPractitioner2?.generalPractitionerAddress?.streetAndNumber.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, gpAddressKey)
                ),
                JournalSectionRow(
                    gpPostalCodeKey,
                    generalPractitioner2?.generalPractitionerAddress?.zip.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, gpPostalCodeKey)
                ),
                JournalSectionRow(
                    gpCityKey,
                    generalPractitioner2?.generalPractitionerAddress?.city.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, gpCityKey)
                )
            )

            return group1 + group2
        }

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

            val assignedMidwifeClinicKey =
                TextModel.from(R.string.journal_section_health_care_midwife_assigned_clinic)
            val craftClassDesiredKey =
                TextModel.from(R.string.journal_section_health_care_midwife_craft_class_desired)
            val craftClassConsultationFormKey =
                TextModel.from(R.string.journal_section_health_care_midwife_consultation_form)
            val midwifeClinicWishesKey =
                TextModel.from(R.string.journal_section_health_care_midwife_clinic_wishes)
            val midwifePractitionerWishesKey =
                TextModel.from(R.string.journal_section_health_care_midwife_practitioner_wishes)
            val midwifeWishesNoteKey =
                TextModel.from(R.string.journal_section_health_care_midwife_weekday_wish)

            return listOf(
                JournalSectionRow(
                    assignedMidwifeClinicKey,
                    midwifeClinicSection?.assignedMidwifeClinic.nameToTextModel(),
                    null,
                    getDictionaryEntryFor(scope, assignedMidwifeClinicKey)
                ),
                JournalSectionRow(
                    craftClassDesiredKey,
                    midwifeClinicSection?.parentalCraftClassDesired?.parentalCraftClassDesired.toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, craftClassDesiredKey)
                ),
                JournalSectionRow(
                    craftClassConsultationFormKey,
                    midwifeClinicSection?.parentalCraftClassDesired?.consultationForm.toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, craftClassConsultationFormKey)
                ),
                JournalSectionRow(
                    midwifeClinicWishesKey,
                    dataCard?.midwifeConsultationSection?.midwifeConsultationWishes?.location.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, midwifeClinicWishesKey)
                ),
                JournalSectionRow(
                    midwifePractitionerWishesKey,
                    dataCard?.midwifeConsultationSection?.midwifeConsultationWishes?.midwife.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, midwifePractitionerWishesKey)
                ),
                JournalSectionRow(
                    midwifeWishesNoteKey,
                    dataCard?.midwifeConsultationSection?.midwifeConsultationWishes?.weekday.toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, midwifeWishesNoteKey)
                )
            )
        }

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

            val plannedBirthPlaceKey =
                TextModel.from(R.string.journal_section_health_care_birthplace_planned)
            val desiredBirthPlaceKey =
                TextModel.from(R.string.journal_section_health_care_birthplace_desired)
            val homeBirthKey = TextModel.from(R.string.journal_section_health_care_birthplace_home)

            return listOfNotNull(
                JournalSectionRow(
                    plannedBirthPlaceKey,
                    dataCard.preferredBirthPlaceName().toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, plannedBirthPlaceKey)
                ),
                JournalSectionRow(
                    desiredBirthPlaceKey,
                    dataCard?.desiredBirthPlaceSection?.desiredBirthPlace.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, desiredBirthPlaceKey)
                ),
                JournalSectionRow(
                    homeBirthKey,
                    dataCard?.desiredBirthPlaceSection?.homeBirth.toYesNoTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, homeBirthKey)
                )
            )
        }

        fun getNurseSection(scope: CoroutineScope): List<JournalSectionRow> {
            val dataCard = dataCardRepository.currentDataCard.value
            val healthCareSection = dataCard?.healthcareSection

            val wardKey = TextModel.from(R.string.journal_section_health_care_nurse_ward)
            val nurseNameKey = TextModel.from(R.string.journal_section_health_care_nurse_nurse_name)
            val nursePhoneKey =
                TextModel.from(R.string.journal_section_health_care_nurse_nurse_phonenumber)

            return listOf(
                JournalSectionRow(
                    wardKey,
                    healthCareSection?.ward.nameToTextModel(),
                    null,
                    getDictionaryEntryFor(scope, wardKey)
                ),
                JournalSectionRow(
                    nurseNameKey,
                    healthCareSection?.healthNurse?.name.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, nurseNameKey)
                ),
                JournalSectionRow(
                    nursePhoneKey,
                    healthCareSection?.healthNurse?.phone.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, nursePhoneKey)
                )
            )
        }
    }