import { defineStore } from 'pinia'
import { ref } from 'vue'
import { AudioController } from '@/utils/audio'

export const useCompetitionStore = defineStore('competition', () => {
  // ==========================================
  // State - 核心数据
  // ==========================================

  /** 从抽题页获取当前题目基础提纲信息 */
  const currentQuestionMainInfo = ref<Api.Competition.CurrentQuestionResponse>({} as Api.Competition.CurrentQuestionResponse)

  /** 当前选中的题目基础信息 (从抽题页获取) */
  const currentQuestionInfo = ref<Api.Competition.QuestionListRecord>({} as Api.Competition.QuestionListRecord)

  /** 当前题目的详细信息 (从详情接口获取) */
  const currentQuestionDetail = ref<Api.Competition.QuestionListDetailRound>({} as Api.Competition.QuestionListDetailRound)

  // ==========================================
  // State - 流程控制 & 交互状态
  // ==========================================

  /** 倒计时剩余时间 (秒) */
  const timeLeft = ref(0)

  /** 定时器引用 */
  const timerInterval = ref<number | null>(null)

  /** 音效控制器 */
  const audioController = new AudioController()

  // ==========================================
  // Actions - 数据操作
  // ==========================================

  /**
   * 设置题目列表
   */
  function setCurrentQuestionMainInfo(obj: Api.Competition.CurrentQuestionResponse) {
    currentQuestionMainInfo.value = obj as Api.Competition.CurrentQuestionResponse
  }

  /**
   * 清空设置题目列表
   */
  function resetCurrentQuestionMainInfo() {
    currentQuestionMainInfo.value = {} as Api.Competition.CurrentQuestionResponse
  }

  /**
   * 设置当前题目基础信息
   */
  function setCurrentQuestionInfo(info: Api.Competition.QuestionListRecord) {
    currentQuestionInfo.value = info
  }

  /**
   * 清空设置当前题目基础信息
   */
  function resetCurrentQuestionInfo() {
    currentQuestionInfo.value = {} as Api.Competition.QuestionListRecord
  }

  /**
   * 设置当前题目详情
   */
  function setCurrentQuestionDetail(detail: Api.Competition.QuestionListDetailRound) {
    currentQuestionDetail.value = detail
  }

  /**
   * 清空设置当前题目详情
   */
  function resetCurrentQuestionDetail() {
    currentQuestionDetail.value = {} as Api.Competition.QuestionListDetailRound
  }

  // ==========================================
  // Actions - 流程控制
  // ==========================================

  /**
   * 启动倒计时
   * @param duration 持续时间(秒)
   */
  function startTimer(duration: number) {
    stopTimer()
    timeLeft.value = duration

    // 播放开始音效
    audioController.play('start')

    timerInterval.value = window.setInterval(() => {
      if (timeLeft.value > 0) {
        timeLeft.value--
        // 5秒内播放倒计时音效
        if (timeLeft.value === 5) {
          audioController.play('boy5')
        }
      }
      else {
        stopTimer()
      }
    }, 1000)
  }

  /**
   * 停止倒计时
   */
  function stopTimer() {
    if (timerInterval.value) {
      clearInterval(timerInterval.value)
      timerInterval.value = null
    }
  }

  /**
   * 重置当前题目流程状态 (不清除题目基础信息)
   */
  function resetRoundState() {
    currentQuestionDetail.value = {} as Api.Competition.QuestionListDetailRound
    stopTimer()
    timeLeft.value = 0
  }

  /**
   * 初始化/重置所有数据
   */
  function initData() {
    currentQuestionMainInfo.value = {} as Api.Competition.CurrentQuestionResponse
    currentQuestionInfo.value = {} as Api.Competition.QuestionListRecord
    resetRoundState()
  }

  return {
    // State
    currentQuestionMainInfo,
    currentQuestionInfo,
    currentQuestionDetail,
    timeLeft,

    // Actions
    setCurrentQuestionMainInfo,
    setCurrentQuestionInfo,
    setCurrentQuestionDetail,
    startTimer,
    stopTimer,
    resetRoundState,
    initData,
    resetCurrentQuestionMainInfo,
    resetCurrentQuestionInfo,
    resetCurrentQuestionDetail,
  }
}, {
  persist: true,
})
