import { defineStore } from 'pinia'
import type {
  TopicDiscussionConfig,
  PracticeMode,
  ScoreMode,
  EvalDimensions,
  PreviewDialogueState,
} from '@/types/englishSpeaking'

const DEFAULT_CONFIG: TopicDiscussionConfig = {
  topic: 'How I get to school',
  grade: 'grade5-1',
  selectedRole: 'tom',
  learningGoals: {
    vocabulary: ['on foot', 'by bus', 'by bike', 'by car', 'by underground', 'bus stop', 'near', 'far from'],
    sentences: [
      'How do you come to school?',
      'I come to school by bus.',
      'I come to school on foot.',
      'Do you live near school?',
      'I live far from school.',
      'How long does it take?',
    ],
  },
  practice: {
    mode: 'time',
    rounds: 2,
    duration: 5,
    showEnglish: true,
    taskHint: true,
    stutterHint: true,
  },
  evaluation: {
    showReport: true,
    dimensions: {
      accuracy: true,
      fluency: true,
      completeness: true,
      rhythm: true,
    },
    scoreMode: 'letter',
  },
}

export const useSpeakingStore = defineStore('speaking', {
  state: () => ({
    config: { ...DEFAULT_CONFIG } as TopicDiscussionConfig,
    // 话题讨论预览当前阶段（由 TopicDiscussionPreview 同步）
    previewState: 'ready' as PreviewDialogueState,
    // 请求重置预览的递增信号（由 CanvasTool 触发 → TopicDiscussionPreview 监听）
    resetSignal: 0,
    // 请求打开配置面板的递增信号（由画布点击 77 型 frame 触发 → CollapsibleToolbar + SpeakingPanel 监听）
    openConfigSignal: 0,
  }),

  actions: {
    // 重置为默认配置
    resetConfig() {
      this.config = JSON.parse(JSON.stringify(DEFAULT_CONFIG))
    },

    setPreviewState(state: PreviewDialogueState) {
      this.previewState = state
    },
    requestResetPreview() {
      this.resetSignal += 1
    },
    openConfigPanel() {
      this.openConfigSignal += 1
    },

    // 用推荐卡片数据预填充
    prefillFromTask(topic: string, vocabulary: string[], sentences: string[]) {
      this.resetConfig()
      this.config.topic = topic
      this.config.learningGoals.vocabulary = [...vocabulary]
      this.config.learningGoals.sentences = [...sentences]
    },

    // 话题
    updateTopic(topic: string) {
      this.config.topic = topic
    },

    // 年级
    updateGrade(grade: string) {
      this.config.grade = grade
    },

    // 学习目标 - 词汇
    addVocabulary(word: string) {
      this.config.learningGoals.vocabulary.push(word)
    },
    removeVocabulary(index: number) {
      this.config.learningGoals.vocabulary.splice(index, 1)
    },

    // 学习目标 - 句型
    addSentence(sentence: string) {
      this.config.learningGoals.sentences.push(sentence)
    },
    removeSentence(index: number) {
      this.config.learningGoals.sentences.splice(index, 1)
    },
    updateSentence(index: number, sentence: string) {
      this.config.learningGoals.sentences[index] = sentence
    },

    // 练习设置
    updatePracticeMode(mode: PracticeMode) {
      this.config.practice.mode = mode
    },
    updateRounds(rounds: number) {
      this.config.practice.rounds = rounds
    },
    updateDuration(duration: number) {
      this.config.practice.duration = duration
    },
    togglePracticeSetting(key: 'showEnglish' | 'taskHint' | 'stutterHint') {
      this.config.practice[key] = !this.config.practice[key]
    },

    // 评估设置
    toggleShowReport() {
      this.config.evaluation.showReport = !this.config.evaluation.showReport
    },
    toggleEvalDimension(dimension: keyof EvalDimensions) {
      this.config.evaluation.dimensions[dimension] = !this.config.evaluation.dimensions[dimension]
    },
    updateScoreMode(scoreMode: ScoreMode) {
      this.config.evaluation.scoreMode = scoreMode
    },
  },
})
