package com.ys.bdtp.adm.mvvm.ui.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.ArrowRight
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ys.bdtp.adm.app.theme.textBlackColor
import com.ys.bdtp.adm.app.theme.underColor
import com.ys.bdtp.adm.mvvm.model.db.entity.task.FloorEntity
import com.ys.bdtp.adm.mvvm.model.db.relation.BuildingWithFloors

data class Building(val id: String, val name: String, val floors: List<String>)

val buildings = listOf(
    Building("1", "建筑 A", listOf("A1", "B1", "C1", "D1", "E1", "F1", "G1")),
    Building("2", "建筑 B", listOf("A2", "B2", "C2", "D2", "E2", "F2", "G2")),
    Building("3", "建筑 C", listOf("A3", "B3", "C3", "D3", "E3", "F3", "G3")),
    Building("4", "建筑 D", listOf("A4", "B4", "C4", "D4", "E4", "F4", "G4")),
    Building("5", "建筑 E", listOf("A5", "B5", "C5", "D5", "E5", "F5", "G5"))
)


@Composable
fun ExpandList(items: List<BuildingWithFloors>, click: (FloorEntity) -> Unit) {
    var selectId by remember { mutableStateOf("1") }
    var isExpand by remember { mutableStateOf(true) }

    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .background(underColor)
    ) {
        items(
            items = items,
            key = { item -> item.building.id }
        ) { building ->
            Row(
                modifier = Modifier
                    .height(60.dp)
                    .fillMaxWidth()
                    .clickable {
                        if (selectId == building.building.id) {
                            isExpand = !isExpand
                        } else {
                            isExpand = true
                            selectId = building.building.id
                        }
                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Spacer(modifier = Modifier.width(24.dp))
                Icon(
                    imageVector = if (building.building.id == selectId && isExpand)
                        Icons.Default.ArrowDropDown
                    else
                        Icons.Default.ArrowRight,
                    contentDescription = ""
                )
                Text(text = building.building.name, color = textBlackColor)
            }

            if (building.building.id == selectId && isExpand) {
                building.floors.forEach { floor ->
                    Row(
                        modifier = Modifier
                            .height(60.dp)
                            .fillMaxWidth()
                            .clickable {
                                click(floor)
                            },
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Spacer(modifier = Modifier.width(48.dp))
                        Text(
                            text = floor.name,
                            color = textBlackColor,
                        )
                    }
                }
            }
        }
    }
}

@Preview
@Composable
fun PreviewExpandList() {
    //ExpandList(buildings)
}