import { Service } from '../Service'

import { token } from '@/util/userInfoUtils'
export class CopyrightService extends Service {
  router = {
    dowloadFile: {
      path: 'https://test-nft.oss-cn-hangzhou.aliyuncs.com/20211210/ba81002e5d98484e94d141a17d5c47f6/',
      dataType: 'application/x-www-form-urlencoded',
    },
    saveDraft: {
        path: '/proxyApi/Draft/save',
        dataType: 'application/json',
    },
    copyrightApply: {
      path: '/proxyApi/copyright/apply/submit',
      dataType: 'application/json',
    },
    copyrightOwnerAdd: {
      path: '/proxyApi/copyright/owner/add',
      dataType: 'application/json',
    },
    getCopyrightOwnerList: {
      path: '/proxyApi/copyright/owner/list',
      dataType: 'application/json',
    },
    uploadCopyrightFile: {
      path: '/proxyApi/copyright/file/upload',
      dataType: 'multipart/form-data',
    },
    deleteFile: {
      path: '/proxyApi/oss/delete',
      dataType: 'application/json',
    },
    getDraft: {
      path: '/proxyApi/Draft/detail',
      dataType: 'application/json',
    },
    getDraftList: {
      path: '/proxyApi/Draft/list',
      dataType: 'application/json',
    },
    getCopyrightList: {
      path: '/proxyApi/copyright/apply/list',
      dataType: 'application/json'
    },
    getContentJson: {
      path: '/proxyApi/copyright/apply/content',
      dataType: 'application/json'
    },
    deleteDraft: {
      path: '/proxyApi/Draft/delete',
      dataType: 'application/json'
    },
    deleteCopyrightApply: {
      path: '/proxyApi/copyright/apply/delete',
      dataType: 'application/json'
    },
    withdrawCopyrightApply: {
      path: '/proxyApi/copyright/apply/withdraw',
      dataType: 'application/x-www-form-urlencoded'
    },
    getTemplateUrlList: {
      path: '/proxyApi/file/template/list/',
      dataType: 'application/x-www-form-urlencoded'
    },
    getMyList: {
      path: "/proxyApi/nft/list/copyright",
      dataType: 'application/json'
    },
    downloadCprCer: {
      path:"/proxyDowload/copyRightCertificate",
      dataType: 'application/json;charset=utf-8'
    },
    deleteCopyrightOwner: {
      path: "/proxyApi/copyright/owner/delete",
      dataType: 'application/json'
    },
    getApplyDetail: {
      path: "/proxyApi/copyright/apply/detail",
      dataType: 'application/json'
    },
    getApplyCount: {
      path: '/proxyApi/copyright/apply/evidence/count',
      dataType: 'application/x-www-form-urlencoded'
    },
    getApplyState: {
      path: '/proxyApi/copyright/apply/state',
      dataType: 'application/x-www-form-urlencoded'  
    },
    editCprApply: {
      path: '/proxyApi/copyright/apply/update',
      dataType: 'application/json'
    },
    getCprCertificateConfig: {
      path: '/proxyApi/copyright/apply/certificate/',
      dataType: 'application/x-www-form-urlencoded'
    }

  }
  constructor() {
    super()
  }

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


  /**获取我的NFT列表 */
  async getMyList(): Promise<any>{
    return await this.service.get(this.router.getMyList.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getMyList.dataType,
      },
    })
  }

  /**下载文件 */
  async downloadFile(): Promise<any[]> {
    return await this.service.get(this.router.dowloadFile.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.dowloadFile.dataType,
      },
    })
  }

  /**保存草稿 */
  async saveDraft(rawJson: string, mapJson: string, step: number, copyrightId?: string ): Promise<any[]> {
      return await this.service.post(this.router.saveDraft.path, {
          content: rawJson,
          json: mapJson,
          step,
          copyrightId
      }, {
        headers: {
            Authorization: this.getAuth(),
            'Content-Type': this.router.saveDraft.dataType,
          }, 
      })
  }

  /**版权申请 */
  async copyrightApply(mapData:any): Promise<any[]> {
      return await this.service.post(this.router.copyrightApply.path, mapData, {
        headers: {
            Authorization: this.getAuth(),
            'Content-Type': this.router.copyrightApply.dataType,
          }, 
      })
  }

  /**添加著作权人 */
  async copyrightOwnerAdd(ownerData: any): Promise<any> {
    return await this.service.post(this.router.copyrightOwnerAdd.path, ownerData, {
      headers: {
          Authorization: this.getAuth(),
          'Content-Type': this.router.copyrightOwnerAdd.dataType,
        }, 
    })
  }
  /**删除著作权人 */
  async deleteCopyrightOwner(id:number): Promise<any> {
    return await this.service.post(this.router.deleteCopyrightOwner.path, {}, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.copyrightOwnerAdd.dataType,
      }, 
      params: {
        id
      }
    })
  }

  /**获取著作权人列表 */
  async getCopyrightOwnerList(): Promise<any> {
    return await this.service.get(this.router.getCopyrightOwnerList.path, {
      headers: {
          Authorization: this.getAuth(),
          'Content-Type': this.router.getCopyrightOwnerList.dataType,
        }, 
    })
  }

  /**上传版权申请附件 */
  async uploadCopyrightFile(file: File, callBack?:(p:number) => void): Promise<string>{
    const fd = new FormData()
    fd.append('file', file)
    return await this.service.post(this.router.uploadCopyrightFile.path, fd, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.uploadCopyrightFile.dataType,
      }, 
      onUploadProgress: (p)=>{
        if(callBack) callBack(p)
      }
    })
  }

  /**删除文件 */
  async deleteFile(fileUrl: string){
    return await this.service.post(this.router.deleteFile.path, {}, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.deleteFile.dataType,
      }, 
      params: {
        fileUrl: fileUrl
      }
    })
  }

  /**获取草稿 */
  async getDraft(draftId: any) {
    return await this.service.get(this.router.getDraft.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getDraft.dataType,
      }, 
      params: {
        id: draftId
      }
    })
  }

  /**获取草稿列表 */
  async getDraftList(){
    return await this.service.get(this.router.getDraftList.path, {
      headers: {
        token: this.getAuth(),
        Authorization: this.getAuth(),
        'Content-Type': this.router.getDraftList.dataType,
      }, 
    })
  }

  /**删除草稿 */
  async deleteDraft(draftId: number){
    return await this.service.post(this.router.deleteDraft.path, {},{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getDraftList.dataType,
      }, 
      params: {
        id: draftId
      }
    })
  }

  /**查询版权登记列表 */
  async getCopyrightList(state: number){
    return await this.service.get(this.router.getCopyrightList.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getDraftList.dataType,
      }, 
      params: {
        state
      }
    })
  }

  /**获取contentJson */
  async getContentJson(editId: any){
    return await this.service.get(this.router.getContentJson.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getContentJson.dataType,
      }, 
      params: {
        id: editId
      }
    })
  }

  /**删除版权申请 */
  async deleteCopyrightApply(id: number){
    return await this.service.post(this.router.deleteCopyrightApply.path, {}, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.deleteCopyrightApply.dataType,
      }, 
      params: {
        id
      }
    })
  }

  /**撤回版权申请 */
  async withdrawCopyrightApply(id: number){
    return await this.service.post(this.router.withdrawCopyrightApply.path, {}, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.withdrawCopyrightApply.dataType,
      }, 
      params: {
        id
      }
    })
  }

  /**下载版权证书 */
  async downloadCprCer(body: any) {
    return await this.service.post(this.router.downloadCprCer.path, body, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.downloadCprCer.dataType,
      },
      responseType:'blob',
    })
  }

  /**根据类型查询附件的路径 */
  async getTemplateUrlList(type: string){
    return await this.service.get(this.router.getTemplateUrlList.path+type, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getTemplateUrlList.dataType,
      },
    })
  }

  /**获取申请详情 */
  async getApplyDetail(id: string): Promise<any>{
    return await this.service.get(this.router.getApplyDetail.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getApplyDetail.dataType,
      },
      params: {
        id
      }
    })
  }

  /**获取存证数量 */
  async getApplyCount():Promise<any>{
    return await this.service.get(this.router.getApplyCount.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getApplyCount.dataType,
      },
    })
  }

  /**获取版权状态 */
  async getApplyState(id: string):Promise<any>{
    return await this.service.get(this.router.getApplyState.path, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getApplyState.dataType,
      },
      params: {
        id
      }
    })
  }

  /**编辑申请 */
  async editCprApply(mapData:any): Promise<any> {
    return await this.service.post(this.router.editCprApply.path, mapData, {
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.editCprApply.dataType,
      },
    })
  }

  /**查看版权存证证书 */
  async getCprCertificate(id: number): Promise<any> {
    return await this.service.get(this.router.getCprCertificateConfig.path+id,{
      headers: {
        Authorization: this.getAuth(),
        'Content-Type': this.router.getCprCertificateConfig.dataType,
      },
    })
  }
}


