/*
 * F7cloud Talk - Android Client
 *
 * SPDX-FileCopyrightText: 2023 Marcel Hibbe <dev@mhibbe.de>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package ru.f7cloud.talk.openconversations.viewmodels

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import ru.f7cloud.talk.models.json.conversations.Conversation
import ru.f7cloud.talk.openconversations.data.OpenConversationsRepository
import ru.f7cloud.talk.utils.ApiUtils
import ru.f7cloud.talk.utils.database.user.CurrentUserProvider
import kotlinx.coroutines.launch
import javax.inject.Inject

class OpenConversationsViewModel @Inject constructor(
    private val repository: OpenConversationsRepository,
    private val currentUserProvider: CurrentUserProvider
) : ViewModel() {

    sealed interface ViewState

    object FetchConversationsStartState : ViewState
    object FetchConversationsEmptyState : ViewState
    object FetchConversationsErrorState : ViewState
    open class FetchConversationsSuccessState(val conversations: List<Conversation>) : ViewState

    private val _viewState: MutableLiveData<ViewState> = MutableLiveData(FetchConversationsStartState)
    val viewState: LiveData<ViewState>
        get() = _viewState

    private val _searchTerm: MutableLiveData<String> = MutableLiveData("")
    val searchTerm: LiveData<String>
        get() = _searchTerm

    fun fetchConversations() {
        _viewState.value = FetchConversationsStartState

        viewModelScope.launch {
            currentUserProvider.getCurrentUser()
                .onSuccess {
                    val apiVersion = ApiUtils.getConversationApiVersion(
                        it,
                        intArrayOf(
                            ApiUtils.API_V4,
                            ApiUtils.API_V3,
                            1
                        )
                    )
                    val url = ApiUtils.getUrlForOpenConversations(apiVersion, it.baseUrl!!)

                    repository.fetchConversations(
                        it,
                        url,
                        _searchTerm.value ?: ""
                    )
                        .onSuccess { conversations ->
                            if (conversations.isEmpty()) {
                                _viewState.value = FetchConversationsEmptyState
                            } else {
                                _viewState.value = FetchConversationsSuccessState(conversations)
                            }
                        }
                        .onFailure { exception ->
                            Log.e(TAG, "Failed to fetch conversations", exception)
                            _viewState.value = FetchConversationsErrorState
                        }
                }
        }
    }

    fun updateSearchTerm(newTerm: String) {
        _searchTerm.value = newTerm
    }

    companion object {
        private val TAG = OpenConversationsViewModel::class.simpleName
    }
}
