package com.horrortv.app.presentation

import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import androidx.navigation.navDeepLink
import com.horrortv.app.presentation.detail.DetailScreen
import com.horrortv.app.presentation.detail.DetailViewModel
import com.horrortv.app.presentation.home.HomeScreen
import com.horrortv.app.presentation.home.HomeViewModel
import com.horrortv.app.presentation.player.PlayerScreen
import com.horrortv.app.presentation.search.SearchScreen
import com.horrortv.app.presentation.search.SearchViewModel

object Routes {
    const val HOME = "home"
    const val SEARCH = "search"
    const val DETAIL = "detail/{imdbId}"
    const val PLAYER = "player/{imdbId}/{title}"
    
    object Args {
        const val IMDB_ID = "imdbId"
        const val TITLE = "title"
    }
    
    object DeepLinks {
        const val SCHEME = "horrortv"
        const val HOST = "horrortv.app"
        const val PATH_MOVIE = "/movie"
        const val PATH_PLAY = "/play"
    }
    
    fun detail(imdbId: String): String = "detail/$imdbId"
    fun player(imdbId: String, title: String): String = "player/$imdbId/${encodeTitle(title)}"
    
    private fun encodeTitle(title: String): String = Uri.encode(title)
}

@Composable
fun AppNavigation(
    navController: NavHostController,
    deepLinkUri: Uri? = null,
    modifier: androidx.compose.ui.Modifier = androidx.compose.ui.Modifier
) {
    NavHost(
        navController = navController,
        startDestination = Routes.HOME,
        modifier = modifier
    ) {
        composable(Routes.HOME) {
            val viewModel: HomeViewModel = hiltViewModel()
            HomeScreen(
                viewModel = viewModel,
                onNavigateToSearch = { navController.navigate(Routes.SEARCH) },
                onNavigateToDetail = { imdbId -> navController.navigate(Routes.detail(imdbId)) }
            )
        }
        
        composable(Routes.SEARCH) {
            val viewModel: SearchViewModel = hiltViewModel()
            SearchScreen(
                viewModel = viewModel,
                onNavigateBack = { navController.popBackStack() },
                onNavigateToDetail = { imdbId -> navController.navigate(Routes.detail(imdbId)) }
            )
        }
        
        composable(
            route = Routes.DETAIL,
            arguments = listOf(
                navArgument(Routes.Args.IMDB_ID) { type = NavType.StringType }
            ),
            deepLinks = listOf(
                navDeepLink {
                    uriPattern = "${Routes.DeepLinks.SCHEME}://movie/{${Routes.Args.IMDB_ID}}"
                },
                navDeepLink {
                    uriPattern = "https://${Routes.DeepLinks.HOST}${Routes.DeepLinks.PATH_MOVIE}/{${Routes.Args.IMDB_ID}}"
                }
            )
        ) {
            val viewModel: DetailViewModel = hiltViewModel()
            val imdbId = it.arguments?.getString(Routes.Args.IMDB_ID) ?: ""
            
            DetailScreen(
                imdbId = imdbId,
                viewModel = viewModel,
                onNavigateBack = { navController.popBackStack() },
                onNavigateToPlayer = { id, title -> navController.navigate(Routes.player(id, title)) }
            )
        }
        
        composable(
            route = Routes.PLAYER,
            arguments = listOf(
                navArgument(Routes.Args.IMDB_ID) { type = NavType.StringType },
                navArgument(Routes.Args.TITLE) { type = NavType.StringType }
            ),
            deepLinks = listOf(
                navDeepLink {
                    uriPattern = "${Routes.DeepLinks.SCHEME}://play/{${Routes.Args.IMDB_ID}}/{${Routes.Args.TITLE}}"
                },
                navDeepLink {
                    uriPattern = "https://${Routes.DeepLinks.HOST}${Routes.DeepLinks.PATH_PLAY}/{${Routes.Args.IMDB_ID}}/{${Routes.Args.TITLE}}"
                }
            )
        ) {
            val imdbId = it.arguments?.getString(Routes.Args.IMDB_ID) ?: ""
            val title = Uri.decode(it.arguments?.getString(Routes.Args.TITLE) ?: "Horror TV")
            
            PlayerScreen(
                imdbId = imdbId,
                title = title,
                onNavigateBack = { navController.popBackStack() }
            )
        }
    }
}