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(
        "R-122","R-121","R-120","R-119","R-118","R-117",
        "R-123","R-124","R-125","Toilet Pria Utara","Toilet Wanita Utara"
    )

    private val denahSelatan = listOf(
        "R-116","Biro Pemasaran","BK & Pusat Karir","R-107","R-108","R-109",
        "R-111","R-112","R-106","R-105","R-104","R-103","R-102","R-101",
        "Ruang Laboratorium Teknik","Toilet Pria Selatan","Toilet Wanita Selatan"
    )

    private val denahLabKomp = listOf(
        "R-319","R-318","R-317","R-316","R-315","R-320","R-321","R-322","R-323","R-324",
        "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 M Rafly Al Fathir (202310715043)" to listOf(
            "R-122","R-121","R-120","R-119","R-118","R-117",
            "R-123","R-124","R-125"
        ),
        "Selatan Lalu Muhammad Anggana Subhan (202310715277)" to listOf(
            "R-116","Biro Pemasaran","BK & Pusat Karir",
            "R-107","R-108","R-109","R-111","R-112","R-106","R-105",
            "R-104","R-103","R-102","R-101","Ruang Laboratorium Teknik"
        ),
        "Lab Komputer Pemrograman (Lantai 3) Rafi Fattan Fitriardi (202310715002)" to listOf(
            "R-319","R-318","R-317","R-316","R-315",
            "R-320","R-321","R-322","R-323","R-324"
        )
    )

    // -------------- 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)) }
        }
    }
}