# JournalKeyValueSectionOverviewViewModel.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

    import androidx.hilt.Assisted
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.SavedStateHandle
    import androidx.lifecycle.ViewModel
    import androidx.navigation.NavDirections
    import dagger.hilt.android.lifecycle.HiltViewModel
    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.ui.main.journal.JournalKeyValueSectionDetailType
    import dk.nsp.mingraviditet.ui.main.journal.JournalSection
    import dk.nsp.mingraviditet.util.android.recycler.RecyclerAdapterView
    import dk.nsp.mingraviditet.util.extensions.toSingleEvent
    import dk.nsp.mingraviditet.util.livedata.SingleEvent
    import dk.nsp.util.TextModel
    import javax.inject.Inject

    @HiltViewModel
    class JournalKeyValueSectionOverviewViewModel @Inject constructor(
        @Assisted private val savedStateHandle: SavedStateHandle
    ) : ViewModel() {

        private val section = savedStateHandle.get<JournalSection>("journalSection")

        private val _rowsToShow: MutableLiveData<List<RecyclerAdapterView<*>>> = MutableLiveData()
        val rowsToShow: LiveData<List<RecyclerAdapterView<*>>> = _rowsToShow

        private val _navigateTo: MutableLiveData<SingleEvent<NavDirections>> = MutableLiveData()
        val navigateTo: LiveData<SingleEvent<NavDirections>> = _navigateTo

        init {
            getRowsToShow()
        }

        private fun getRowsToShow() {
            val rows: List<RecyclerAdapterView<*>> = when (section) {
                JournalSection.Screenings -> getScreeningsRows()
                JournalSection.AroundThePregnancy -> getAroundThePregnancyRows()
                JournalSection.HealthCare -> getHealthCareRows()
                JournalSection.PersonalData -> getPersonalDataRows()
                else -> listOf()
            }

            if (rows.isNotEmpty()) {
                _rowsToShow.postValue(rows)
            }
        }

        private fun getScreeningsRows(): List<RecyclerAdapterView<*>> {
            return listOf(
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(
                        TextModel.from(R.string.journal_section_screenings_bloodtype_item)
                    ) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.ScreeningsBloodtype)
                    }),
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(
                        TextModel.from(R.string.journal_section_screenings_diabetes_item)
                    ) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.ScreeningsDiabetes)
                    }),
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(
                        TextModel.from(R.string.journal_section_screenings_streptococcus_b_item)
                    ) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.ScreeningsStreptococcusGroupB)
                    }),
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(
                        TextModel.from(R.string.journal_section_screenings_d_vitamin_item)
                    ) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.ScreeningsVitaminD)
                    }),
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(
                        TextModel.from(R.string.journal_section_screenings_other_screenings_item),
                        true
                    ) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.ScreeningsOther)
                    })
            )
        }

        private fun navigateToKeyValueDetails(
            with: JournalKeyValueSectionDetailType,
            showBottomInfo: Boolean = false
        ) {
            val bottomInfoTextModel = if (showBottomInfo) {
                TextModel.from(R.string.journal_section_personal_data_personal_bottom_info_text)
            } else null
            _navigateTo.postValue(
                JournalKeyValueSectionOverviewFragmentDirections.actionJournalSectionOverviewFragmentToJournalSectionDetailsFragment(
                    with,
                    bottomInfoTextModel
                )
                    .toSingleEvent()
            )
        }

        private fun getAroundThePregnancyRows(): List<RecyclerAdapterView<*>> {
            return listOf(
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_around_the_pregnancy_expected_date_of_delivery_item)) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.AroundThePregnancyExpectedDateOfBirth)
                    }
                ),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_around_the_pregnancy_assisted_fertility_item)) {
                    navigateToKeyValueDetails(JournalKeyValueSectionDetailType.AroundThePregnancyAssistedFertility)
                }),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_around_the_pregnancy_risk_assessment_item)) {
                    navigateToKeyValueDetails(JournalKeyValueSectionDetailType.AroundThePregnancyRiskAssessment)
                }),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_around_the_pregnancy_past_pregnancies_item)) {
                    _navigateTo.postValue(
                        JournalKeyValueSectionOverviewFragmentDirections.actionJournalSectionOverviewFragmentToJournalDetailsPreviousPregnanciesFragment()
                            .toSingleEvent()
                    )
                }),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_around_the_pregnancy_health_item)) {
                    navigateToKeyValueDetails(JournalKeyValueSectionDetailType.AroundThePregnancyHealthSection)
                }),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_around_the_pregnancy_lifestyle_item)) {
                    navigateToKeyValueDetails(JournalKeyValueSectionDetailType.AroundThePregnancyLifestyle)
                }),
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(
                        TextModel.from(R.string.journal_section_around_the_pregnancy_work_environment_item),
                        true
                    ) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.AroundThePregnancyWorkEnvironment)
                    })
            )
        }

        private fun getHealthCareRows(): List<RecyclerAdapterView<*>> {
            return listOf(
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_health_care_general_practitioner_item)) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.HealthcareGeneralPractitioner)
                    }),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_health_care_midwife_item)) {
                    navigateToKeyValueDetails(JournalKeyValueSectionDetailType.HealthcareMidwifeOrganization)
                }),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_health_care_birthplace_item)) {
                    navigateToKeyValueDetails(JournalKeyValueSectionDetailType.HealthcareBirthplace)
                }),
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(
                        TextModel.from(R.string.journal_section_health_care_nurse_item),
                        true
                    ) {
                        navigateToKeyValueDetails(JournalKeyValueSectionDetailType.HealthcareNurse)
                    })
            )
        }

        private fun getPersonalDataRows(): List<RecyclerAdapterView<*>> {
            return listOf(
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_personal_data_contact_information_item)) {
                    navigateToKeyValueDetails(
                        JournalKeyValueSectionDetailType.PersonalContactInformation,
                        true
                    )
                }),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_personal_data_life_together_and_home_item)) {
                    navigateToKeyValueDetails(
                        JournalKeyValueSectionDetailType.PersonalLifeTogetherAndHome,
                        false
                    )
                }),
                JournalSectionCardItem(SectionCardListItemBindingModel(TextModel.from(R.string.journal_section_personal_data_nationality_and_language_item)) {
                    navigateToKeyValueDetails(
                        JournalKeyValueSectionDetailType.PersonalNationalityAndLanguage,
                        true
                    )
                }),
                JournalSectionCardItem(
                    SectionCardListItemBindingModel(
                        TextModel.from(R.string.journal_section_personal_data_relatives_item),
                        true
                    ) {
                        navigateToKeyValueDetails(
                            JournalKeyValueSectionDetailType.PersonalRelations,
                            true
                        )
                    }),
            )
        }
    }