import { post } from '@/utils/request'

/**
 * 创建试卷
 * @param data
 */
export function createPaper(data) {
  return post('/exam/api/paper/paper/create-paper', data)
}

/**
 * 试卷详情
 * @param data
 */
export function paperDetail(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        data: {
          id: data.id || '1',
          title: '模拟考试试卷',
          totalScore: 100,
          totalTime: 120,
          leftSeconds: 7200,
          // 单选题
          radioList: [
            {
              id: '1001',
              quId: '101',
              sort: 0,
              answered: false,
              title: '单选题1'
            },
            {
              id: '1002',
              quId: '102',
              sort: 1,
              answered: false,
              title: '单选题2'
            }
          ],
          // 多选题
          multiList: [
            {
              id: '2001',
              quId: '201',
              sort: 2,
              answered: false,
              title: '多选题1'
            }
          ],
          // 判断题
          judgeList: [
            {
              id: '3001',
              quId: '301',
              sort: 3,
              answered: false,
              title: '判断题1'
            }
          ],
          // 编程题
          programmingList: [
            {
              id: '101',
              quId: '1',
              sort: 4,
              answered: false,
              title: '两数之和'
            },
            {
              id: '102',
              quId: '2',
              sort: 5,
              answered: false,
              title: '反转链表'
            }
          ]
        }
      })
    }, 500)
  })
}

/**
 * 题目详情
 * @param data
 */
export function quDetail(params) {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 如果是编程题
      if (params.quId === '1' || params.quId === '2' || params.quId === '3') {
        resolve({
          code: 200,
          data: {
            quId: params.quId,
            quType: 4, // 编程题类型
            content: params.quId === '1' ? '给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出和为目标值 target 的那两个整数，并返回它们的数组下标。' : '给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。',
            inputDesc: '第一行输入一个整数数组 nums，以空格分隔\n第二行输入一个整数 target',
            outputDesc: '输出两个整数，表示和为 target 的两个数的下标',
            sampleInput: '[2, 7, 11, 15]\n9',
            sampleOutput: '0 1',
            timeLimit: 1000,
            memoryLimit: 256,
            sort: parseInt(params.quId) - 1,
            answered: false,
            answerList: [] // 编程题没有选项
          }
        })
        return
      }
      
      // 单选题
      if (params.quId.startsWith('10')) {
        resolve({
          code: 200,
          data: {
            quId: params.quId,
            quType: 1, // 单选题
            content: '这是一道单选题',
            sort: parseInt(params.quId.substring(2)) - 1,
            answered: false,
            answerList: [
              { id: 'A' + params.quId, abc: 'A', content: '选项A', checked: false },
              { id: 'B' + params.quId, abc: 'B', content: '选项B', checked: false },
              { id: 'C' + params.quId, abc: 'C', content: '选项C', checked: false },
              { id: 'D' + params.quId, abc: 'D', content: '选项D', checked: false }
            ]
          }
        })
        return
      }
      
      // 多选题
      if (params.quId.startsWith('20')) {
        resolve({
          code: 200,
          data: {
            quId: params.quId,
            quType: 2, // 多选题
            content: '这是一道多选题',
            sort: parseInt(params.quId.substring(2)) - 1 + 2,
            answered: false,
            answerList: [
              { id: 'A' + params.quId, abc: 'A', content: '选项A', checked: false },
              { id: 'B' + params.quId, abc: 'B', content: '选项B', checked: false },
              { id: 'C' + params.quId, abc: 'C', content: '选项C', checked: false },
              { id: 'D' + params.quId, abc: 'D', content: '选项D', checked: false }
            ]
          }
        })
        return
      }
      
      // 判断题
      if (params.quId.startsWith('30')) {
        resolve({
          code: 200,
          data: {
            quId: params.quId,
            quType: 3, // 判断题
            content: '这是一道判断题',
            sort: parseInt(params.quId.substring(2)) - 1 + 3,
            answered: false,
            answerList: [
              { id: 'A' + params.quId, abc: 'A', content: '正确', checked: false },
              { id: 'B' + params.quId, abc: 'B', content: '错误', checked: false }
            ]
          }
        })
        return
      }
      
      // 默认返回
      resolve({
        code: 200,
        data: {
          quId: params.quId,
          quType: 1,
          content: '未知题型',
          sort: 0,
          answered: false,
          answerList: []
        }
      })
    }, 500)
  })
}

/**
 * 填充答案
 * @param data
 */
export function fillAnswer(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('填充答案:', data)
      resolve({
        code: 200,
        data: true,
        msg: 'success'
      })
    }, 300)
  })
}

/**
 * 交卷
 * @param data
 */
export function handExam(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('交卷:', data)
      resolve({
        code: 200,
        data: true,
        msg: 'success'
      })
    }, 500)
  })
}

/**
 * 试卷详情
 * @param data
 */
export function paperResult(data) {
  return post('/exam/api/paper/paper/paper-result', data)
}

/**
 * 错题训练
 * @param data
 */
export function training(data) {
  return post('/exam/api/paper/paper/training', data)
}


/**
 * 检查是否有进行中的考试
 * @returns {*}
 */
export function checkProcess() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        data: null, // 没有进行中的考试
        msg: 'success'
      })
    }, 300)
  })
}
