package com.example.kmptpc_compose_sample.demos.material3

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text as MaterialText
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
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.unit.dp

@Composable
internal fun Material3ButtonDemo() {
    Column(
        Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        MaterialText("Material3 按钮", style = MaterialTheme.typography.subtitle1)
        Button(onClick = {}) { Text("Button (Filled)") }
        FilledTonalButton(onClick = {}) { Text("FilledTonalButton") }
        OutlinedButton(onClick = {}) { Text("OutlinedButton") }
        TextButton(onClick = {}) { Text("TextButton") }
    }
}

@Composable
internal fun Material3TextFieldDemo() {
    var text by remember { mutableStateOf("") }
    Column(
        Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        MaterialText("Material3 输入框", style = MaterialTheme.typography.subtitle1)
        OutlinedTextField(
            value = text,
            onValueChange = { text = it },
            label = { Text("Label") },
            placeholder = { Text("Placeholder") },
            modifier = Modifier.fillMaxWidth()
        )
    }
}

@Composable
internal fun Material3CardDemo() {
    Column(
        Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        MaterialText("Material3 卡片", style = MaterialTheme.typography.subtitle1)
        Card(
            onClick = { },
            modifier = Modifier.fillMaxWidth(),
            colors = CardDefaults.cardColors()
        ) {
            Column(Modifier.fillMaxWidth().padding(16.dp)) {
                Text("Card (Elevated)")
                Text("可点击的填充卡片", style = androidx.compose.material3.MaterialTheme.typography.bodySmall)
            }
        }
        OutlinedCard(
            onClick = { },
            modifier = Modifier.fillMaxWidth()
        ) {
            Column(Modifier.fillMaxWidth().padding(16.dp)) {
                Text("OutlinedCard")
                Text("带边框的卡片", style = androidx.compose.material3.MaterialTheme.typography.bodySmall)
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun Material3AlertDialogDemo() {
    var open by remember { mutableStateOf(false) }
    Column(
        Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        MaterialText("Material3 对话框", style = MaterialTheme.typography.subtitle1)
        Button(onClick = { open = true }) { Text("打开 AlertDialog") }
        if (open) {
            AlertDialog(
                onDismissRequest = { open = false },
                title = { Text("标题") },
                text = { Text("这是 Material3 的 AlertDialog 内容。") },
                confirmButton = {
                    TextButton(onClick = { open = false }) { Text("确定") }
                },
                dismissButton = {
                    TextButton(onClick = { open = false }) { Text("取消") }
                }
            )
        }
    }
}

@Composable
internal fun Material3SwitchDemo() {
    var checked1 by remember { mutableStateOf(false) }
    var checked2 by remember { mutableStateOf(true) }
    Column(
        Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        MaterialText("Material3 Switch", style = MaterialTheme.typography.subtitle1)
        Row(
            Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text("开关 1")
            Switch(checked = checked1, onCheckedChange = { checked1 = it })
        }
        Row(
            Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text("开关 2（默认开）")
            Switch(checked = checked2, onCheckedChange = { checked2 = it })
        }
    }
}

@Composable
internal fun Material3FABDemo() {
    Column(
        Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(12.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        MaterialText("Material3 FAB", style = MaterialTheme.typography.subtitle1)
        FloatingActionButton(onClick = { }) {
            Text("+")
        }
    }
}

@Composable
internal fun Material3IconButtonDemo() {
    Column(
        Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        MaterialText("Material3 IconButton", style = MaterialTheme.typography.subtitle1)
        Row(
            Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            IconButton(onClick = { }) { Text("♡") }
            IconButton(onClick = { }) { Text("✎") }
            IconButton(onClick = { }) { Text("⋮") }
        }
    }
}
