// 我的音色库&形象库
import {
  bluebookAiAgent,
} from '@/xiaolanbenlib/api/index'
import request from '@/xiaolanbenlib/module/axios.js'
import Taro from '@tarojs/taro'

import {FileTypes} from '@/consts'


export type TAvatarItem = {
  avatarId: number,
  avatarLogo: string,
  avatarUrl: string,
  isOriginal: boolean
  canDel: boolean
  isVideo: boolean
  videoSeconds: number
}
// 上传形象--并生成微视频
export type TUploadAvatarResponse = {
  data: TAvatarItem[],
  message: string, // 任务状态描述 失败原因 ,
  status: string, // 任务状态 processing 处理中/ success 处理成功/ process_fail 处理失败/not_found 未找到 ,
  userId: string,
  taskId: string
}
export const uploadAvatar = (data: {
  "aiGenerated": boolean,
  "avatarUrl": string,
  "description": string
}) => {
  return request.post<TUploadAvatarResponse>(`${bluebookAiAgent}api/v1/my/avatar`, data)
}

// 根据指定形象归生成微视频
export const genAvatarVideo = (data: {
  "avatarUrl": string,
}) => {
  return request.post<TUploadAvatarResponse>(`${bluebookAiAgent}api/v1/my/avatar/4video`, data)
}

// 获取上传形象任务的处理状态
export const getUploadedAvatarStatus = (taskId: string|number) => {
  return request.get<TUploadAvatarResponse>(`${bluebookAiAgent}api/v1/my/avatar/task/${taskId}`)
}

// 获取个人形象库
export const fetchMyAvatars = ({pageSize = 20, pageIndex = 1}:{pageSize?: number, pageIndex?: number}) => {
  return request.get<{
    pageIndex: number
    pageSize: number
    totalCount: number
    data: TAvatarItem[]
  }>(`${bluebookAiAgent}api/v1/my/avatars`, {params: {
    pageSize,
    pageIndex
  }})
}


// 删除指定的形象记录
export const deleteAvatar = (avatarId: string|number) => {
  return request.delete(`${bluebookAiAgent}api/v1/my/avatar/${avatarId}`)
}

// 用户上传文件--返回{'fileId':'xxxx', 'publicUrl':'xxxx'}
// /blue-aiagent/api/v1/my/upload

export const uploadFilesFromWechat = ()=> {
  Taro.chooseMessageFile({
    count: 10,
    type: "file",
    extension: FileTypes.map((f) => `${f}`),
    success(res) {
      const tempFilePaths = res.tempFiles;
      console.log("tempFilePaths", tempFilePaths);
      const files = tempFilePaths.map((file) => {
        return {
          file,
          status: 0,
          fileProgress: {
            progress: 0,
            totalBytesSent: 0,
            totalBytesExpectedToSend: 0,
          },
        };
      });
      // setUploadingList(files);
      // setUploadList(files);
      console.log(files);
    },
  });
}


// 前端获取自主上传文件至存贮的安全令牌
export type TUploadSecurityToken = {
  "accessKeyId": string,
  "accessKeySecret": string,
  "expiration": string,
  "fullPath": string,
  "publicUrl": string,
  "securityToken": string,
  "statusCode": string
}

export const getUploadSecurityToken = () => {
  return request.get<TUploadSecurityToken>(`${bluebookAiAgent}api/v1/my/upload/securityToken`)
}




