import num from '@/utils/num'
import { px2px, secondsToHours } from '@/utils/tool'

// 未分组的名称
export const UNGROUP_NAME = '其他'

// 总计
export const P_TYPE_TOTAL = 1
// 每日
export const P_TYPE_DAILY = 2
// 个人
export const P_GROUP_PERSONAL = 1
// 小组
export const P_GROUP_GROUP = 2

// 处理数值
export const processNumberValue = function (values: any[], column: any, len: number) {
  let ret = ''
  if (!values.every(value => Number.isNaN(value))) {
    ret = num.toFixed2(
      values.reduce((prev, curr) => {
        const value = Number(curr)
        if (!Number.isNaN(value)) {
          return prev + curr
        } else {
          return prev
        }
      }, 0) / len,
    )
    if (column.property === 'learnTime' && ret !== '0') {
      ret = secondsToHours(Number(ret))
    }
  } else {
    ret = ''
  }
  return ret
}

function timeValueFn(time: any) {
  return time && secondsToHours(time)
}

function makeCellWithAverage(props: any, valueFn: any = undefined) {
  const prop = props.prop
  return {
    ...props,
    render: (_: any, scope: any) => {
      let val = scope.row[prop]
      if (valueFn) {
        val = valueFn(val)
      }
      if (scope.row.average && scope.row[prop] < scope.row.average[prop]) {
        // 低于平均值的，用特殊颜色显示
        return <span style='color:#ef8933;'>{val}</span>
      } else {
        return val
      }
    },
  }
}

const wordStrangerCount = {
  label: '陌生',
  prop: 'wordStrangerCount',
}

const name = {
  label: '姓名',
  prop: 'name',
  width: px2px(150),
  noAverage: true,
}

const starCount = {
  label: '星星',
  prop: 'starCount',
  sortable: true,
}

const wordKnowCount = {
  label: '认识',
  prop: 'wordKnowCount',
}

const wordKnowWellCount = {
  label: '熟悉',
  prop: 'wordKnowWellCount',
}

const wordGraspCount = makeCellWithAverage({
  label: '掌握',
  prop: 'wordGraspCount',
  sortable: true,
})

// const rightRate = makeCellWithAverage(
//   {
//     label: '正确率',
//     prop: 'rightRate',
//     sortable: true,
//   },
//   (val: any) => val + '%',
// )

const wordCount = makeCellWithAverage({
  label: '总数',
  prop: 'wordCount',
  sortable: true,
})

const newWordCount = makeCellWithAverage({
  label: '新词数',
  prop: 'newWordCount',
})

// const wordReviewCount = {
//     label: '复习数',
//     prop: 'wordReviewCount',
//     render: (h, scope) => {
//         return scope.row.wordCount - scope.row.newWordCount
//     }
// }

const sentenceCount = makeCellWithAverage({
  label: '句子',
  prop: 'sentenceCount',
  sortable: true,
})

const knowledgeCount = makeCellWithAverage({
  label: '知识点',
  prop: 'knowledgeCount',
  sortable: true,
})

const learnTime = makeCellWithAverage(
  {
    label: '有效时长',
    prop: 'learnTime',
    width: px2px(180),
    sortable: true,
  },
  timeValueFn,
)

// 英语（每日）
export const englishDailyColumn = function (event: { [key: string]: any }): any[] {
  const cols = [
    name,
    learnTime,
    wordStrangerCount,
    wordKnowCount,
    wordKnowWellCount,
    wordGraspCount,
    newWordCount,
    wordCount,
    starCount,
    sentenceCount,
    // knowledgeCount,
  ]
  if (event.group === P_GROUP_PERSONAL) {
    cols.unshift({
      label: '序号',
      type: 'index',
    })
  }
  return cols
}

// 英语（总计）
export const englishTotalColumn = function (event: { [key: string]: any }): any[] {
  const cols = [
    name,
    learnTime,
    wordStrangerCount,
    wordKnowCount,
    wordKnowWellCount,
    wordGraspCount,
    wordCount,
    starCount,
    // rightRate,
    sentenceCount,
    // knowledgeCount,
  ]
  if (event.group === P_GROUP_PERSONAL) {
    cols.unshift({
      label: '序号',
      type: 'index',
    })
  }
  return cols
}

// 综合排名（每日、总计）
export const rankColumn = function (event: { [key: string]: any }): any[] {
  const cols = [
    {
      label: '姓名',
      prop: 'name',
      noAverage: true,
    },
    {
      label: '有效时长',
      prop: 'learnTime',
      sortable: true,
      render: (_: any, scope: any) => timeValueFn(scope.row.learnTime),
    },
    // {
    //   label: '单词掌握',
    //   prop: 'wordGraspCount',
    //   sortable: true,
    // },
    // {
    //   label: '单词总数',
    //   prop: 'wordCount',
    //   sortable: true,
    // },
    // {
    //   label: '句子',
    //   prop: 'sentenceLearnCount',
    //   sortable: true,
    // },
    {
      label: '知识点',
      prop: 'knowledgeCount',
      sortable: true,
    },
    {
      label: '错题',
      prop: 'errorCount',
      sortable: true,
    },
    {
      label: '订正',
      prop: 'correctCount',
      sortable: true,
    },
    {
      label: '乐贝',
      prop: 'currency',
      sortable: true,
    },
  ]
  if (event.group === P_GROUP_PERSONAL) {
    cols.unshift({
      label: '序号',
      type: 'index',
    } as any)
  }
  return cols
}

// 学科（每日、总计）
export const subjectColumn = function (event: { [key: string]: any }): any[] {
  const cols = [
    {
      label: '姓名',
      prop: 'name',
      noAverage: true,
    },
    {
      label: '有效时长',
      prop: 'learnTime',
      sortable: true,
      render: (_: any, scope: any) => timeValueFn(scope.row.learnTime),
    },
    {
      label: '答题正确',
      prop: 'successTestCount',
      sortable: true,
    },
    {
      label: '答题错误',
      prop: 'errorTestCount',
      sortable: true,
    },
    {
      label: '知识点总数',
      prop: 'allKnowledgeCount',
      sortable: true,
    },
    {
      label: '知识点掌握',
      prop: 'allKnowledgeGraspCount',
      sortable: true,
    },
    {
      label: '订正总数',
      prop: 'allReviewCount',
      sortable: true,
    },
    {
      label: '订正正确',
      prop: 'successReviewCount',
      sortable: true,
    },
  ]
  if (event.group === P_GROUP_PERSONAL) {
    cols.unshift({
      label: '序号',
      type: 'index',
    } as any)
  }
  return cols
}
