/*
 * Book's Story — free and open-source Material You eBook reader.
 * Copyright (C) 2024-2026 Acclorite
 * SPDX-License-Identifier: GPL-3.0-only
 */

package org.dueattendant149.bookshelf.ui.browse

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.flow.SharedFlow
import org.dueattendant149.bookshelf.R
import org.dueattendant149.bookshelf.core.helpers.toggle
import org.dueattendant149.bookshelf.presentation.browse.BrowseEffect
import org.dueattendant149.bookshelf.presentation.library.LibraryScreen
import org.dueattendant149.bookshelf.presentation.settings.BrowseSettingsScreen
import org.dueattendant149.bookshelf.ui.common.helpers.LocalSettings
import org.dueattendant149.bookshelf.ui.common.helpers.showToast
import org.dueattendant149.bookshelf.ui.navigator.LocalNavigator

@Composable
fun BrowseEffects(effects: SharedFlow<BrowseEffect>, focusRequester: FocusRequester) {
    val navigator = LocalNavigator.current
    val settings = LocalSettings.current
    val context = LocalContext.current

    LaunchedEffect(effects, focusRequester, context, navigator, settings.browsePinnedPaths.value) {
        effects.collect { effect ->
            when (effect) {
                is BrowseEffect.OnRequestFocus -> {
                    focusRequester.requestFocus()
                }

                is BrowseEffect.OnBooksAdded -> {
                    context.getString(R.string.books_added)
                        .showToast(context = context)
                }

                is BrowseEffect.OnNavigateToLibrary -> {
                    navigator.push(LibraryScreen, saveInBackStack = false)
                }

                is BrowseEffect.OnUpdatePinnedPaths -> {
                    settings.browsePinnedPaths.update(
                        settings.browsePinnedPaths.lastValue.toggle(effect.path)
                    )
                }

                is BrowseEffect.OnNavigateToBrowseSettings -> {
                    navigator.push(BrowseSettingsScreen)
                }
            }
        }
    }
}