@file:Suppress("HardCodedStringLiteral")

package eu.gaudian.translator.view.dictionary

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import eu.gaudian.translator.R
import eu.gaudian.translator.ui.theme.ThemePreviews
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.LocalConnectionConfigured
import eu.gaudian.translator.view.NoConnectionScreen
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.AppTabLayout
import eu.gaudian.translator.view.composable.Screen
import eu.gaudian.translator.view.composable.TabItem
import eu.gaudian.translator.view.settings.SettingsRoutes
import eu.gaudian.translator.viewmodel.CorrectionViewModel
import eu.gaudian.translator.viewmodel.DictionaryViewModel
import eu.gaudian.translator.viewmodel.LanguageViewModel


@Composable
private fun getDictionaryTabs(): List<TabItem> {
    return listOf(
        DictionaryTab(stringResource(R.string.label_dictionary), AppIcons.Dictionary),
        DictionaryTab(stringResource(R.string.title_corrector), AppIcons.Check)
    )
}
private data class DictionaryTab(override val title: String, override val icon: ImageVector) :
    TabItem

@Composable
fun MainDictionaryScreen(
    navController: NavController
    ) {


    val activity = LocalContext.current.findActivity()

    val dictionaryViewModel: DictionaryViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val correctionViewModel: CorrectionViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val languageViewModel: LanguageViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val dictionaryTabs = getDictionaryTabs()

    val connectionConfigured = LocalConnectionConfigured.current

    if (!connectionConfigured) {
        NoConnectionScreen(onSettingsClick =  {navController.navigate(SettingsRoutes.API_KEY)})
        return
    }

    var selectedTab by remember { mutableStateOf(dictionaryTabs[0]) }
        Column {
            AppTabLayout(
                tabs = dictionaryTabs,
                selectedTab = selectedTab,
                onTabSelected = { selectedTab = it },
                onNavigateBack = {
                    if (!navController.popBackStack()) {
                        navController.navigate(Screen.Home.route) {
                            launchSingleTop = true
                            restoreState = false
                        }
                    }
                }
            )

            when (selectedTab) {
                dictionaryTabs[0] -> DictionaryScreen(
                    navController = navController,
                    onEntryClick = { entry ->
                        // Set flag indicating navigation is from external source (not DictionaryResultScreen)
                        dictionaryViewModel.setNavigatingFromDictionaryResult(false)
                        navController.navigate("dictionary_result/${entry.id}")
                    },
                    onNavigateToOptions = {
                        navController.navigate("dictionary_options")
                    }
                )
                dictionaryTabs[1] -> CorrectionScreen(
                    correctionViewModel = correctionViewModel,
                    languageViewModel = languageViewModel
                )
            }
        }
}

@ThemePreviews
@Composable
fun DictionaryHostScreenPreview() {
    val navController = rememberNavController()

    MainDictionaryScreen(
        navController = navController,
    )
}