import type { FindPageResponse } from '../basic'

import type { Question } from '#/components/question-item'

import { useUserStore } from '@vben/stores'

import { get, post } from '#/api/http'
import obj from '#/utils/obj'
import str from '#/utils/str'

import { BasicService } from '../basic'

class QuestionService extends BasicService {
  canSubmit(question: any, userId: any) {
    if (!userId) {
      const { userInfo } = storeToRefs(useUserStore())
      userId = userInfo.value.id
    }
    const status = question.status
    if (status === status_draft || status === status_approving) {
      // 保存草稿、提交审核
      if (question.creatorId !== userId) {
        return '您不是本题的创建者，不能进行【保存草稿】、【提交审核】操作！'
      }
    } else {
      if (question.creatorId === userId) {
        return '您是本题的【创建者】，不能进行【审核】、【复核】操作！'
      }
      if (status === status_reviewed && question.approverId === userId) {
        return '您已是本题的【审核人】，不能进行【复核】操作！'
      }
      if (status === status_reviewing && question.reviewerId === userId) {
        return '您已是本题的【复核人】，不能进行【审核】操作！'
      }
    }
    return false
  }

  clean(question: Question) {
    if (typeof question.configJson === 'string') {
      question.configJson = JSON.parse(question.configJson)
    }
    question.options = str.toList(question.options)
    question.answer = str.toList(question.answer)
  }

  // 分页查询
  override async findPage(
    current: number,
    size: number,
    condition: any,
  ): Promise<FindPageResponse> {
    const data = (await super.findPage(current, size, condition)) as any
    const rows = data.pageResult.rows || []
    for (const item of rows) {
      this.clean(item)
    }
    return data
  }

  async get(id: any) {
    const question = await get<any>(`/question/get/${id}`)
    questionService.clean(question)
    return question
  }

  isReadonly(question: any, userId: any) {
    if (!userId) {
      const { userInfo } = storeToRefs(useUserStore())
      userId = userInfo.value.id
    }
    const status = question.status
    if (status === status_reviewed) return true
    if (status === status_draft || status === status_rejected) {
      return question.creatorId !== userId
    } else {
      if (question.creatorId === userId) {
        return true
      }
      if (status === status_reviewing && question.approverId === userId) {
        return true
      }
      if (status === status_approving && question.reviewerId === userId) {
        return true
      }
    }
    return false
  }

  async updateStatus(ids: any, status: any) {
    if (ids && !Array.isArray(ids)) {
      ids = [ids]
    }
    return await post<any>('question/updateStatus', { ids, status })
  }

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

  protected removeUrl(): string {
    return '/question/delete'
  }

  protected saveOrUpdateUrl(): string {
    return '/question/saveOrUpdate'
  }
}

export const questionService = new QuestionService()

export const difficulty_options = [
  { label: '容易', value: '11' },
  { label: '较易', value: '12' },
  { label: '适中', value: '13' },
  { label: '较难', value: '14' },
  { label: '困难', value: '15' },
]
export const difficulty_map = obj.options2map(difficulty_options)

// // 选择题
// export const type_single = '1'
// // 填空题
// export const type_fill = '2'
// // 多选题
// export const type_multiple = '3'
// // 判断题
// export const type_judge = '4'

// export const type_options = [
//   { label: '单选题', value: type_single },
//   { label: '多选题', value: type_multiple },
//   { label: '判断题', value: type_judge },
//   { label: '填空题', value: type_fill },
//   { label: '应用题', value: type_essay },
// ]
// export const type_map = obj.options2map(type_options)

export const status_draft = '1'
export const status_approving = '2'
export const status_reviewing = '3'
export const status_reviewed = '4'
export const status_rejected = '5'
export const status_options = [
  { label: '草稿', value: status_draft },
  { label: '待审核', value: status_approving },
  { label: '待复核', value: status_reviewing },
  { label: '已通过', value: status_reviewed },
  { label: '被拒绝', value: status_rejected },
]
export const status_map = obj.options2map(status_options)

/**
 * 答案
 * 1、如果是判断题，是0，0是错误，1是正确
 * 2、如果是单选题，是1，1是正确选项的索引
 * 3、如果是多选题，是[0,3]，0和3是正确选项的索引
 * 4、如果是填空题，是['11','kk','tt']，代表有3个填空，分别是11、kk、tt
 * 5、如果是应用题，是富文本字符串
 */
