# KnowledgeArticleViewModel.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 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.knowledge.article

    import androidx.hilt.Assisted
    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MediatorLiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.SavedStateHandle
    import androidx.lifecycle.ViewModel
    import dagger.hilt.android.lifecycle.HiltViewModel
    import dk.nsp.domain.knowledge.Article
    import dk.nsp.domain.knowledge.ArticleContentSection
    import dk.nsp.mingraviditet.util.android.recycler.RecyclerAdapterView
    import dk.nsp.mingraviditet.util.extensions.toSingleEvent
    import dk.nsp.mingraviditet.util.livedata.SingleEvent
    import java.time.LocalDateTime
    import javax.inject.Inject

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

        private val article = savedStateHandle.get<Article>("article")
        private val relevantId = savedStateHandle.get<Int>("relevantId") ?: -1

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

        val scrollToItem: LiveData<SingleEvent<Int>> = MediatorLiveData<SingleEvent<Int>>().apply {
            addSource(_rowsToShow) {
                findRelevantId(it)?.let { postValue(it.toSingleEvent()) }
            }
        }

        init {
            val articleRows = createArticleRows()

            _rowsToShow.postValue(articleRows)
        }

        private fun findRelevantId(adapterViews: List<RecyclerAdapterView<*>>): Int? {
            if (relevantId == -1) return null

            return adapterViews.indexOfFirst { it is ArticleParagraphCardRow && it.articleContentSection.id == relevantId }
        }

        private fun createArticleRows(): List<RecyclerAdapterView<*>> {
            return article?.let {
                val adapterViews = mutableListOf<RecyclerAdapterView<*>>()

                adapterViews.add(createExcerptRow(it.description))
                adapterViews.addAll(createParagraphCards(it.sections, relevantId))
                adapterViews.add(createLastUpdatedRow(it.lastUpdated))

                adapterViews
            } ?: emptyList()
        }

        private fun createExcerptRow(excerpt: String): ArticleExcerptRow =
            ArticleExcerptRow(excerpt)

        private fun createParagraphCards(
            articleContentSections: List<ArticleContentSection>,
            relevantId: Int
        ): List<ArticleParagraphCardRow> {
            return articleContentSections.map {
                ArticleParagraphCardRow(it, relevantId)
            }
        }

        private fun createLastUpdatedRow(lastUpdatedDate: LocalDateTime): ArticleLastUpdatedDateRow {
            return ArticleLastUpdatedDateRow(lastUpdatedDate)
        }
    }