import { Service } from '../Service'

import { token } from '@/util/userInfoUtils'
import { iSaveData } from './types'
import { NftTransferDTO } from '@/types/Dto'
export class NFTService extends Service {
  router = {
    getMyList: {
      path: '/proxyApi/nft/list/current',
      dataType: 'application/x-www-form-urlencoded',
    },
    getList: {
      path: '/proxyApi/nft/list',
      dataType: 'application/x-www-form-urlencoded',
    },
    genId: {
      path: '/proxyApi/nft/generateNftId',
      dataType: 'application/x-www-form-urlencoded',
    },
    detail: {
      path: '/proxyApi/nft/get',
      dataType: 'application/x-www-form-urlencoded',
    },
    themes: {
      path: '/proxyApi/label/list',
      dataType: 'application/x-www-form-urlencoded',
    },
    ossPolicy: {
      path: '/proxyApi/oss/policy',
      dataType: 'application/x-www-form-urlencoded',
    },
    fileUpload: {
      path: '/proxyApi/oss/upload',
      dataType: 'multipart/form-data'
    },
    getMd5: { path: '/proxyApi/nft/file/md5', dataType: 'multipart/form-data' },
    save: { path: '/proxyApi/nft/save', dataType: 'multipart/form-data' },
    publish: { path: '/proxyApi/nft/publish', dataType: 'application/json' },
    getCategory: {
      path: '/proxyApi/category/list',
      dataType: 'application/x-www-form-urlencoded',
    },
    editCollection: {
      path: '/proxyApi/collection/add',
      dataType: 'application/x-www-form-urlencoded',
    },
    getCollectionList: {
      path: '/proxyApi/collection/list',
      dataType: 'application/x-www-form-urlencoded',
    },
    officialNFTList: {
      path: '/proxyApi/commemorate/nft/list',
      dataType: 'application/x-www-form-urlencoded'
    },
    officialNFTDetail:{
      path:'/proxyApi/commemorate/nft/get/',
      dataType: 'application/x-www-form-urlencoded'
    },
    applyOfficialNft:{
      path:'/proxyApi/commemorate/nft/receive',
      dataType: 'application/x-www-form-urlencoded'
    },
    nftTransfer:{
      path:'/proxyApi/nft/transfer'
    },
    sendTransferCode:{
      path:'/proxyApi/verificationCode/send/Sms/transferNft',
      dataType: 'application/x-www-form-urlencoded'
    },
    getCertificateData:{
      path:'/proxyApi/nft/certificate',
      dataType: 'application/x-www-form-urlencoded'
    },
    downloadNFTFile:{
      path:"/proxyApi/nft/download",
      dataType: 'application/x-www-form-urlencoded'
    },
    downloadCerfificate:{
      path:"/proxyDowload/",
      dataType: 'application/json;charset=utf-8'
    },
    getTransferList:{
      path:"/proxyApi/nft/transfer/record/list/",
      dataType: 'application/json;charset=utf-8'
    },
    getTransferDetail:{
      path:"/proxyApi/nft/transfer/record/detail/",
      dataType: 'application/json;charset=utf-8'
    },
    certificateByIdcard:{
      path:"/proxyApi/user/auth/person",
      dataType: 'multipart/form-data'
    }
  }
  constructor() {
    super()
  }

  private getAuth() {
    return 'Bearer ' + token.getToken()
  }

  /**
   * 获取NFT的详细信息
   * @param id
   * @returns
   */
  async getNFTdetail(id: number): Promise<any> {
    return await this.service.get(this.router.detail.path + '/' + id, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.detail.dataType,
      },
    })
  }

  /**
   * 操作用户的收藏夹
   * @param id
   * @returns
   */
  async editCollection(id: number) {
    return await this.service.post(
      this.router.editCollection.path,
      {},
      {
        headers: {
          Authorization: this.getAuth(),
          'Content-Type': this.router.editCollection.dataType,
        },
        params: {
          id: id,
        },
      },
    )
  }

  async getCollection(): Promise<any[]> {
    return await this.service.get(this.router.getCollectionList.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.editCollection.dataType,
      },
    })
  }

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

  /**
   * 获取我的NFT列表
   * @param categoryId
   * @returns
   */
  async getMyList(categoryId?: number): Promise<any> {
    return await this.service.get(this.router.getMyList.path, {
      headers: {
        Authorization: this.getAuth(),
        '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.getAuth(),
        '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.getAuth(),
        '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.getAuth(),
        'Content-Type': this.router.detail.dataType,
      },
      params: { id: id },
    })
  }

  /**
   * 获取md5
   *
   */
  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.getAuth(),
        'Content-Type': this.router.getMd5.dataType,
      },
    })
  }

  //普通文件上传
  async fileUpload(file:File):Promise<string>{
    const fd = new FormData()
    fd.append('file',file)
    return await this.service.post(this.router.fileUpload.path,fd,{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.fileUpload.dataType,
      }
    })
  }

  async downloadNFTFile(id:number):Promise<any>{
    return await this.service.post(this.router.downloadNFTFile.path +'/' +id,{},{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.ossPolicy.dataType,
      },
      responseType:'blob'
    })
  }

  async downloadCerfificate(body:any = {}){
    return await this.service.post(this.router.downloadCerfificate.path,body,{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.downloadCerfificate.dataType,
      },
      responseType:'blob',
    })
  }

  /**
   * 完成OSS签名
   */
  async ossPolicy() {
    const ret = await this.service.get(this.router.ossPolicy.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.ossPolicy.dataType,
      },
    })
    return ret
  }

  async uploadFileToOss(file: File, data: any) {
    const { accessid, dir,  host, policy, signature } = data as any
    const fd = new FormData()
    fd.append('name', file.name)
    fd.append('key', dir + '${filename}')
    fd.append('policy', policy)
    fd.append('OSSAccessKeyId', accessid)
    fd.append('success_action_status', '200')
    fd.append('signature', signature)
    fd.append('file', file)
    return await fetch(host, {
      method: 'POST',
      body: fd,
    })
  }

  /**
   * 发布
   * @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.getAuth(),
        'Content-Type': this.router.publish.dataType,
      },
    })
  }

  async getCertificate(id:any){
    return await this.service.get(this.router.getCertificateData.path + `/${id}`,{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getCertificateData.dataType,
      },
    })
  }

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

  async save(data: iSaveData) {
    const fd = new FormData()

    Object.keys(data).forEach((keyName) => {
      const value = data[keyName as keyof iSaveData]
      value !== undefined? fd.append(keyName, typeof value === 'number' ? String(value) : value):''
    })

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

  /**
   * 获取官方活动NFT列表
   * @returns 
   */
  async officialNFTList(){
    return await this.service.get(this.router.officialNFTList.path, {
      headers: {
        'Content-Type': this.router.officialNFTList.dataType,
      },
    })
  }

  /**
   * 官方nft的详细内容
   * @param id 
   * @returns 
   */
  async officialNFTDetail(id:any){
    return await this.service.get(this.router.officialNFTDetail.path+id, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.officialNFTDetail.dataType,
      }
    })
  }

  /**
   * 申请领取官方活动NFT
   */
  async applyOfficialNft(id:number){
    return await this.service.post(this.router.applyOfficialNft.path,{},{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.officialNFTDetail.dataType,
      },
      params:{id}
    })
  }


  /**
   * 转让NFT
   * @param data NftTransferDTO
   * @returns 
   */
  async nftTransfer(data:NftTransferDTO): Promise<any>{
    return await this.service.post(this.router.nftTransfer.path,data,{
      headers: {
        Authorization: this.getAuth()
      }
    })
  }

  async sendTransferCode(): Promise<any>{
    return await this.service.post(this.router.sendTransferCode.path,{},{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.officialNFTDetail.dataType,
      }
    })
  }
   /**
  * 获取nft转让记录列表
  */
  async getTransferList(type:string):Promise<any>{
    return await this.service.get(this.router.getTransferList.path+type,{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getTransferList.dataType,
      }
    })
  }
  /**
   * 获取nft转让记录详情
   */
  async getTransferDetail(id:string):Promise<any>{
    return await this.service.get(this.router.getTransferDetail.path+id,{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getTransferDetail.dataType,
      }
    })
  }
  /**
   * 个人实名认证
   */
  async certificateByIdcard(front:File,back:File):Promise<any>{
    const fl = new FormData()
    fl.append('front',front)
    fl.append('back',back)
    return await this.service.post(this.router.certificateByIdcard.path,fl,{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.officialNFTDetail.dataType,
      },
    })
  }
}


