# PersonalDataHelper.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.personaldata

    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.adapterviews.KeyValueData
    import dk.nsp.mingraviditet.ui.adapterviews.KeyValueRow
    import dk.nsp.mingraviditet.ui.adapterviews.KeyValueSectionHeader
    import dk.nsp.mingraviditet.ui.main.journal.extensions.toTextModel
    import dk.nsp.mingraviditet.util.android.recycler.RecyclerAdapterView
    import dk.nsp.mingraviditet.util.extensions.toTextModelOrDefault
    import dk.nsp.models.YesNoEnum
    import dk.nsp.util.TextModel
    import kotlinx.coroutines.CoroutineScope

    class PersonalDataHelper(
        private val dataCardRepository: DataCardRepository,
        private val dictionaryRepository: DictionaryRepository
    ) {
        private fun getDictionaryEntryFor(scope: CoroutineScope, key: TextModel): DictionaryEntry? {
            return dictionaryRepository.getDictionaryEntryFor(scope, key)
        }

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

            val cprKey = TextModel.from(R.string.journal_section_personal_data_personal_cpr)
            val nameKey = TextModel.from(R.string.journal_section_personal_data_personal_name)
            val addressKey = TextModel.from(R.string.journal_section_personal_data_personal_address)
            val zipKey = TextModel.from(R.string.journal_section_personal_data_personal_zip)
            val mobilePhoneKey =
                TextModel.from(R.string.journal_section_personal_data_personal_phonenumber_mobile)
            val homePhoneKey =
                TextModel.from(R.string.journal_section_personal_data_personal_phonenumber_home)
            val workPhoneKey =
                TextModel.from(R.string.journal_section_personal_data_personal_phonenumber_work)
            val temporaryAddressKey =
                TextModel.from(R.string.journal_section_personal_data_personal_temporary_adress)
            val temporaryAddressDurationKey =
                TextModel.from(R.string.journal_section_personal_data_personal_temporary_adress_duration)

            return listOfNotNull(
                JournalSectionRow(
                    cprKey,
                    personalDataSection?.cpr.toTextModelOrDefault()
                ),
                JournalSectionRow(
                    nameKey,
                    personalDataSection?.name?.fullName.toTextModelOrDefault()
                ),
                JournalSectionRow(
                    addressKey,
                    personalDataSection?.address?.streetAndNumber.toTextModelOrDefault()
                ),
                JournalSectionRow(
                    zipKey,
                    personalDataSection?.address.zipAndCityToTextModel()
                ),
                JournalSectionRow(
                    mobilePhoneKey,
                    personalDataSection?.telecoms?.mobile.toTextModelOrDefault()
                ),
                JournalSectionRow(
                    homePhoneKey,
                    personalDataSection?.telecoms?.home.toTextModelOrDefault()
                ),
                JournalSectionRow(
                    workPhoneKey,
                    personalDataSection?.telecoms?.work.toTextModelOrDefault()
                ),
                personalDataSection?.temporaryAddress?.let { tempAddress ->
                    JournalSectionRow(
                        temporaryAddressKey,
                        tempAddress.getFullAddressAsSingleLineTextModel()
                    )
                },
                personalDataSection?.temporaryAddress?.let {
                    JournalSectionRow(
                        temporaryAddressDurationKey,
                        it.getDurationTextModel()
                    )
                }
            )
        }

        fun getPersonalLifeTogetherAndHome(scope: CoroutineScope): List<JournalSectionRow> {
            val dataCard = dataCardRepository.currentDataCard.value
            val socialHistory = dataCard?.socialHistorySection

            val partnershipStatusKey =
                TextModel.from(R.string.journal_section_personal_data_practical_partnership_status)
            val livingConditionsKey =
                TextModel.from(R.string.journal_section_personal_data_practical_living_conditions)
            val partnerNameKey =
                TextModel.from(R.string.journal_section_personal_data_practical_partners_name)
            val partnerCprKey =
                TextModel.from(R.string.journal_section_personal_data_practical_partners_cpr)

            return listOf(
                JournalSectionRow(
                    partnershipStatusKey,
                    socialHistory?.maritalOrPartnershipStatus.toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, partnershipStatusKey)
                ),
                JournalSectionRow(
                    livingConditionsKey,
                    socialHistory?.livingConditions.toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, livingConditionsKey)
                ),
                JournalSectionRow(
                    partnerNameKey,
                    dataCard?.childPartnerSection?.name?.fullName.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, partnerNameKey)
                ),
                JournalSectionRow(
                    partnerCprKey,
                    dataCard?.childPartnerSection?.identifier.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, partnerCprKey)
                )
            )
        }

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

            val nationalityKey =
                TextModel.from(R.string.journal_section_personal_data_practical_nationality)
            val needsTranslatorKey =
                TextModel.from(R.string.journal_section_personal_data_practical_need_translator)
            val translationLanguageKey =
                TextModel.from(R.string.journal_section_personal_data_practical_translator_language)
            val additionalInfoKey =
                TextModel.from(R.string.journal_section_personal_data_practical_additional_info)

            return listOf(
                JournalSectionRow(
                    nationalityKey,
                    languageSection?.nationality.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, nationalityKey)
                ),
                JournalSectionRow(
                    needsTranslatorKey,
                    (if (languageSection?.translationRequirements == null) YesNoEnum.False else YesNoEnum.True).toTextModel(),
                    null,
                    getDictionaryEntryFor(scope, needsTranslatorKey)
                ),
                JournalSectionRow(
                    translationLanguageKey,
                    languageSection?.translationRequirements?.language.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, translationLanguageKey)
                ),
                JournalSectionRow(
                    additionalInfoKey,
                    languageSection?.additionalInformations.toTextModelOrDefault(),
                    null,
                    getDictionaryEntryFor(scope, additionalInfoKey)
                )
            )
        }

        fun getNextOfKinSection(scope: CoroutineScope): List<RecyclerAdapterView<*>> {
            val dataCard = dataCardRepository.currentDataCard.value
            val personalDataSection = dataCard?.personalDataSection

            val nameKey = TextModel.from(R.string.journal_section_personal_data_next_of_kin_name)
            val relationKey =
                TextModel.from(R.string.journal_section_personal_data_next_of_kin_relation)
            val mobilePhoneKey =
                TextModel.from(R.string.journal_section_personal_data_next_of_kin_phonenumber_mobile)
            val homePhoneKey =
                TextModel.from(R.string.journal_section_personal_data_next_of_kin_phonenumber_home)
            val workPhoneKey =
                TextModel.from(R.string.journal_section_personal_data_next_of_kin_phonenumber_work)
            val additionalInfoKey =
                TextModel.from(R.string.journal_section_personal_data_next_of_kin_additional_info)

            val rows = personalDataSection?.nextOfKin?.let {
                it.mapIndexed { index, nextOfKin ->
                    listOf(
                        KeyValueSectionHeader(
                            TextModel.from(
                                R.string.journal_section_personal_data_next_of_kin_title,
                                TextModel.from(if (it.size == 1) "" else (index + 1).toString())
                            )
                        ),
                        KeyValueRow(
                            KeyValueData(
                                nameKey,
                                nextOfKin.name?.fullName.toTextModelOrDefault(),
                                null,
                                null
                            )
                        ),
                        KeyValueRow(
                            KeyValueData(
                                relationKey,
                                nextOfKin.relation.toTextModel(),
                                null,
                                null
                            )
                        ),
                        KeyValueRow(
                            KeyValueData(
                                mobilePhoneKey,
                                nextOfKin.telecoms?.mobile.toTextModelOrDefault(),
                                null,
                                null
                            )
                        ),
                        KeyValueRow(
                            KeyValueData(
                                homePhoneKey,
                                nextOfKin.telecoms?.home.toTextModelOrDefault(),
                                null,
                                null
                            )
                        ),
                        KeyValueRow(
                            KeyValueData(
                                workPhoneKey,
                                nextOfKin.telecoms?.work.toTextModelOrDefault(),
                                null,
                                null
                            )
                        ),
                        KeyValueRow(
                            KeyValueData(
                                additionalInfoKey,
                                nextOfKin.additionalInformation.toTextModelOrDefault(),
                                null,
                                null
                            )
                        )
                    )
                }.flatten()
            }

            return rows ?: emptyList()
        }
    }