package id.ac.ubharajaya.panicbutton

import android.content.Intent
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.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed

class RuanganActivity : ComponentActivity() {

    private val denahUtara = listOf(
        "R122","R121","R120","R119","R118","R117",
        "R123","R124","R125","Toilet Pria Utara","Toilet Wanita Utara"
    )

    private val denahSelatan = listOf(
        "R116","Biro Pemasaran","BK & Pusat Karir","R107","R108","R109",
        "R111","R112","R106","R105","R104","R103","R102","R101",
        "Ruang Laboratorium Teknik","Toilet Pria Selatan","Toilet Wanita Selatan"
    )

    private val denahLabKomp = listOf(
        "R319","R318","R317","R316","R315","R320","R321","R322","R323","R324",
        "Toilet Pria Lantai 3","Toilet Wanita Lantai 3"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            PilihRuanganScreen { ruangan ->
                val denah = when (ruangan) {
                    in denahUtara -> "utara"
                    in denahSelatan -> "selatan"
                    in denahLabKomp -> "labkomp"
                    else -> "utara"
                }

                val intent = Intent(this, JalurEvakuasiActivity::class.java)
                intent.putExtra("denah", denah)
                startActivity(intent)
            }
        }
    }
}

@Composable
fun PilihRuanganScreen(onSelect: (String) -> Unit) {

    val semuaRuangan = listOf(
        "Utara" to listOf(
            "R122","R121","R120","R119","R118","R117",
            "R123","R124","R125"
        ),
        "Selatan" to listOf(
            "R116","Biro Pemasaran","BK & Pusat Karir",
            "R107","R108","R109","R111","R112","R106","R105",
            "R104","R103","R102","R101","Ruang Laboratorium Teknik"
        ),
        "Lab Komputer Pemrograman (Lantai 3)" to listOf(
            "R319","R318","R317","R316","R315",
            "R320","R321","R322","R323","R324"
        )
    )

    // -------------- FIX: SCROLLING DENGAN LAZYCOLUMN ----------------
    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {

        item {
            Text(
                "Pilih Ruangan Anda",
                style = MaterialTheme.typography.headlineSmall
            )
            Spacer(modifier = Modifier.height(12.dp))
        }

        semuaRuangan.forEach { (judul, rooms) ->
            item {
                Text(judul, style = MaterialTheme.typography.titleMedium)
                Spacer(modifier = Modifier.height(6.dp))
            }

            items(rooms.size) { index ->
                val r = rooms[index]
                Card(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 4.dp)
                        .clickable { onSelect(r) }
                ) {
                    Text(r, modifier = Modifier.padding(16.dp))
                }
            }

            item { Spacer(modifier = Modifier.height(20.dp)) }
        }
    }
}