/* eslint-disable @typescript-eslint/member-ordering */
import { get, post } from '../request'
import { getSubjectById } from '../subject/config'
import { BasicService } from './basic'

// 任务配置
type TaskConfig = {
  id?: any
  taskId?: any
}

export type TaskWordTableConfig = {
  bookId: any // 教材
  stageId: any // 阶段
  amount: any // 复习数量
} & TaskConfig

export type TaskWordQuestConfig = {
  level: any // 入门、进阶
  amount: any // 满星关卡数
} & TaskConfig

export type TaskSentenceConfig = {
  level: any // 第几阶
  amount: any // 句子数量
} & TaskConfig

export type TaskFreeLearnConfig = {
  bookId: any // 教材id
  chapterIds: any // 章节id
  amount: any // 通关次数
} & TaskConfig

type Config = {
  gradeId: any
  subjectId: any
  chapters: any

  stageId: any // 阶段
  stageName: string
  versionId: any // 版本
  versionName: string
  bookName: string
  bookId: any // 教材

  levelName: string
}

export type Task = {
  id: any
  schoolId: string
  objTypeCode: string
  personCount: number
  completedPersonCount: number
  objTypeList: {
    id: any
    taskId: string
    objectId: string
  }[]
  startTime: string
  endTime: string
  delFlag: number
  priority: number
  createTime: string
  createUserId: string
  createUserName: string
  taskExtraRewardList: {
    id: any
    taskId: string
    amount: number
    startRank: number
    endRank: number
  }[]
  taskFreeLearnConfig: TaskFreeLearnConfig // null 使用 any 类型
  taskWordMasteryConfig: TaskWordTableConfig // null 使用 any 类型
  taskWordQuestConfig: TaskWordQuestConfig // null 使用 any 类型
  taskSentenceConfig: TaskSentenceConfig // null 使用 any 类型
  taskWordReviewConfig: TaskWordTableConfig
  topicTypeCode: string
  status: number
  target: string
  reward: number
  punish: number
  remark: string
  config: any
}

class TaskService extends BasicService<any> {
  protected saveOrUpdateUrl(): string {
    return '/supervisor/task'
  }

  protected removeUrl(): string {
    throw new Error('Method not implemented.')
  }

  protected findPageUrl(): string {
    return '/supervisor/task/list'
  }

  /**
   * 获取自己正在执行的任务
   */
  async getProcessingTasks(config?: any): Promise<any> {
    return await get('/supervisor/usertask/process/list', undefined, config)
  }

  async claimReward(data: any): Promise<any> {
    return await post('/supervisor/usertask/receive/reward', data)
  }

  /**
   *
   * @param 一键领取
   * @returns
   */
  async batchClaimReward(): Promise<any> {
    return await post('/supervisor/usertask/receive/rewards/all')
  }

  /**
   * 取消
   */
  async cancel(taskId: any) {
    await post(`/supervisor/task/cancel/${taskId}`)
  }

  /** 获得任务详情 */
  async getDetail(taskId: any): Promise<Task> {
    return await get(`/supervisor/task/get/${taskId}`)
  }

  // 获得额外奖励
  getExtraRewardList(task: Task) {
    const list = task.taskExtraRewardList
    if (!list?.length) return undefined
    list.sort((a: any, b: any) => {
      const res = a.startRank - b.startRank
      return res ? res : a.endRank - b.endRank
    })
    return list
  }

  // 获得书本的名称
  getBookName(task: Task) {
    let cfg = task.config as Config
    if (typeof cfg === 'string') {
      try {
        cfg = JSON.stringify(cfg) as any
      } catch {
        return
      }
    }
    if (!cfg) return
    if (cfg.bookName) {
      if (cfg.subjectId) {
        // 有学科
        return `[${getSubjectById(cfg.subjectId).name}]${cfg.bookName}`
      } else if (cfg.stageName) {
        // 有阶段
        return cfg.stageName + '-' + cfg.versionName + '-' + cfg.bookName
      } else {
        return cfg.bookName
      }
    } else if (cfg.stageName) {
      // 只有阶段
      return cfg.stageName
    }
  }
}

export const taskService = new TaskService()
