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.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDefaults
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.DateRangePicker
import androidx.compose.material3.DisplayMode
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.material3.rememberDateRangePickerState
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

/**
 * Material3 日期选择器（日历模式）。
 * 使用 defaultLocale()，会走 OHOS 的 CalendarLocale + PlatformDateFormat：
 * 星期名、首日、月份/日期格式等。
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun Material3DatePickerDemo() {
    val state = rememberDatePickerState()
    Column(
        Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        Text(
            "DatePicker（日历）— 测试 CalendarLocale / PlatformDateFormat",
            style = MaterialTheme.typography.subtitle1
        )
        DatePicker(
            state = state,
            modifier = Modifier.fillMaxWidth(),
            colors = DatePickerDefaults.colors()
        )
        Spacer(Modifier.height(8.dp))
        Text(
            "已选: ${state.selectedDateMillis?.toString() ?: "未选"}",
            style = MaterialTheme.typography.body2
        )
    }
}

/**
 * Material3 日期选择器（输入模式）。
 * 直接使用 PlatformDateFormat：getDateInputFormat、parse、formatWithPattern 等。
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun Material3DatePickerInputDemo() {
    val state = rememberDatePickerState(initialDisplayMode = DisplayMode.Input)
    Column(
        Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        Text(
            "DatePicker（输入）— 测试 PlatformDateFormat 解析与格式",
            style = MaterialTheme.typography.subtitle1
        )
        DatePicker(
            state = state,
            modifier = Modifier.fillMaxWidth(),
            colors = DatePickerDefaults.colors()
        )
        Text(
            "已选: ${state.selectedDateMillis?.toString() ?: "未选"}",
            style = MaterialTheme.typography.body2
        )
    }
}

/**
 * Material3 日期选择器对话框。
 * 同样使用 defaultLocale + PlatformDateFormat。
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun Material3DatePickerDialogDemo() {
    var open by remember { mutableStateOf(false) }
    val state = rememberDatePickerState()

    Column(
        Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        Text(
            "DatePickerDialog — 测试 CalendarLocale / PlatformDateFormat",
            style = MaterialTheme.typography.subtitle1
        )
        TextButton(onClick = { open = true }) {
            Text("打开日期选择对话框")
        }
        if (open) {
            DatePickerDialog(
                onDismissRequest = { open = false },
                confirmButton = {
                    TextButton(onClick = { open = false }) { Text("确定") }
                },
                dismissButton = {
                    TextButton(onClick = { open = false }) { Text("取消") }
                }
            ) {
                DatePicker(state = state, modifier = Modifier.fillMaxWidth())
            }
        }
    }
}

/**
 * Material3 日期范围选择器。
 * 使用 defaultLocale，会用到 PlatformDateFormat 的格式与首日等。
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun Material3DateRangePickerDemo() {
    val state = rememberDateRangePickerState()
    Column(
        Modifier.fillMaxWidth(),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        Text(
            "DateRangePicker — 测试 CalendarLocale / 日期格式与范围",
            style = MaterialTheme.typography.subtitle1
        )
        DateRangePicker(
            state = state,
            modifier = Modifier.fillMaxWidth(),
            colors = DatePickerDefaults.colors()
        )
        Text(
            "起: ${state.selectedStartDateMillis?.toString() ?: "未选"} 止: ${state.selectedEndDateMillis?.toString() ?: "未选"}",
            style = MaterialTheme.typography.body2
        )
    }
}
