package com.example.lexilearn

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.lexilearn.data.model.MaterialDataModel
import com.example.lexilearn.ui.views.pDetailMaterial.DetailMaterialScreen
import com.example.lexilearn.ui.views.pHome.HomeScreen
import com.example.lexilearn.ui.views.pLearAlphabet.LearnAlphabetScreen
import com.example.lexilearn.ui.views.pNavMaterial.NavMaterialScreen
import com.example.lexilearn.ui.views.pLearnNumber.LearnNumberScreen
import com.example.lexilearn.ui.views.pLogin.LoginScreen
import com.example.lexilearn.ui.views.pQuiz.pRead.ReadScreen
import com.example.lexilearn.ui.views.pQuiz.pSpell.SpellScreen
import com.example.lexilearn.ui.views.pQuiz.pWrite.WriteScreen
import com.example.lexilearn.ui.views.pRegister.RegisterScreen
import com.example.lexilearn.ui.views.pResultScreening.ResultScreeningScreen
import com.example.lexilearn.ui.views.pScreening.ScreeningScreen
import com.example.lexilearn.ui.views.pSplashcreen.SplashScreen
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApp()
        }
    }
}

@Composable
fun MyApp() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = "home") {
        composable("splash") { SplashScreen(navController) }
        composable("login") { LoginScreen(navController) }
        composable("register") { RegisterScreen(navController) }
        composable("home") { HomeScreen(navController) }
        composable("read") { ReadScreen(navController) }
        composable("spell") { SpellScreen(navController) }
        composable("write") { WriteScreen(navController) }
        composable("screening") { ScreeningScreen(navController) }
        composable("resultscreening") { ResultScreeningScreen(navController) }
        composable("learnAlphabet") { LearnAlphabetScreen(navController) }
        composable("learnNumber") { LearnNumberScreen(navController) }
        composable("navMaterial/{materialId}") { backStackEntry ->
            val materialId = backStackEntry.arguments?.getString("materialId")
            NavMaterialScreen(navController, materialId ?: "")
        }
        composable("detailMaterial") { backStackEntry ->
            val materialList = navController.previousBackStackEntry
                ?.savedStateHandle
                ?.get<List<MaterialDataModel>>("materialList")

            if (materialList != null) {
                DetailMaterialScreen(navController, materialList)
            }else{
                println("masuk kondisi else")
            }
        }

//        composable("detailMaterial/{materialJson}") { backStackEntry ->
//            val jsonMaterial = backStackEntry.arguments?.getString("materialJson")
//
//            // 🔥 Konversi JSON String kembali ke List<MaterialDataModel>
//            val type = object : TypeToken<List<MaterialDataModel>>() {}.type
//            val materialList: List<MaterialDataModel> = Gson().fromJson(jsonMaterial, type)
//
//            DetailMaterialScreen(navController, materialList)
//        }
    }
}