import { Service } from './Service'

import { token } from '@/util/userInfoUtils'
export class NFTService extends Service {
  router = {
    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',
      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',
    },
    editCollection:{
      path: '/collection/add',
      dataType: 'application/x-www-form-urlencoded'
    },
    getCollectionList:{
      path: '/collection/list',
      dataType: 'application/x-www-form-urlencoded',
    }
  }
  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() {
    return await this.service.get(this.router.themes.path, {
      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
   * @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.getAuth(),
        '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.getAuth(),
        '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.getAuth(),
        'Content-Type': this.router.save.dataType,
      },
    })) as {
      fileHash: string
      id: number
      nftId: string
      wallet: string
    }
  }
}
