package com.example.livingai.pages.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import com.example.livingai.pages.addprofile.AddProfileScreen
import com.example.livingai.pages.addprofile.AddProfileViewModel
import com.example.livingai.pages.camera.CameraCaptureScreen
import com.example.livingai.pages.camera.VideoRecordScreen
import com.example.livingai.pages.camera.ViewImageScreen
import com.example.livingai.pages.camera.ViewVideoScreen
import com.example.livingai.pages.home.HomeScreen
import com.example.livingai.pages.listings.ListingsScreen
import com.example.livingai.pages.onboarding.OnBoardingScreen
import com.example.livingai.pages.onboarding.OnBoardingViewModel
import com.example.livingai.pages.ratings.RatingScreen
import com.example.livingai.pages.ratings.RatingViewModel
import com.example.livingai.pages.settings.SettingsScreen
import org.koin.androidx.compose.koinViewModel

@Composable
fun NavGraph(
    startDestination: Route
) {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = startDestination) {
        navigation<Route.AppStartNavigation>(
            startDestination = Route.OnBoardingScreen
        ) {
            composable<Route.OnBoardingScreen> {
                val onBoardingViewModel: OnBoardingViewModel = koinViewModel()
                OnBoardingScreen(
                    events = onBoardingViewModel::onEvent
                )
            }
        }

        navigation<Route.HomeNavigation>(
            startDestination = Route.HomeScreen
        ) {
            composable<Route.HomeScreen> {
                HomeScreen(navController = navController)
            }

            composable<Route.ListingsScreen> {
                ListingsScreen(
                    navController = navController
                )
            }

            composable<Route.AddProfileScreen> { backStackEntry ->
                val route: Route.AddProfileScreen = backStackEntry.toRoute()
                val viewModel: AddProfileViewModel = koinViewModel()
                val currentId by viewModel.currentAnimalId
                val videoUri by viewModel.videoUri

                val newImageUri = backStackEntry.savedStateHandle.get<String>("newImageUri")
                val newImageOrientation = backStackEntry.savedStateHandle.get<String>("newImageOrientation")
                val newVideoUri = backStackEntry.savedStateHandle.get<String>("newVideoUri")

                LaunchedEffect(newImageUri, newImageOrientation) {
                    if (newImageUri != null && newImageOrientation != null) {
                        viewModel.addPhoto(newImageOrientation, newImageUri)
                        backStackEntry.savedStateHandle.remove<String>("newImageUri")
                        backStackEntry.savedStateHandle.remove<String>("newImageOrientation")
                    }
                }
                LaunchedEffect(newVideoUri) {
                    if (newVideoUri != null) {
                        viewModel.setVideo(newVideoUri)
                        backStackEntry.savedStateHandle.remove<String>("newVideoUri")
                    }
                }

                val photos = viewModel.photos

                AddProfileScreen(
                    navController = navController,
                    viewModel = viewModel,
                    onSave = { 
                        viewModel.saveAnimalDetails()
                        navController.popBackStack(Route.HomeScreen, inclusive = false)
                    },
                    onCancel = { navController.popBackStack() },
                    onTakePhoto = { orientation ->
                        val existingPhoto = photos[orientation]
                        if (existingPhoto != null) {
                            navController.navigate(Route.ViewImageScreen(
                                imageUri = existingPhoto, 
                                shouldAllowRetake = true, 
                                orientation = orientation, 
                                showAccept = false,
                                animalId = currentId ?: "unknown"
                            ))
                        } else {
                            navController.navigate(Route.CameraScreen(orientation = orientation, animalId = currentId ?: "unknown"))
                        }
                    },
                    onTakeVideo = { 
                        if (videoUri != null) {
                            navController.navigate(Route.ViewVideoScreen(
                                videoUri = videoUri!!,
                                shouldAllowRetake = true,
                                animalId = currentId ?: "unknown"
                            ))
                        } else {
                            navController.navigate(Route.VideoRecordScreen(animalId = currentId ?: "unknown"))
                        }
                    }
                )
            }

            composable<Route.RatingScreen> {
                val viewModel: RatingViewModel = koinViewModel()
                RatingScreen(viewModel = viewModel, navController = navController)
            }

            composable<Route.SettingsScreen> {
                SettingsScreen(navController = navController)
            }
            
            composable<Route.CameraScreen> { backStackEntry ->
                val route: Route.CameraScreen = backStackEntry.toRoute()
                CameraCaptureScreen(navController = navController, orientation = route.orientation, animalId = route.animalId)
            }

            composable<Route.VideoRecordScreen> { backStackEntry ->
                val route: Route.VideoRecordScreen = backStackEntry.toRoute()
                VideoRecordScreen(navController = navController, animalId = route.animalId)
            }

            composable<Route.ViewImageScreen> { backStackEntry ->
                val args: Route.ViewImageScreen = backStackEntry.toRoute()
                ViewImageScreen(
                    imageUri = args.imageUri,
                    shouldAllowRetake = args.shouldAllowRetake,
                    showAccept = args.showAccept,
                    showBack = args.showBack,
                    onRetake = {
                        navController.popBackStack()
                        // navController.navigate(Route.CameraScreen(...))
                    },
                    onAccept = {
                        navController.getBackStackEntry<Route.AddProfileScreen>().savedStateHandle["newImageUri"] = args.imageUri
                        navController.getBackStackEntry<Route.AddProfileScreen>().savedStateHandle["newImageOrientation"] = args.orientation
                        navController.popBackStack<Route.AddProfileScreen>(inclusive = false)
                    },
                    onBack = { navController.popBackStack() }
                )
            }

            composable<Route.ViewVideoScreen> { backStackEntry ->
                val args: Route.ViewVideoScreen = backStackEntry.toRoute()
                ViewVideoScreen(
                    videoUri = args.videoUri,
                    shouldAllowRetake = args.shouldAllowRetake,
                    onRetake = {
                        navController.popBackStack()
                        navController.navigate(Route.VideoRecordScreen(animalId = args.animalId))
                    },
                    onAccept = {
                        navController.getBackStackEntry<Route.AddProfileScreen>().savedStateHandle["newVideoUri"] = args.videoUri
                        navController.popBackStack<Route.AddProfileScreen>(inclusive = false)
                    },
                    onBack = { navController.popBackStack() }
                )
            }
        }
    }
}