package com.syaroful.agrilinkvocpro

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import com.syaroful.agrilinkvocpro.core.components.DownloadModuleConfirmationDialog
import com.syaroful.agrilinkvocpro.data.UserPreferences
import com.syaroful.agrilinkvocpro.navigation.SetupNavigation
import com.syaroful.agrilinkvocpro.presentation.screen.home.DynamicModuleViewModel
import com.syaroful.agrilinkvocpro.presentation.theme.AgrilinkVocproTheme
import org.koin.android.ext.android.get
import org.koin.androidx.viewmodel.ext.android.viewModel

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            AgrilinkVocproTheme {
                val dynamicModuleViewModel: DynamicModuleViewModel by viewModel()
                val userPreferences: UserPreferences = get()
                SetupNavigation(
                    dynamicModuleViewModel = dynamicModuleViewModel,
                    userPreferences = userPreferences,
                    onLaunchFeature = { className ->
                        val intent = Intent().apply {
                            setClassName(packageName, className)
                        }
                        startActivity(intent)
                    }
                )
                DownloadModuleConfirmationDialog(
                    moduleName = dynamicModuleViewModel.currentModuleToDownload.value,
                    onDismiss = { dynamicModuleViewModel.currentModuleToDownload.value = null },
                    onClickConfirm = { dynamicModuleViewModel.confirmDownload { startFeature(it) } }
                )
            }
        }
    }

    private fun startFeature(className: String) {
        val intent = Intent().apply {
            setClassName(packageName, className)
        }
        startActivity(intent)
    }
}




