package com.aiosman.riderpro.ui.composables

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontVariation.width
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.aiosman.riderpro.R
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch

data class MenuItem(
    val title: String,
    val icon: Int,
    val action: () -> Unit

)

@Composable
fun DropdownMenu(
    expanded: Boolean = false,
    menuItems: List<MenuItem> = emptyList(),
    width: Int? = null,
    onDismissRequest: () -> Unit = {},
) {
    MaterialTheme(
        shapes = MaterialTheme.shapes.copy(
            extraSmall = RoundedCornerShape(
                16.dp
            )
        )
    ) {
        androidx.compose.material3.DropdownMenu(
            expanded = expanded,
            onDismissRequest = onDismissRequest,
            modifier = Modifier
                .let {
                    if (width != null) it.width(width.dp) else it
                }
                .background(Color.White)
        ) {
            for (item in menuItems) {
                Box(
                    modifier = Modifier
                        .padding(vertical = 14.dp, horizontal = 24.dp)
                        .noRippleClickable {
                            item.action()
                        }) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(
                            item.title,
                            fontWeight = FontWeight.W500
                        )
                        if (width != null) {
                            Spacer(modifier = Modifier.weight(1f))

                        } else {
                            Spacer(modifier = Modifier.width(16.dp))
                        }
                        Icon(
                            painter = painterResource(id = item.icon),
                            contentDescription = "",
                            modifier = Modifier.size(24.dp)
                        )
                    }
                }
            }
        }
    }
}