import SwiftUI

struct QuizView: View {
    let config: QuizConfig

    @Environment(SpeechService.self) private var speech
    @Environment(\.dismiss) private var dismiss

    @State private var questions: [QuizQuestion] = []
    @State private var currentIndex = 0
    @State private var selectedOption: String?
    @State private var wrongWords: [Word] = []
    @State private var finished = false
    @State private var startDate = Date.now

    private var current: QuizQuestion? {
        questions.indices.contains(currentIndex) ? questions[currentIndex] : nil
    }

    var body: some View {
        Group {
            if finished {
                QuizResultView(
                    total: questions.count,
                    wrongWords: wrongWords,
                    duration: Date.now.timeIntervalSince(startDate),
                    onRestart: restart,
                    onExit: { dismiss() }
                )
            } else if let question = current {
                questionView(question)
            }
        }
        .navigationTitle(config.mode.rawValue)
        .navigationBarTitleDisplayMode(.inline)
        .onAppear {
            if questions.isEmpty { restart() }
        }
    }

    private func questionView(_ question: QuizQuestion) -> some View {
        VStack(spacing: 24) {
            VStack(spacing: 8) {
                ProgressView(value: Double(currentIndex), total: Double(questions.count))
                Text("第 \(currentIndex + 1) / \(questions.count) 题")
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }

            VStack(spacing: 12) {
                Text(question.prompt)
                    .font(.system(size: config.mode == .enToZh ? 44 : 34, weight: .bold))
                if config.mode == .enToZh {
                    Button {
                        speech.speak(question.prompt)
                    } label: {
                        Image(systemName: "speaker.wave.2.fill")
                            .font(.title2)
                    }
                }
            }
            .frame(maxWidth: .infinity)
            .padding(.vertical, 24)

            VStack(spacing: 12) {
                ForEach(question.options, id: \.self) { option in
                    Button {
                        select(option, for: question)
                    } label: {
                        Text(option)
                            .font(.headline)
                            .frame(maxWidth: .infinity)
                            .padding(.vertical, 14)
                    }
                    .buttonStyle(.bordered)
                    .tint(optionColor(option, for: question))
                    .disabled(selectedOption != nil)
                }
            }

            if let selectedOption {
                VStack(spacing: 12) {
                    Text(selectedOption == question.answer ? "回答正确" : "正确答案：\(question.answer)")
                        .font(.headline)
                        .foregroundStyle(selectedOption == question.answer ? .green : .red)
                    Button(currentIndex + 1 < questions.count ? "下一题" : "查看成绩") {
                        next()
                    }
                    .buttonStyle(.borderedProminent)
                }
            }

            Spacer()
        }
        .padding()
    }

    private func optionColor(_ option: String, for question: QuizQuestion) -> Color {
        guard let selectedOption else { return .accentColor }
        if option == question.answer { return .green }
        if option == selectedOption { return .red }
        return .gray
    }

    private func select(_ option: String, for question: QuizQuestion) {
        guard selectedOption == nil else { return }
        selectedOption = option
        if option != question.answer {
            wrongWords.append(question.word)
        }
    }

    private func next() {
        if currentIndex + 1 < questions.count {
            currentIndex += 1
            selectedOption = nil
        } else {
            finished = true
        }
    }

    private func restart() {
        questions = QuizGenerator.makeQuestions(mode: config.mode, words: config.words)
        currentIndex = 0
        selectedOption = nil
        wrongWords = []
        finished = false
        startDate = .now
    }
}

#Preview {
    NavigationStack {
        QuizView(config: QuizConfig(mode: .enToZh, words: [
            Word(word: "red", phonetic: "/red/", meaning: "红色", example: "The apple is red.", exampleMeaning: "苹果是红色的。"),
            Word(word: "blue", phonetic: "/bluː/", meaning: "蓝色", example: "The sky is blue.", exampleMeaning: "天空是蓝色的。"),
            Word(word: "green", phonetic: "/ɡriːn/", meaning: "绿色", example: "The grass is green.", exampleMeaning: "草地是绿色的。"),
            Word(word: "yellow", phonetic: "/ˈjeloʊ/", meaning: "黄色", example: "The banana is yellow.", exampleMeaning: "香蕉是黄色的。")
        ]))
        .environment(SpeechService())
    }
}
