package io.nexilis.alpha.ui.main

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import io.nexilis.alpha.ui.screen.Screen
import io.nexilis.service.data.viewmodels.BuddyViewModel

@Composable
fun Root(navController: NavHostController = rememberNavController()) {
    val viewModel: BuddyViewModel = hiltViewModel()
    val me by viewModel.me.observeAsState()
    var start = Graph.authentication
    me?.let {
        val isAnonymous = it.first_name.startsWith("USR") && it.is_change_profile == "0"
        if (!isAnonymous) {
            start = Graph.home
        }
    }
    NavHost(navController = navController, route = Graph.root, startDestination = start) {
        navigation(route = Graph.authentication, startDestination = Screen.SignIn.route) {
            composable(route = Screen.SignIn.route) {
                SignIn(navController = navController) {
                    if (it) {
                        Log.d("SAPI", ">>>>> got to home")
                        navController.navigate(Graph.home)
                    }
                }
            }
        }
        composable(route = Graph.home) {
            Main(navController = navController)
        }
        composable(route = Screen.Friend.route) {
            Friend(navController = navController)
        }
    }
}

object Graph {
    const val root = "root_graph"
    const val authentication = "auth_graph"
    const val home = "home_graph"
    const val child = "home_child_graph"
}