package com.example.siamobile

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.example.siamobile.ui.theme.SIAMobileTheme
import androidx.compose.foundation.clickable

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

@Composable
fun KHSScreen() {
    val context = LocalContext.current

    // Data mahasiswa
    val mahasiswaList = listOf(
        "000000000000 EXAMPLE",
        "202210715185 AHMAD DIMAS RAMADHAN",
        "202210715139 ANDRE SAPUTRA GINTING",
        "202210715292 BAGAS WAHYU PERMANA",
        "202210715175 CHRISTIAN NOAH SIPAHUTAR",
        "202210715232 DANIEL EXAUDI GULTOM",
        "202210715131 DANIEL FERNANDO S LAIA",
        "202110715255 FADEN PRAWIRA BUDIHARJA",
        "202210715124 FAIZ DIANDRA SETIAWAN",
        "202210715340 FAJAR TRI JULLIA SIEN",
        "202210715291 HANIFAH ADHIYA",
        "202210715287 IMANUEL RODERICUS PARLINDUNGAN TEMPO",
        "202210715298 ISMA RAUDHATUL JANAH",
        "202210715160 KRISPINUS NATHANIEL INDARTO",
        "202210715118 MOHAMAD DIANDRA FERDIANSYAH",
        "202210715197 MUHAMAD HABIBALLAH",
        "202210715136 MUHAMAD RAJWA ATHORIQ",
        "202210715240 MUHAMMAD FATHAN ZULKARNAIN",
        "202210715290 NAJWA MIMA SALSABILA",
        "202210715145 RASYID DARUSMAN",
        "202210715311 REFLON ARNOLDI SIAGIAN",
        "202210715334 REHAN SURYA ARDHANA",
        "202210715088 RISKY PUJIANTO",
        "202210715237 SULTAN AHMAD RIZKI BADANI"
    )

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        // Header
        Text(
            text = "Kartu Hasil Studi",
            style = MaterialTheme.typography.headlineMedium,
            fontWeight = FontWeight.Bold
        )
        Spacer(modifier = Modifier.height(8.dp))
        Text(
            text = "Daftar Mahasiswa:",
            style = MaterialTheme.typography.bodyLarge
        )
        Spacer(modifier = Modifier.height(16.dp))

        // Daftar mahasiswa dengan scrolling
        Column(
            modifier = Modifier
                .weight(1f)
                .verticalScroll(rememberScrollState()),
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            mahasiswaList.forEach { mahasiswa ->
                KHSItem(mahasiswaName = mahasiswa)
            }
        }

        // Tombol Kembali
        Spacer(modifier = Modifier.height(16.dp))
        Button(
            onClick = { (context as? ComponentActivity)?.finish() },
            modifier = Modifier.align(Alignment.CenterHorizontally)
        ) {
            Text(text = "Kembali")
        }
    }
}

@Composable
fun KHSItem(mahasiswaName: String) {
    val context = LocalContext.current
    val mahasiswaActivityMap = mapOf(
        "000000000000 EXAMPLE" to Mhs000000000000::class.java,
        "202210715136 MUHAMAD RAJWA ATHORIQ" to Mhs202210715136::class.java
        //"202210715185 AHMAD DIMAS RAMADHAN" to Mhs202210715185::class.java,
        // silahkan ditambahkan di sini untuk NPM yang lain
    )

    Card(
        shape = RoundedCornerShape(12.dp),
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                val targetActivity = mahasiswaActivityMap[mahasiswaName]
                if (targetActivity != null) {
                    val intent = android.content.Intent(context, targetActivity)
                    context.startActivity(intent)
                }
            },
        elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
    ) {
        Box(
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
            contentAlignment = Alignment.CenterStart
        ) {
            Text(
                text = mahasiswaName,
                style = MaterialTheme.typography.bodyLarge,
                fontWeight = FontWeight.Medium
            )
        }
    }
}


/*

@Composable
fun KHSItem(mahasiswaName: String) {
    Card(
        shape = RoundedCornerShape(12.dp),
        modifier = Modifier.fillMaxWidth(),
        elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
    ) {
        Box(
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
            contentAlignment = Alignment.CenterStart
        ) {
            Text(
                text = mahasiswaName,
                style = MaterialTheme.typography.bodyLarge,
                fontWeight = FontWeight.Medium
            )
        }
    }
}
*/