package com.jishisongfu.duobibi.ui.records

import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FilterChipDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.jishisongfu.duobibi.ui.ALL_PLATFORMS
import com.jishisongfu.duobibi.ui.theme.DbbColors

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NewRecordScreen(onBack: () -> Unit, vm: NewRecordViewModel = hiltViewModel()) {
    // 表单字段:旋转/切后台不丢内容,避免重输。
    var title by rememberSaveable { mutableStateOf("") }
    var priceText by rememberSaveable { mutableStateOf("") }
    var source by rememberSaveable { mutableStateOf(ALL_PLATFORMS.first()) }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("手动记一笔", fontWeight = FontWeight.Bold, color = DbbColors.Ink) },
                navigationIcon = { IconButton(onClick = onBack) { Icon(Icons.AutoMirrored.Filled.ArrowBack, "返回", tint = DbbColors.Ink) } },
                colors = TopAppBarDefaults.topAppBarColors(containerColor = DbbColors.Card),
            )
        },
    ) { padding ->
        Column(
            Modifier.padding(padding).fillMaxSize().background(MaterialTheme.colorScheme.background)
                .verticalScroll(rememberScrollState()).padding(16.dp),
        ) {
            OutlinedTextField(value = title, onValueChange = { title = it }, label = { Text("商品名") },
                singleLine = true, modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(14.dp))
            Spacer(Modifier.height(12.dp))
            OutlinedTextField(
                value = priceText,
                onValueChange = { priceText = it.filter { c -> c.isDigit() || c == '.' } },
                label = { Text("到手价(元)") },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
                singleLine = true, modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(14.dp),
            )
            Spacer(Modifier.height(14.dp))
            Text("来源平台", color = DbbColors.InkSec, fontWeight = FontWeight.Medium)
            Spacer(Modifier.height(8.dp))
            Row(Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()), horizontalArrangement = Arrangement.spacedBy(6.dp)) {
                ALL_PLATFORMS.forEach { p ->
                    FilterChip(selected = source == p, onClick = { source = p }, label = { Text(p) },
                        colors = FilterChipDefaults.filterChipColors(selectedContainerColor = DbbColors.Teal, selectedLabelColor = Color.White))
                }
            }
            Spacer(Modifier.height(24.dp))
            val price = priceText.toDoubleOrNull()
            Button(
                onClick = { if (title.isNotBlank() && price != null && price > 0) vm.save(title, price, source, null, onBack) },
                enabled = title.isNotBlank() && price != null && price > 0,
                modifier = Modifier.fillMaxWidth().height(50.dp), shape = RoundedCornerShape(14.dp),
                colors = ButtonDefaults.buttonColors(containerColor = DbbColors.Teal),
            ) { Text("保存到擂台", fontWeight = FontWeight.SemiBold, fontSize = 16.sp) }
            Spacer(Modifier.height(8.dp))
            Text("保存后会自动归入同名商品组,可在「比价擂台」里跨平台对比。", color = DbbColors.InkTer, fontSize = 11.sp)
        }
    }
}
