import { Service } from '../Service'

import { token } from '@/util/userInfoUtils'
import { iSaveData } from './types'
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',
    },
    ossPolicy: {
      path: '/oss/policy',
      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',
    },
    officialNFTList: {
      path: '/commemorate/nft/list',
      dataType: 'application/x-www-form-urlencoded'
    },
    officialNFTDetail:{
      path:'/commemorate/nft/get/',
      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
   *
   */
  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,
      },
    })
  }

  /**
   * 完成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,
      },
    })
  }

  /**
   * 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,
      }
    })
  }

  async applyOfficialNft(){
    
  }
}
