# FAQViewModel.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.more.support.faq

    import androidx.lifecycle.LiveData
    import androidx.lifecycle.MediatorLiveData
    import androidx.lifecycle.MutableLiveData
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
    import dagger.hilt.android.lifecycle.HiltViewModel
    import dk.nsp.domain.faq.FAQItem
    import dk.nsp.domain.repository.FAQRepository
    import dk.nsp.mingraviditet.ui.adapterviews.EmptyStateFullHeight
    import dk.nsp.mingraviditet.ui.adapterviews.ErrorStateFullHeight
    import dk.nsp.mingraviditet.ui.adapterviews.FoldableCardData
    import dk.nsp.mingraviditet.ui.adapterviews.FoldableCardRow
    import dk.nsp.mingraviditet.util.android.recycler.RecyclerAdapterView
    import dk.nsp.shared.Failed
    import dk.nsp.shared.Success
    import dk.nsp.shared.util.launchDefault
    import dk.nsp.util.TextModel
    import java.time.LocalDateTime
    import javax.inject.Inject

    @HiltViewModel
    class FAQViewModel @Inject constructor(
        private val faqRepository: FAQRepository
    ) : ViewModel() {

        private val _rowsToShow = MutableLiveData<List<RecyclerAdapterView<*>>>()
        val rowsToShow: LiveData<List<RecyclerAdapterView<*>>> =
            MediatorLiveData<List<RecyclerAdapterView<*>>>().apply {
                addSource(_rowsToShow) {
                    val rows = it.toMutableList()

                    postValue(rows)
                }
            }

        init {
            getFaqs()
        }

        private fun getFaqs() = viewModelScope.launchDefault {
            when (val faqs = faqRepository.getFAQs(viewModelScope).await()) {
                is Success -> {
                    val rows = if (faqs.value.isEmpty()) {
                        listOf(EmptyStateFullHeight(TextModel.Companion.from("Intet indhold")))
                    } else {
                        faqs.value.map { createFAQCard(it) }
                    }
                    _rowsToShow.postValue(rows)
                }
                is Failed -> {
                    _rowsToShow.postValue(listOf(ErrorStateFullHeight()))
                }
            }
        }

        private fun createFAQCard(faq: FAQItem): FoldableCardRow {
            return FoldableCardRow(
                FoldableCardData(
                    LocalDateTime.of(
                        faq.updated.year,
                        faq.updated.monthValue,
                        faq.updated.dayOfMonth,
                        faq.updated.hour,
                        faq.updated.minute,
                        faq.updated.second
                    ),
                    TextModel.Companion.from(faq.title),
                    TextModel.Companion.from(faq.text)
                )
            )
        }
    }