package com.example.siamobile

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.siamobile.ui.theme.SIAMobileTheme
import android.content.Intent
import androidx.compose.ui.platform.LocalContext

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

@Composable
fun AppNavigator() {
    var currentScreen by remember { mutableStateOf("MainMenu") }

    when (currentScreen) {
        "MainMenu" -> MainMenuScreen(
            onMenuClick = { selectedMenu ->
                when (selectedMenu) {
                    "About" -> currentScreen = "AboutScreen"
                    "Pengaturan" -> currentScreen = "SettingsScreen"
                    "Bantuan" -> currentScreen = "HelpScreen"
                }
            }
        )
        "AboutScreen" -> AboutScreen(onBackClick = { currentScreen = "MainMenu" })
        "SettingsScreen" -> SettingsScreen(onBackClick = { currentScreen = "MainMenu" })
        "HelpScreen" -> HelpScreen(onBackClick = { currentScreen = "MainMenu" })
    }
}


@Composable
fun MainMenuScreen(onMenuClick: (String) -> Unit, modifier: Modifier = Modifier) {
    val context = LocalContext.current

    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.SpaceBetween
    ) {
        Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
            Text(
                text = "Selamat Datang di Aplikasi Akademik",
                style = MaterialTheme.typography.headlineMedium,
                fontWeight = FontWeight.Bold
            )
            Text(
                text = "Pilih menu di bawah untuk melanjutkan:",
                style = MaterialTheme.typography.bodyMedium
            )
            Spacer(modifier = Modifier.height(8.dp))

            val menuItems = listOf(
                "Kartu Hasil Studi",
                "Jadwal Kuliah",
                "Pengaturan",
                "Bantuan",
                "About"
            )

            menuItems.forEach { menu ->
                MenuCard(menuTitle = menu) {
                    when (menu) {
                        "Kartu Hasil Studi" -> {
                            // Membuka KHSActivity
                            context.startActivity(Intent(context, KHSActivity::class.java))
                        }
                        else -> {
                            // Menu lainnya menggunakan handler onMenuClick
                            onMenuClick(menu)
                        }
                    }
                }
            }
        }

        Footer(version = "1.0.0", copyright = "© 2025")
    }
}

@Composable
fun MenuCard(menuTitle: String, onClick: () -> Unit) {
    Card(
        shape = RoundedCornerShape(12.dp),
        modifier = Modifier
            .fillMaxWidth()
            .clickable { onClick() },
        elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
    ) {
        Box(
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
            contentAlignment = Alignment.CenterStart
        ) {
            Text(
                text = menuTitle,
                style = MaterialTheme.typography.bodyLarge,
                fontWeight = FontWeight.Medium
            )
        }
    }
}

@Composable
fun AboutScreen(onBackClick: () -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.SpaceBetween
    ) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Tentang Aplikasi",
                style = MaterialTheme.typography.headlineMedium,
                fontWeight = FontWeight.Bold
            )
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = "Aplikasi Akademik ini dibuat untuk membantu mahasiswa mengakses informasi akademik seperti jadwal, presensi, dan berita kampus. Dikembangkan oleh Fasilkom Ubharajaya.",
                style = MaterialTheme.typography.bodyMedium,
                textAlign = TextAlign.Center
            )
            Spacer(modifier = Modifier.height(16.dp))
            Text(
                text = "Versi 1.0.0",
                style = MaterialTheme.typography.bodySmall,
                fontWeight = FontWeight.Light,
                textAlign = TextAlign.Center
            )
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = "© 2025 Fasilkom Ubharajaya",
                style = MaterialTheme.typography.bodySmall,
                fontWeight = FontWeight.Light,
                textAlign = TextAlign.Center
            )
        }

        Button(
            onClick = onBackClick,
            modifier = Modifier.align(Alignment.CenterHorizontally)
        ) {
            Text(text = "Kembali ke Menu Utama")
        }
    }
}

@Composable
fun SettingsScreen(onBackClick: () -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.SpaceBetween
    ) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Pengaturan",
                style = MaterialTheme.typography.headlineMedium,
                fontWeight = FontWeight.Bold
            )
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = "Atur preferensi aplikasi Anda di sini.",
                style = MaterialTheme.typography.bodyMedium,
                textAlign = TextAlign.Center
            )
        }

        Button(
            onClick = onBackClick,
            modifier = Modifier.align(Alignment.CenterHorizontally)
        ) {
            Text(text = "Kembali ke Menu Utama")
        }
    }
}

@Composable
fun HelpScreen(onBackClick: () -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.SpaceBetween
    ) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Bantuan",
                style = MaterialTheme.typography.headlineMedium,
                fontWeight = FontWeight.Bold
            )
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = "Jika Anda membutuhkan bantuan, silakan hubungi tim IT kampus Anda atau lihat panduan pengguna.",
                style = MaterialTheme.typography.bodyMedium,
                textAlign = TextAlign.Center
            )
        }

        Button(
            onClick = onBackClick,
            modifier = Modifier.align(Alignment.CenterHorizontally)
        ) {
            Text(text = "Kembali ke Menu Utama")
        }
    }
}

@Composable
fun Footer(version: String, copyright: String) {
    Column(
        modifier = Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Versi $version",
            style = MaterialTheme.typography.bodySmall,
            textAlign = TextAlign.Center
        )
        Text(
            text = copyright,
            style = MaterialTheme.typography.bodySmall,
            textAlign = TextAlign.Center
        )
    }
}

@Preview(showBackground = true)
@Composable
fun MainMenuScreenPreview() {
    SIAMobileTheme {
        MainMenuScreen(onMenuClick = {})
    }
}

@Preview(showBackground = true)
@Composable
fun AboutScreenPreview() {
    SIAMobileTheme {
        AboutScreen(onBackClick = {})
    }
}

@Preview(showBackground = true)
@Composable
fun SettingsScreenPreview() {
    SIAMobileTheme {
        SettingsScreen(onBackClick = {})
    }
}

@Preview(showBackground = true)
@Composable
fun HelpScreenPreview() {
    SIAMobileTheme {
        HelpScreen(onBackClick = {})
    }
}
