import { useEffect, useState } from 'react'

const ACT_GAMEOVER = 2
const ACT_ENLIST_FULL = 2
const ACT_SIGNED = 1

export default function useStatus(info) {
  const { dynamicId, isSign, activityStatus, isEnlist, enlistStart, enlistEnd } = info || {}

  const [disabled, setDisabled] = useState(false)
  const [btnText, setBtnText] = useState('立即报名')

  useEffect(() => {
    if (!dynamicId) return;

    // 活动结束
    if (activityStatus === ACT_GAMEOVER) {
      setBtnText('活动已结束')
      setDisabled(true)
      return;
    }

    // 报名人数已满
    if (isEnlist === ACT_ENLIST_FULL) {
      setBtnText('人数已满')
      setDisabled(true)
      return;
    }

    // 已报名
    if (isSign === ACT_SIGNED) {
      setBtnText('已报名')
      setDisabled(true)
      return;
    }

    // 报名时间
    const now = new Date()
    const start = new Date(enlistStart)
    const end = new Date(enlistEnd)
    if (now < start) {
      setBtnText('报名未开始')
      setDisabled(true)
      return;
    }

    if (now > end) {
      setBtnText('报名已结束')
      setDisabled(true)
      return;
    }

    setBtnText('立即报名')
    setDisabled(false)
  },[dynamicId])

  return [btnText, disabled]
}
