import { Component, Vue, Inject } from 'vue-property-decorator';
import { ChainStatus } from '@/const/enum';
@Component
export default class Proof extends Vue {
  /* 本mixins被 存证页面和 回收站页面 两个页面使用 */
  @Inject('goToBrowser') protected goToBrowser!: (proof: any) => void;
  @Inject('getProofViewingWebSite') protected getProofViewingWebSite!: (
    proof: any,
  ) => string;
  protected baseProof: any = '';
  protected preProof: any = '';
  /* 是否是增量 */
  protected isZengliang: boolean = false;
  /* 添加存证框是否显示 */
  protected showAddProof: boolean = false;
  /* 动作列表 */
  protected ActionList: any = {
    blockChain: {
      key: 'blockChain',
      icon: 'shanglian',
      text: '上链',
      callback: this.blockChainProof,
    },
    edit: {
      key: 'edit',
      icon: 'bianji2x',
      text: '编辑',
      callback: this.goProofDetail,
    },
    del: {
      key: 'del',
      icon: 'shanchu2x',
      text: '移至回收站',
      callback: this.delProof,
    },
    copy: {
      key: 'copy',
      icon: 'chuangjianfuben2x',
      text: '创建副本',
      callback: this.copyProof,
    },
    blockquery: {
      key: 'blockquery',
      icon: 'qukuailianchaxun2x',
      text: '区块链查询',
      callback: this.goToBrowser,
    },
    viewQrCode: {
      key: 'viewQrCode',
      icon: 'chakanerweima2x',
      text: '查看二维码',
      callback: this.goToQrCodePage,
    },
    copyhash: {
      key: 'copyhash',
      icon: 'fuzhiHASHdizhi2x',
      text: '复制hash地址',
      callback: this.copyHash,
    },
    del2: {
      key: 'del2',
      icon: 'shanchu2x',
      text: '移至回收站',
      callback: this.abandonProof,
    },
    zengliang: {
      key: 'zengliang',
      icon: 'shanglian',
      text: '增量更新',
      callback: this.increment,
    },
    hide: {
      key: 'hide',
      icon: 'yinsishezhi2x',
      text: '隐私设置',
      callback: this.hide,
    },
  };
  protected resetList() {
    throw Error('resetList 在列表实现');
  }
  private goToQrCodePage(proof: any) {
    const locationUrl = this.getProofViewingWebSite(proof);
    this.$router.push({
      name: 'ViewCode',
      query: {
        locationUrl,
      },
    });
  }
  private abandonProof(proof: any) {
    /* 接受到的proof是存证的所有信息 */
    /* 存证包含了
      遗弃标志
      创建时间
      存证详细内容
      存证hash
      id
      increment_num? 猜测它应该表示 当前是否是增量存证，并且是第几级增量
      is_valid 是否有效？
      名字 存证名称
      note 包含了很多信息，其中有 用户名字，用户头像，一些banner ...
      proof_version 存证版本
      status，存证状态
      更新时间
      用户id
       */
    const { id: proofId } = proof;
    this.$confirmdel().then((res: any) => {
      this.$api.proof.abandon(proofId, '暂无原因').then(() => {
        this.$toast.success('已放入回收站');
        this.resetList();
      });
    });
  }
  private blockChainProof(proof: any) {
    this.$confirmchain().then((res: any) => {
      this.$api.proof
        .sendBlockChian(proof.id)
        .then(() => {
          this.$toast.success('提交上链成功');
          this.resetList();
        })
        .catch((err: any) => {
          if (err.code === 10811) {
            this.$dialog
              .confirm({
                title: '提示',
                message: '您还未认证，请先认证后在进行上链',
                confirmButtonText: '去认证',
                cancelButtonText: '取消',
              })
              .then(() => {
                this.$router.push({ name: 'Auth' });
              });
          }
          if (err.code === 10702) {
            this.$dialog
              .confirm({
                title: '提示',
                message: '剩余上链次数不足，请充值',
                confirmButtonText: '去充值',
                cancelButtonText: '取消',
              })
              .then(() => {
                this.$router.push({
                  name: 'Recharge',
                });
              });
          }
        });
    });
  }
  private goProofDetail(proof: any) {
    const { id: proofId } = proof;
    const toBlockStatuss: ChainStatus[] = [
      ChainStatus.SUCCESS,
      ChainStatus.HIDED,
      ChainStatus.HIDING,
      ChainStatus.HIDEFAILURE,
    ];
    if (toBlockStatuss.includes(proof.status)) {
      this.goToBrowser(proof);
    } else {
      this.$router.push({ name: 'ProofDetail', query: { proofId } });
    }
  }
  private async copyProof(proof: any) {
    await this.$api.proof.add(proof.name, JSON.parse(proof.detail), proof.note);
    this.$toast.success('复制成功');
    this.resetList();
  }
  private async reName(proof: any) {
    this.$toast('重命名成功');
  }
  private delProof(proof: any) {
    const { id: proofId } = proof;
    this.$dialog.confirm({
      title: '提示',
      message: '删除后，信息无法找回，确定要删除吗？',
      beforeClose: (action, done) => {
        if (action === 'confirm') {
          this.$api.proof
            .delete(proofId)
            .then((res: any) => {
              this.$toast.success('删除成功');
              this.resetList();
              done();
            })
            .catch((err: any) => {
              done();
            });
        } else {
          done();
        }
      },
    });
  }
  private copyHash(proof: any) {
    this.$copyText(proof.hash);
    this.$toast('hash地址复制成功');
  }
  private async increment(proof: any) {
    const { increment_num, hash } = proof;
    if (increment_num > 0) {
      const { results } = await this.$api.proof.incrementList(hash);
      const preProof = results[0];
      if (!preProof.hash) {
        this.$toast('请先上链上一条存证');
        return Promise.reject('请先上链上一条存证');
      }
      this.preProof = preProof;
    } else {
      this.preProof = proof;
    }
    this.baseProof = proof;
    this.showAddProof = true;
    this.isZengliang = true;
    this.$router.push({
      name: 'ProofList',
      query: {
        preProof: JSON.stringify(this.preProof),
        baseProof: JSON.stringify(this.baseProof),
      },
    });
  }

  private async hide(proof: any) {
    this.$dialog.confirm({
      title: '提示',
      message: '隐藏该存证后，区块链列表不做展示，确定要隐藏吗？',
      beforeClose: async (action, done) => {
        if (action === 'confirm') {
          await this.$api.proof.deleteBlockChainProof(proof.id);
          this.$toast('隐藏提交成功');
          done();
        } else {
          done();
        }
        this.resetList();
      },
    });
  }
}
