const Mock = require('mockjs')
const Random = Mock['Random']

const mockGeneration = 7 // primary
const mockGroups = [1, 2, 3, 4, 5, 6]
const mockPreferenceCount = 3 // 1 or 3 // 1志愿/3志愿

module.exports = [
  {
    url: '/mock/front/elective/generation/getElectiveStatus',
    type: 'get',
    response: config => {
      return {
        code: 200,
        msg: 'success',
        data: {
          year: 2021,
          roundId: 1,
          roundName: '第X次选科',
          groupIds: mockGroups.toString(),
          roundGroups: mockGroups.map(groupId => ({
            groupId: groupId,
            classCount: Random.integer(2, 6),
            personCount: function() {
              return this.classCount * 50
            },
            limitPerson: false,
            rankOut: false
          })),
          allMatched: true,
          currentGeneration: mockGeneration,

          // +
          disenrollCount: 0, // 未录人数
          enablePushNextDMGeneration: true,
          allowDMAlgorithm: true,
          doneDMAlgorithm: true,
          allowForce: true,
          enableForceAdjustEntry: true
        }
      }
    }
  },
  {
    url: '/mock/front/elective/generation/getElectiveSummary',
    type: 'get',
    response: config => {
      const results = []
      for (let generation = 1; generation <= mockGeneration; generation++) {
        const copyGeneration = generation
        switch (generation) {
          case 1:
            // primary
            const allPreferences = []
            for (let pref = 0; pref < mockPreferenceCount; pref++) {
              const preferences = []
              const groupDefines = [
                {
                  category: 'actualCount',
                  queryCode: Random.cname(),
                  displayName: '已报名',
                  detailName: '报名组合',
                  values: mockGroups.map(groupId => ({
                    groupId: groupId,
                    value: Random.integer(120, 400),
                    color: 'B',
                    bold: false,
                    star: false,
                    disabled: false
                  }))
                }
              ]
              const nonGroupDefines = [
                {
                  category: 'unfinishedCount',
                  queryCode: Random.cname(),
                  displayName: '未报名',
                  detailName: '',
                  values: [{
                    groupId: 0,
                    value: Random.integer(0, 10),
                    color: '',
                    bold: false,
                    star: false,
                    disabled: false
                  }]
                }
              ]
              preferences.push(...groupDefines)
              preferences.push(...nonGroupDefines)
              allPreferences.push(preferences)
            }
            const primary = {
              roundId: 1,
              generation: copyGeneration,
              categories: allPreferences
            }
            results.push(primary)
            break
          default:
            const accumulateDefine = {
              generations: [2, 4, 6, 7],
              factory: () => [
                {
                  category: 'approvedCount',
                  queryCode: Random.cname(),
                  displayName: '正常录取',
                  detailName: '录取组合',
                  values: mockGroups.map(groupId => ({
                    groupId: groupId,
                    value: Random.integer(120, 300),
                    color: '',
                    bold: false,
                    star: false,
                    disabled: false
                  }))
                },
                {
                  category: 'forcedCount',
                  queryCode: Random.cname(),
                  displayName: '调剂录取',
                  detailName: '调剂组合',
                  values: mockGroups.map(groupId => ({
                    groupId: groupId,
                    value: Random.integer(0, 10),
                    color: '',
                    bold: false,
                    star: false,
                    disabled: false
                  }))
                }
              ]
            }
            const commonDefines = [
              {
                generations: [2],
                factory: () => [
                  {
                    category: 'indicateCount',
                    displayName: '指标',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: true,
                      queryCode: 'abc',
                      disabled: false
                    }))
                  },
                  {
                    category: 'approvedCount',
                    queryCode: Random.cname(),
                    displayName: '录取人数',
                    detailName: '录取组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'adjustCount',
                    queryCode: Random.cname(),
                    displayName: '调剂人数',
                    detailName: '调剂组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'matchedCount',
                    queryCode: Random.cname(),
                    displayName: '专业符合',
                    detailName: '推荐组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'nonmatchedCount',
                    queryCode: Random.cname(),
                    displayName: '专业不符',
                    detailName: '推荐组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  }
                ]
              },
              {
                generations: [3, 5],
                factory: () => [
                  {
                    category: 'matchedApproved',
                    queryCode: Random.cname(),
                    displayName: '专业符合同意',
                    detailName: '报名组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'matchedNotOptional',
                    queryCode: Random.cname(),
                    displayName: '专业符合改选',
                    detailName: '报名组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'matchedRejected',
                    queryCode: Random.cname(),
                    displayName: '专业符合拒绝',
                    detailName: '拒绝组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'matchedNonaction',
                    queryCode: Random.cname(),
                    displayName: '专业符合未填',
                    detailName: '推荐组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'matchedRankout',
                    queryCode: Random.cname(),
                    displayName: '专业符合被挤出',
                    detailName: '报名组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'nonmatchedApproved',
                    queryCode: Random.cname(),
                    displayName: '专业符合已填',
                    detailName: '报名组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'nonmatchedRejected',
                    queryCode: Random.cname(),
                    displayName: '不可调剂拒绝',
                    detailName: '报名组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'nonmatchedNonaction',
                    queryCode: Random.cname(),
                    displayName: '不可调剂未填',
                    detailName: '报名组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  }
                ]
              },
              {
                generations: [4],
                factory: () => [
                  {
                    category: 'approvedCount',
                    queryCode: Random.cname(),
                    displayName: '补录录取',
                    detailName: '录取组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'forcedCount',
                    queryCode: Random.cname(),
                    displayName: '调剂录取',
                    detailName: '录取组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'matchedCount',
                    queryCode: Random.cname(),
                    displayName: '可调剂人数',
                    detailName: '调剂组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'nonmatchedCount',
                    queryCode: Random.cname(),
                    displayName: '不可调剂人数',
                    detailName: '报名组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  }
                ]
              },
              {
                generations: [6],
                factory: () => [
                  {
                    category: 'approvedCount',
                    queryCode: Random.cname(),
                    displayName: '补录录取',
                    detailName: '录取组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  },
                  {
                    category: 'forcedCount',
                    queryCode: Random.cname(),
                    displayName: '调剂录取',
                    detailName: '录取组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  }
                ]
              },
              {
                generations: [7],
                factory: () => [
                  {
                    category: 'forcedCount',
                    queryCode: Random.cname(),
                    displayName: '调剂录取',
                    detailName: '录取组合',
                    values: mockGroups.map(groupId => ({
                      groupId: groupId,
                      value: Random.integer(120, 400),
                      color: '',
                      bold: false,
                      star: false,
                      disabled: false
                    }))
                  }
                ]
              }
            ]
            const defines = commonDefines.find(d => d.generations.includes(generation))
            const inAccDef = accumulateDefine.generations.includes(generation)
            if (defines) {
              results.push({
                roundId: 1,
                generation: copyGeneration,
                categories: defines.factory(),
                accumulates: inAccDef ? accumulateDefine.factory() : undefined
              })
            }
            break
        }
      }

      return {
        code: 200,
        msg: 'success',
        data: results
      }
    }
  },
  {
    url: '/mock/front/elective/generation/getElectiveGenerationDetails',
    type: 'get',
    response: config => {
      return {
        code: 200,
        msg: 'success',
        total: Random.integer(5, 200),
        data: {
          groupDescriptors: mockGroups.map(groupId => {
            return {
              groupId,
              descriptors: [
                { key: 'setting', value: Random.integer(100, 9999), description: '设置人数' },
                { key: 'actual', value: Random.integer(0, 9999), description: '实报人数' }
              ]
            }
          }),
          'details|5-20': [
            {
              'id|+1': 1000,
              'roundId': 1,
              'studentId|+1': 100,
              'studentName': Random.cname(),
              'groupId': config.query.groupId,
              'groupName': Random.cname(),
              'classId|+1': 20,
              'className': Random.cname(),
              'datetime': Random.date(),
              'histories|4-15': [
                {
                  'generation|1': [1, 2, 3, 4, 5, 6, 7],
                  'groupId|1': mockGroups,
                  'description': Random.cname(),
                  'rankDescriptors|1-3': [
                    { key: Random.cname(), value: Random.integer(1, 300), description: Random.cname() }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  },
  {
    url: '/mock/front/elective/generation/getGenerationOptionalMajorsBatch',
    type: 'get',
    response: config => {
      let studentIds = config.query.studentIds
      if (typeof studentIds === 'string') studentIds = studentIds.split(',')
      const majorsMap = {}
      studentIds.forEach(id => {
        majorsMap[id] = {
          'bestMatchedGroupId|1': mockGroups,
          'majors|0-6': [{
            'collegeId|+1': 5000,
            'collegeName': Random.cname(),
            'majorCategoryCode': Random.cname(),
            'majorCategoryName': Random.cname(),
            'majors': {},
            'limitationA': '', // 选科限制1
            'limitationB': '', // 选科限制2
            'matchedGroupIds|0-3': mockGroups // 匹配的组合
          }]
        }
      })
      return {
        code: 200,
        msg: 'success',
        data: majorsMap
      }
    }
  }
]
