import { PluginBase } from '../../PluginBase';
import { scanIdentityCard } from '../../../../plugins/util/scan-identity-card';

export class ScanIdentityCardDemo extends PluginBase {
  public getPluginShortDesc(): JSX.Element {
    return (
      <div>启动扫描身份证号码</div>
    );
  }

  public getPluginFullDesc(): JSX.Element {
    return (
      <div>
        <div>启动扫描身份证号码返回的报文参考 "查看代码"</div>
      </div>
    );
  }

  public state = {
    value: '',
    idCardJson: '',
  };

  private viewCode = (mdPath) => () => {
    this.showMarkdownPopup(require(`./${mdPath}.md`));
  }
  // 身份证扫描正面
  private openScanIdCardSdk = () => {
    scanIdentityCard({
      isPositive: 'true',
    }).then(result => {
      this.setState({
        idCardJson: result.cardInfo.idCardJson,
        value: result.cardInfo.idCardImgBase64,
      });
    }).catch(err => {
      alert(JSON.stringify(err));
    });
  }
  // 身份证扫描反面
  private openScanIdCardSdkInverse = () => {
    scanIdentityCard({
      isPositive: 'false',
    }).then(result => {
      this.setState({
        idCardJson: result.cardInfo.idCardJson,
        value: result.cardInfo.idCardImgBase64,
      });
    }).catch(err => {
      alert(JSON.stringify(err));
    });
  }
  // 身份证扫描正面
  private openScanIdCardSdkFromAlbum = () => {
    scanIdentityCard({
      isPositive: 'true',
      isNeedAlbum: 'true',
    }).then(result => {
      this.setState({
        idCardJson: result.cardInfo.idCardJson,
        value: result.cardInfo.idCardImgBase64,
      });
    }).catch(err => {
      alert(JSON.stringify(err));
    });
  }
  // 身份证扫描正面
  private openScanIdCardSdkFromAlbumInverse = () => {
    scanIdentityCard({
      isPositive: 'false',
      isNeedAlbum: 'true',
    }).then(result => {
      this.setState({
        idCardJson: result.cardInfo.idCardJson,
        value: result.cardInfo.idCardImgBase64,
      });
    }).catch(err => {
      alert(JSON.stringify(err));
    });
  }
  public render() {
    const base64 = `url(data:image/jpeg;base64,${this.state.value})`;
    return (
      <div className="plugin-page">
        <h2 className="left">{this.props.pluginName}</h2>
        <div className="short-desc">{this.getPluginShortDesc()}</div>
        <div className="full-desc">{this.getPluginFullDesc()}</div>
        <div className="text-area" style={{ 'background-image': base64 }}></div>
        <h2>idCardJson</h2>
        <div className="text-area">
          {this.state.idCardJson}
        </div>
        <div className="body">
          <div className="play-body-item">
            <button className="exec" onClick={this.openScanIdCardSdk}>调用扫描身份证(正面)</button>
            <button className="view" onClick={this.viewCode('scan-identity-card')}>查看代码</button>
          </div>
          <div className="play-body-item">
            <button className="exec" onClick={this.openScanIdCardSdkInverse}>调用扫描身份证(反面)</button>
            <button className="view" onClick={this.viewCode('scan-identity-card')}>查看代码</button>
          </div>
          <div className="play-body-item">
            <button className="exec" onClick={this.openScanIdCardSdkFromAlbum}>从相册中识别身份证(正面)</button>
            <button className="view" onClick={this.viewCode('scan-identity-card')}>查看代码</button>
          </div>
          <div className="play-body-item">
            <button className="exec" onClick={this.openScanIdCardSdkFromAlbumInverse}>从相册中识别身份证(反面)</button>
            <button className="view" onClick={this.viewCode('scan-identity-card')}>查看代码</button>
          </div>
        </div>
      </div>
    );
  }
}
