# JournalOverallAssessmentViewModel.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.overallassessment

    import androidx.hilt.Assisted
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.SavedStateHandle
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import dagger.hilt.android.lifecycle.HiltViewModel
    import dk.nsp.domain.dictionary.DictionaryEntry
    import dk.nsp.domain.repository.DataCardRepository
    import dk.nsp.domain.repository.DictionaryRepository
    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.ui.adapterviews.KeyValueData
    import dk.nsp.mingraviditet.ui.adapterviews.KeyValueRow
    import dk.nsp.mingraviditet.ui.main.journal.extensions.toTextModel
    import dk.nsp.mingraviditet.util.android.recycler.RecyclerAdapterView
    import dk.nsp.mingraviditet.util.extensions.toSingleEvent
    import dk.nsp.mingraviditet.util.extensions.toTextModelOrDefault
    import dk.nsp.mingraviditet.util.livedata.SingleEvent
    import dk.nsp.util.TextModel
    import kotlinx.coroutines.CoroutineScope
    import javax.inject.Inject

    @HiltViewModel
    class JournalOverallAssessmentViewModel @Inject constructor(
        private val dataCardRepository: DataCardRepository,
        private val dictionaryRepository: DictionaryRepository,
        @Assisted private val savedStateHandle: SavedStateHandle
    ) : ViewModel() {

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

        private val _onDictionary: MutableLiveData<SingleEvent<DictionaryEntry>> = MutableLiveData()
        val onDictionary: LiveData<SingleEvent<DictionaryEntry>> = _onDictionary

        init {
            createRows()
        }

        private fun createRows() {
            val dataCard = dataCardRepository.currentDataCard.value
            val overallAssessmentSection = dataCard?.overallAssessmentSection
            val prenatalAssessmentKey =
                TextModel.from(R.string.journal_section_overall_assessment_prenatal_assessment)
            val prenatalCareLevelKey =
                TextModel.from(R.string.journal_section_overall_assessment_prenatal_carelevel)
            val prenatalCommentsKey =
                TextModel.from(R.string.journal_section_overall_assessment_prenatal_comments)
            val obstetricRequired =
                TextModel.from(R.string.journal_section_overall_assessment_obstetric_required)
            val obstetricReason =
                TextModel.from(R.string.journal_section_overall_assessment_obstetrics_reason)
            val socialServicesPerinatalCareRequiredKey =
                TextModel.from(R.string.journal_section_overall_assessment_social_services_perinatal_care_required)
            val socialServicesPerinatalCareReasonKey =
                TextModel.from(R.string.journal_section_overall_assessment_social_services_perinatal_care_reason)

            val rows = listOf(
                KeyValueRow(
                    KeyValueData(
                        prenatalAssessmentKey,
                        overallAssessmentSection?.prenatalCareAssessment?.assessment.toTextModelOrDefault(),
                        null,
                        dictionaryCallback(prenatalAssessmentKey)
                    )
                ),
                KeyValueRow(
                    KeyValueData(
                        prenatalCareLevelKey,
                        overallAssessmentSection?.prenatalCareAssessment?.careLevel.toTextModel(),
                        null,
                        dictionaryCallback(prenatalCareLevelKey)
                    )
                ),
                KeyValueRow(
                    KeyValueData(
                        prenatalCommentsKey,
                        overallAssessmentSection?.prenatalCareAssessment?.comment.toTextModelOrDefault(),
                        null,
                        null
                    )
                ),
                KeyValueRow(
                    KeyValueData(
                        obstetricRequired,
                        overallAssessmentSection?.obstetricAssessment?.required.toTextModel(),
                        null,
                        dictionaryCallback(obstetricRequired)
                    )
                ),
                KeyValueRow(
                    KeyValueData(
                        obstetricReason,
                        overallAssessmentSection?.obstetricAssessment?.reason.toTextModelOrDefault(),
                        null,
                        null
                    )
                ),
                KeyValueRow(
                    KeyValueData(
                        socialServicesPerinatalCareRequiredKey,
                        overallAssessmentSection?.socialServicesPerinatalCareAssessment?.required.toTextModel(),
                        null,
                        dictionaryCallback(socialServicesPerinatalCareRequiredKey)
                    )
                ),
                KeyValueRow(
                    KeyValueData(
                        socialServicesPerinatalCareReasonKey,
                        overallAssessmentSection?.socialServicesPerinatalCareAssessment?.reason.toTextModelOrDefault(),
                        null,
                        null
                    )
                )
            )

            _rowsToShow.postValue(rows)
        }

        private fun dictionaryCallback(key: TextModel): (() -> Unit)? {
            return getDictionaryEntryFor(
                viewModelScope,
                key
            )?.let { { _onDictionary.postValue(it.toSingleEvent()) } }
        }

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