import logApi from '@/api/log'
import type { LockScreenTime, VideoPlayTime, StudyTime } from '@/api/log'
import type { AxiosResponse } from 'axios'

export enum LogType {
  LockScreenTime,
  VideoPlayTime,
  StudyTime,
}

type ConfigMap = {
  [LogType.LockScreenTime]: LockScreenTime
  [LogType.VideoPlayTime]: VideoPlayTime
  [LogType.StudyTime]: StudyTime
}

type ReportFunction<T> = (data: T) => Promise<AxiosResponse<any, any>>

export const useLog = async <T extends LogType>(type: T, data: ConfigMap[T]) => {
  const logTypeMap: {
    [K in LogType]?: ReportFunction<ConfigMap[K]>
  } = {
    [LogType.LockScreenTime]: logApi.reportLockScreenTime,
    [LogType.VideoPlayTime]: logApi.reportVideoPlayTime,
    [LogType.StudyTime]: logApi.reportStudyTime,
  }

  const fn = logTypeMap[type]
  try {
    if (!fn) return
    return await fn(data)
  } catch (err) {
    console.log(err)
  }
}
