import { getBatchDynamicData } from '@/service/index/api'

const TYPE_LABELS = {
  c: '冲',
  w: '稳',
  b: '保',
} as const

type WishTypeKey = keyof typeof TYPE_LABELS

const TYPE_STYLE_MAP = {
  2: {
    text: '冲',
    color: '#EB5241',
    bgColor: 'rgba(235,82,65,0.15)',
    roundedBgColor: '#E75859',
  },
  1: {
    text: '稳',
    color: '#FA8E23',
    bgColor: 'rgba(250,142,35,0.15)',
    roundedBgColor: '#FF8800',
  },
  0: {
    text: '保',
    color: '#15C496',
    bgColor: 'rgba(21,196,150,0.15)',
    roundedBgColor: '#34C724',
  },
} as const

const DEFAULT_TYPE_STYLE = TYPE_STYLE_MAP[0]

export const calcTypeName = (type: number) => {
  const style = TYPE_STYLE_MAP[type as keyof typeof TYPE_STYLE_MAP] ?? DEFAULT_TYPE_STYLE

  return {
    text: style.text,
    style: {
      color: style.color,
      backgroundColor: style.bgColor,
    },
    roundedBgColor: style.roundedBgColor,
  }
}
export const coverTypeModel = (tModel: Record<WishTypeKey, number>, total: number) => {
  const _result = (Object.keys(TYPE_LABELS) as WishTypeKey[]).map((key) => ({
    name: `${TYPE_LABELS[key]}(${tModel[key]})`,
    value: key === 'b' ? '0' : key === 'w' ? '1' : '2',
  }))

  _result.unshift({
    name: `全部${total}`,
    value: '-1',
  })
  return _result
}

export const countModel = (list: any[]) => {
  const tModel = ref([
    { type: 2, count: 0 },
    { type: 1, count: 0 },
    { type: 0, count: 0 },
  ])

  list.forEach((item) => {
    item.vItems.forEach((vItem: any) => {
      const target = tModel.value.find((t) => t.type === vItem.type)
      if (target) target.count++
    })
  })

  return { tModel }
}

export const useScore = (
  provinceCode: string,
  batchName: string,
  requireSubject: any[],
  expectedScore: number,
) => {
  const score = ref(0)
  const minScore = ref(0)
  const maxScore = ref(0)
  const sliderValue = ref([0, 0])
  const _requireSubject = requireSubject.length > 0 ? requireSubject[0].name : ''
  getBatchDynamicData({ LocationCode: provinceCode, Course: _requireSubject }).then((resp) => {
    if (resp.code === 200) {
      const _result = resp.result as { batches: any[]; maxScore: number; minScore: number }
      if (_result.batches.length > 0) {
        const _score = _result.batches.find((item) => item.batch === batchName)?.score || 0
        score.value = _score
      }

      minScore.value = _result.minScore
      maxScore.value = _result.maxScore
      if (maxScore.value !== 0) {
        sliderValue.value[0] =
          expectedScore < minScore.value + 40 ? minScore.value : expectedScore - 40

        sliderValue.value[1] =
          expectedScore > maxScore.value - 40 ? maxScore.value : expectedScore + 40
      }
    }
  })
  return { score, minScore, maxScore, sliderValue }
}
