package tk.zwander.lockscreenwidgets.compose

import androidx.annotation.StringRes
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import tk.zwander.common.compose.components.TitleBar
import tk.zwander.common.compose.util.insetsContentPadding
import tk.zwander.lockscreenwidgets.R

private data class UsageInfo(
    @StringRes val title: Int,
    @StringRes val message: Int,
    @StringRes val dialogTitle: Int = title,
    @StringRes val dialogMessage: Int = message,
)

@Composable
private fun rememberUsageInfos(): List<UsageInfo> {
    return remember {
        [
            UsageInfo(
                title = R.string.usage_add_widget,
                message = R.string.usage_add_widget_desc
            ),
            UsageInfo(
                title = R.string.usage_modify_frame,
                message = R.string.usage_modify_frame_desc
            ),
            UsageInfo(
                title = R.string.usage_reorder_widgets,
                message = R.string.usage_reorder_widgets_desc
            ),
            UsageInfo(
                title = R.string.usage_remove_widgets,
                message = R.string.usage_remove_widgets_desc
            ),
            UsageInfo(
                title = R.string.usage_id_blacklists,
                message = R.string.usage_id_blacklists_desc
            ),
            UsageInfo(
                title = R.string.usage_widget_tiles,
                message = R.string.usage_widget_tiles_desc
            ),
        ]
    }
}

@Composable
fun UsageLayout(
    title: String,
    modifier: Modifier = Modifier
) {
    var selectedInfo by remember {
        mutableStateOf<UsageInfo?>(null)
    }

    val items = rememberUsageInfos()

    Surface(
        modifier = modifier,
    ) {
        Column(
            modifier = Modifier.fillMaxSize(),
        ) {
            TitleBar(title = title)

            LazyColumn(
                modifier = Modifier.fillMaxWidth(),
                verticalArrangement = Arrangement.spacedBy(8.dp),
                contentPadding = insetsContentPadding(
                    WindowInsets.navigationBars,
                    WindowInsets.ime,
                    extraPadding = PaddingValues(16.dp),
                ),
            ) {
                items(items, { it.hashCode() }) { item ->
                    OutlinedCard(
                        onClick = {
                            selectedInfo = item
                        },
                        modifier = Modifier.fillMaxWidth(),
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(8.dp),
                            verticalArrangement = Arrangement.spacedBy(8.dp),
                        ) {
                            Text(
                                text = stringResource(id = item.title),
                                style = MaterialTheme.typography.titleMedium,
                            )

                            Text(
                                text = stringResource(id = item.message),
                            )
                        }
                    }
                }
            }
        }
    }

    selectedInfo?.let { info ->
        AlertDialog(
            onDismissRequest = { selectedInfo = null },
            title = { Text(text = stringResource(id = info.dialogTitle)) },
            text = { Text(text = stringResource(id = info.dialogMessage)) },
            confirmButton = {
                TextButton(onClick = { selectedInfo = null }) {
                    Text(text = stringResource(id = android.R.string.ok))
                }
            },
        )
    }
}
