import str from '@/utils/str'
import { http } from './http'
import { get } from './request'
import { $XXL } from '@/utils'

export type Option = {
  value: any
  label: any
}
interface EnglishSubjectExpLevelVo {
  currentName: string
  currentLevelNum: number
  currentExp: number
  currentExpStart: number
  currentExpEnd: number
  nextName: string
  nextLevelNum: number
  nextExp: number
  currentLevelCode: null
}

interface VipUserInfo {
  vipType: number
  vipExp: number
  currentLevel: string
  currentLevelIntVal: number
  currentLevelStartExp: number
  nextLevelExp: number
  nextLevelName: string
  nextLevelIntVal: number
  vipStartTime: string
  vipEndTime: string
  vipLengthTime: string
}

interface PersonInUsePersonalities {
  createTime: string
  createUser: string
  updateTime: string
  updateUser: string
  id: any
  name: string
  resourceUrl: string
  personalInfoResourceUrl: string
  homePageResourceUrl: string
  introduction: string
  status: number
  productCode: string
  currencyNum: number
  showTag: number
  discount: null
  discountNum: null
  validityTime: number
  limitType: number
  sort: number
  remark: number
  delFlag: number
  defaultFlag: number
  usageFlag: number
  unlockId: string
  useStatus: number
  lockStatus: null
  expireTime: null
}

interface Medal {
  medalId: any
}

export interface UserData {
  userId: string
  schoolId: any
  account: string
  name: string
  nickName: string
  sex: number
  schoolName: string
  education: number
  className: string
  experience: number
  currency: string
  diamond: string
  levelPassNum: null
  personalImageNum: number
  englishSubjectExpLevelVo: EnglishSubjectExpLevelVo
  vipUserInfo: VipUserInfo
  avatarUrl: string
  avatarFrameUrl: null
  personInUsePersonalities: PersonInUsePersonalities[]
  nickNameChangeCardCount: number
  popNotices: any[]
  unCheckMedal: Medal[]
  learnGradeId: string
  learnTargetType: number
  weakSubjectIds: string
  isShowReading: number
  isShowAnimation: number
  classId: any
  smartUserIdentifier: string
  schoolPersonType: number
  lockFlag: number
  isTeacher: boolean
  // 是否为可以接受任务的学员
  isTaskStudent: boolean
  taskRewardStatus: any
  schoolClass: SchoolClass[]
}

type SchoolClass = {
  schoolName: any
  schoolId: any
  classList: {
    className: any
    classId: any
  }[]
}

// 获取用户红点提示
export type GetUserPointApi = {
  messageBox: boolean
  activity: boolean
  announcement: boolean
  config: boolean
  knapsack: boolean
  homeWork: boolean
}

const globalApi = {
  /**
   * 获取字典
   */
  getDictionaries: () => {
    return http.request({
      url: '/sysDictType/tree',
      headers: {
        Authorization: '',
      },
    })
  },

  /**
   * 获取用户信息
   */
  getUserInfoApi: async (headers?: anyObj): Promise<UserData> => {
    return (await get('/personal/getHomePage', undefined, {
      headers,
    })) as UserData
  },

  // 获取用户红点提示
  getUserPointApi: async (): Promise<GetUserPointApi> => {
    return await get('/personal/getRedPoint')
  },

  // 获取地区下拉列表
  getRegionSelector: () => {
    return http.request({
      url: '/sysUser/regionSelector',
    })
  },

  // 获取年级下拉列表
  getClassSelector: () => {
    return http.request({
      url: '/intrudeLevel/getSchoolGradeList',
    })
  },

  // 已读弹窗提醒
  closePopWin: (id: any) =>
    http.request({
      url: `/personal/closePopWin/${id}`,
    }),

  // 获取年级列表
  getGradeList: (params?: anyObj) =>
    http.request({
      url: '/grade/list',
      params,
    }),

  // 获取学科列表
  getSubjectListById: (id: any) =>
    http.request({
      url: `/subject/list/${id}`,
    }),

  // 生词本-标生标熟
  changeWordType: (data: anyObj) => {
    return http.request({
      url: `/word/operate`,
      data,
      method: 'POST',
    })
  },

  // 上传文件
  uploadFile: (data: any) =>
    http.request({
      url: '/sysFileInfo/tenUploadAll',
      method: 'post',
      timeout: 15000,
      headers: {
        'content-type': 'multipart/form-data',
      },
      data,
    }),

  // 用户视频表-id查询
  getUserVideoById: (id: any) =>
    http.request({
      url: `/userVideo/${id}`,
    }),

  // 获取静态资源地址
  getAssetsList: () =>
    http.request({
      url: '/listFrontStaticFiles',
      headers: {
        Authorization: '',
      },
    }),

  // 检查APK版本
  getVersions: async (params: anyObj) => {
    const url = location.href
    if (
      url.includes('localhost') ||
      url.includes('192.168.') ||
      url.includes('127.0.0.1') ||
      url.includes('lingxixue.com')
    ) {
      params = {
        magic: str.getMagic(),
        ...(params || {}),
      }
    }
    return get<any>('/apkManagement/list', params)
  },

  // 检查版本
  checkVersion: (params: anyObj) =>
    http.request({
      url: '/apkManagement/queryLatest',
      headers: {
        Authorization: '',
      },
      params,
    }),

  // 更新APPs
  updateApps: (data: anyObj) =>
    http.request({
      url: '/userAppRecord/updateApp',
      method: 'POST',
      data,
    }),

  // 获取未记录的APP
  updateUnRecordApp: (data: anyObj) =>
    http.request({
      url: '/appLogoRecord/filterUnLoadedLogo',
      method: 'POST',
      data,
    }),

  // 记录APP信息
  updateAppInfo: (data: anyObj) =>
    http.request({
      url: '/appLogoRecord/batchSave',
      method: 'POST',
      data,
    }),

  // 更新徽章
  updateMedal: (data: anyObj) =>
    http.request({
      url: '/userMedal/updateCheck',
      method: 'POST',
      data,
    }),
}

export default globalApi
