package ru.forbion.f7cloud.feature.deck

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import ru.forbion.f7cloud.core.auth.AuthSession
import ru.forbion.f7cloud.core.network.UnauthorizedException

data class DeckUiState(
    val loading: Boolean = false,
    val boards: List<DeckBoard> = emptyList(),
    val selectedBoardId: Int? = null,
    val boardDetail: DeckBoardDetail? = null,
    val error: String? = null,
    val unauthorized: Boolean = false,
)

class DeckViewModel(
    private val repository: DeckRepository = DeckRepository(),
) : ViewModel() {
    private val _state = MutableStateFlow(DeckUiState())
    val state: StateFlow<DeckUiState> = _state.asStateFlow()

    fun load(session: AuthSession) {
        viewModelScope.launch(Dispatchers.IO) {
            _state.value = _state.value.copy(loading = true, error = null)
            runCatching { repository.loadBoards(session) }
                .onSuccess { boards ->
                    _state.value = _state.value.copy(loading = false, boards = boards, error = null)
                }
                .onFailure { t ->
                    _state.value = _state.value.copy(
                        loading = false,
                        error = t.message,
                        unauthorized = t is UnauthorizedException,
                    )
                }
        }
    }

    fun openBoard(session: AuthSession, boardId: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            _state.value = _state.value.copy(
                loading = true,
                selectedBoardId = boardId,
                boardDetail = null,
                error = null,
            )
            runCatching { repository.loadBoardDetail(session, boardId) }
                .onSuccess { detail ->
                    _state.value = _state.value.copy(loading = false, boardDetail = detail)
                }
                .onFailure { t ->
                    _state.value = _state.value.copy(
                        loading = false,
                        error = t.message,
                        unauthorized = t is UnauthorizedException,
                    )
                }
        }
    }

    fun closeBoard() {
        _state.value = _state.value.copy(selectedBoardId = null, boardDetail = null)
    }

    fun openCardById(session: AuthSession, cardId: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            _state.value = _state.value.copy(loading = true, error = null)
            runCatching { repository.loadCard(session, cardId) }
                .onSuccess { card ->
                    openBoard(session, card.boardId)
                }
                .onFailure { t ->
                    _state.value = _state.value.copy(
                        loading = false,
                        error = t.message ?: "Не удалось открыть карточку",
                        unauthorized = t is UnauthorizedException,
                    )
                }
        }
    }
}
