import { ConstantLocalStorage } from '@/utils/constant'
import { ConstantStore } from '@/utils/constant'
import { defineStore } from 'pinia'
import { toRefs } from 'vue'
import { reactive } from 'vue'
import { useSessionStorageState } from 'vue-hooks-plus'



/**
 * 计算时间的store
 */
export const useGameCountdownStore = defineStore(ConstantStore.COUNTDOWN, () => {
  
  const [ countdown, setCountdown ] = useSessionStorageState(ConstantLocalStorage.COUNTDOWN, {
    defaultValue: 0
  })

  const _sum = (time: number) => setCountdown(countdown.value! + time)

  const _clear = () => setCountdown(0)

  return {
    sum: _sum,
    get: () => countdown.value,
    clear: _clear
  }

})

/**
 * 计算用户本张题卡获得的星星数
 * @param wrongCount 错误次数
 * @param totalStart 
 */
export const useCalcStartStore = defineStore(ConstantStore.STAR, () => {

  const state = reactive({
    wrongCount: 0,
    total: 0
  })

  const _get = () => {
    const r = state.total - state.wrongCount
    
    return r <= 0 ? 1 : r
  }

  function _clear () {
    state.wrongCount = 0
    state.total = 0
  }

  return {
    ...toRefs(state),
    get: _get,
    clear: _clear
  }

})


export const useCalcQuantityStore = defineStore(ConstantStore.QUANTITY, () => {

  const state = reactive({
    correctQuantity: 0, 
    totalQuantity: 0,
    wrongCount: 0
  })

  const _getStar = () => {
    const r = 6 - state.wrongCount
    return r <= 0 ? 1 : r
  }


  return {
    ...toRefs(state),
    getStar: _getStar,
    clear: () => {
      state.correctQuantity = 0
      state.totalQuantity = 0
      state.wrongCount = 0
    }
  }

})




export const useWisdomCoinStore = defineStore(ConstantStore.WISDOMCOIN, () => {

  // WisdomCoin
  const state = reactive({
    total: 0,  //  总的智慧币
    itemCoin: 0, // 单次玩的智慧币
    remainderWisdomCoin: 0 // 本次可加的智慧币的上限
  })

  const _set = (record: number) => state.total = record

  // 增加智慧币, 按照错误与正确的规则增加
  const _add = ( ) => {

  }

  return {
    ...state,
    add: _add
  }

})


