import {
  iGetList,
  iGetItem,
  iGetArticleNum,
  iUpdateItem,
  iAddItem,
  iChangeReleaseStatus,
  iChangeSelectReleaseStatus,
  iDeleteArticle,
  iDeleteArticles,
  iGetCollectList,
  iGetPvList,
  iGetStastics
} from '@/api/articleFix'

import listResult from '@/model/listResult'

import MODULETYPE from '@/const/moduleType'
import CARRIERKEY from '@/const/carrierKey'

import { translateLabel } from '@/utils'

const getList = async data => listResult(await iGetList(data), x => ({
  id: x.id,
  moduleType: translateLabel(MODULETYPE.ARTICLE_ITEM_MODAL_TYPE, x.moduleType),
  title: x.title,
  usedNum: translateLabel(CARRIERKEY.CARRIER, x.contentType),
  doctorName: x.doctorName,
  timeArr: x.startTime ? `${x.startTime}~${x.endTime}` : `-`,
  startTime: x.startTime,
  endTime: x.endTime,
  likeNum: x.likeNum,
  collectNum: x.collectNum,
  level: x.level,
  score: x.score,
  isRelease: x.isRelease,
  isReleaseF: x.isRelease ? '已发布' : '未发布',
  isFeature: x.isFeature,
  isFeatureF: x.isFeature ? '设为精选' : '未设精选',
  headUrl: x.coverUrl ? `${process.env.VUE_APP_BASE_IMG}${x.coverUrl}` : '',
  sort: x.sort,
  contentType: x.contentType
}))

const getCollectList = async data => listResult(await iGetCollectList(data), x => ({
  title: x.articleTitle,
  nickName: x.nickname,
  createDate: x.createDate,
  status: x.status,
  statusF: x.status ? '有效' : '无效'
}))

const getPvList = async data => listResult(await iGetPvList(data), x => ({
  title: x.articleTitle,
  nickName: x.nickname,
  createDate: x.createDate
}))

const getStastics = async params => {
  const result = await iGetStastics(params)
  if (result.success) {
    const data = result.data.filter(x => (!params.data.contentType || x.contentType === params.data.contentType))
    result.data = {
      titles: data.map(x => x.articleTitle),
      pvs: data.map(x => x.pv),
      collects: data.map(x => x.collect)
    }
  }
  return result
}

const getItem = async params => {
  const result = await iGetItem(params)
  if (result.success) {
    const data = result.data
    result.data = {
      id: data.id,
      moduleType: data.moduleType,
      title: data.title,
      carrier: data.contentType,
      info: {
        shareUrl: data.shareUrl ? [{ url: `${process.env.VUE_APP_BASE_IMG}${data.shareUrl}` }] : [],
        text: data.contentText,
        doctorId: data.doctorId,
        videoText: data.videoText,
        videoUrl: data.videoUrl,
        coverUrl: data.coverUrl ? [{ url: `${process.env.VUE_APP_BASE_IMG}${data.coverUrl}` }] : [],
        audioText: data.audioText,
        audioUrl: data.audioUrl,
        qaList: data.qaList ? data.qaList : [],
        playTime: data.playTime
      },
      isRelease: data.isRelease,
      isFeature: data.isFeature,
      likeNum: data.likeNum,
      collectNum: data.collectNum,
      playNum: data.playNum,
      score: data.score,
      level: data.level,
      tags: JSON.parse(JSON.stringify(data.tags)),
      timeArr: data.startTime === null ? '' : [data.startTime, data.endTime],
      sort: data.sort,
      contentSources: data.contentSources
    }
  }
  return result
}

const getArticleNum = async params => await iGetArticleNum(params)

const saveItem = async({ id, ...params }) => {
  if (id) {
    return await iUpdateItem(id, params)
  } else {
    return await iAddItem(params)
  }
}

const changeReleaseStatus = async params => await iChangeReleaseStatus(params)

const changeSelectReleaseStatus = async params => await iChangeSelectReleaseStatus(params)

const deleteArticle = async params => await iDeleteArticle(params)

const deleteArticles = async params => await iDeleteArticles(params)

export {
  getList,
  getItem,
  getArticleNum,
  saveItem,
  changeReleaseStatus,
  changeSelectReleaseStatus,
  deleteArticle,
  deleteArticles,
  getCollectList,
  getPvList,
  getStastics
}
