import SwiftUI

struct WordListView: View {
    let category: WordCategory
    let onRoute: (AppRoute) -> Void

    var body: some View {
        List(category.words) { word in
            NavigationLink(value: AppRoute.word(word)) {
                VStack(alignment: .leading, spacing: 4) {
                    Text(word.word)
                        .font(.headline)
                    Text(word.meaning)
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                }
                .padding(.vertical, 2)
            }
        }
        .navigationTitle(category.name)
        .toolbar {
            ToolbarItem(placement: .primaryAction) {
                Menu {
                    Button("英选汉") { startQuiz(.enToZh) }
                    Button("汉选英") { startQuiz(.zhToEn) }
                } label: {
                    Label("测验", systemImage: "questionmark.circle")
                }
            }
        }
    }

    private func startQuiz(_ mode: QuizMode) {
        if category.words.count <= 10 {
            onRoute(.quiz(QuizConfig(mode: mode, words: category.words)))
        } else {
            onRoute(.quizSetup(QuizSetupRequest(mode: mode, words: category.words)))
        }
    }
}

#Preview {
    NavigationStack {
        WordListView(category: WordCategory(id: "colors", name: "颜色", icon: "paintpalette.fill", words: [
            Word(word: "red", phonetic: "/red/", meaning: "红色", example: "The apple is red.", exampleMeaning: "苹果是红色的。")
        ])) { _ in }
    }
}
