import { Service } from './Service'
import { NFT_CREATE } from '@/types/Dto'
import { token } from '@/util/userInfoUtils'
export class NFTService extends Service {
  router = {
    create: { path: '/nft/publish' },
    getMyList: {
      path: '/nft/list/current',
      dataType: 'application/x-www-form-urlencoded',
    },
    getList: {
      path: '/nft/list',
      dataType: 'application/x-www-form-urlencoded',
    },
    genId: {
      path: '/nft/generateNftId',
      dataType: 'application/x-www-form-urlencoded',
    },
    detail: {
      path: '/nft/get/{id}',
      dataType: 'application/x-www-form-urlencoded',
    },
    themes: {
      path: '/label/list',
      dataType: 'application/x-www-form-urlencoded',
    },
    getMd5: { path: '/nft/file/md5', dataType: 'multipart/form-data' },
    save: { path: '/nft/save', dataType: 'multipart/form-data' },
    publish: { path: '/nft/publish', dataType: 'application/json' },
    getCategory: {
      path: '/category/list',
      dataType: 'application/x-www-form-urlencoded',
    },
  }
  auth = 'Bearer ' + token.getToken()
  constructor() {
    super()
  }

  /**
   * 获取验证码
   * @param phone
   * @param codeType  短信模板, 1:登录短信 2:修改密码 3:修改手机号
   */
  async create(data: NFT_CREATE) {
    return await this.service.post(this.router.create.path, data, {
      headers: {
        Authorization: this.auth,
      },
    })
  }

  /**
   *
   * @returns 获取剧目主题表
   */
  async getThemeList() {
    return await this.service.get(this.router.themes.path, {
      headers: {
        Authorization: this.auth,
        'Content-Type': this.router.themes.dataType,
      },
    })
  }
  async getCategory(): Promise<any[]> {
    return await this.service.get(this.router.getCategory.path)
  }

  /**
   * 获取我的NFT列表
   * @param categoryId
   * @returns
   */
  async getMyList(categoryId?: number) {
    return await this.service.get(this.router.getMyList.path, {
      headers: {
        Authorization: this.auth,
        'Content-Type': this.router.getMyList.dataType,
      },
      params: { categoryId: categoryId ? categoryId : null },
    })
  }

  /**
   * 获取所有NFT的列表
   * @param pageNum
   * @param pageSize
   * @param categoryId
   * @returns
   */
  async getList(pageNum?: number, pageSize?: number, categoryId?: number) {
    return await this.service.get(this.router.getList.path, {
      headers: {
        Authorization: this.auth,
        'Content-Type': this.router.getList.dataType,
      },
      params: {
        categoryId: categoryId ? categoryId : null,
        pageNum: pageNum,
        pageSize: pageSize,
      },
    })
  }

  /**
   * 生成id
   * @param categoryId
   * @returns
   */
  async generateNftId(categoryId: number) {
    return await this.service.get(this.router.genId.path, {
      headers: {
        Authorization: this.auth,
        'Content-Type': this.router.genId.dataType,
      },
      params: { categoryId: categoryId },
    })
  }

  /**
   * 按照id查找nft详情
   * @param id
   */
  async detail(id: number) {
    return await this.service.get(this.router.detail.path, {
      headers: {
        Authorization: this.auth,
        'Content-Type': this.router.detail.dataType,
      },
      params: { id: id },
    })
  }

  /**
   * 获取md5
   * @param id
   */
  async getMd5(file: File) {
    const fd = new FormData()
    fd.append('file', file)
    return await this.service.post(this.router.getMd5.path, fd, {
      headers: {
        Authorization: this.auth,
        'Content-Type': this.router.getMd5.dataType,
      },
    })
  }

  /**
   * 发布
   * @param id
   */
  async publish(obj: {
    fileHash: string
    id: number
    nftId: string
    wallet: string
  }) {
    return await this.service.post(this.router.publish.path, obj, {
      headers: {
        Authorization: this.auth,
        'Content-Type': this.router.publish.dataType,
      },
    })
  }

  /**
   * nft基本信息保存
   * @param id
   */

  async save(
    author: string,
    categoryId: number,
    fileHash: string,
    isArchives: number,
    name: string,
    synopsis: string,
    theme: string,
    file: File,
    isGrant: number,
  ) {
    const fd = new FormData()
    fd.append('author', author)
    fd.append('categoryId', categoryId.toString())
    fd.append('fileHash', fileHash)
    fd.append('isArchives', isArchives.toString())
    fd.append('name', name)
    fd.append('synopsis', synopsis)
    fd.append('theme', theme)
    fd.append('file', file)
    fd.append('isGrant', String(isGrant))

    return (await this.service.post(this.router.save.path, fd, {
      headers: {
        Authorization: this.auth,
        'Content-Type': this.router.save.dataType,
      },
    })) as {
      fileHash: string
      id: number
      nftId: string
      wallet: string
    }
  }
}
