

class BarCodeUtil {
  static barcode = null;

  /**
   * @private
   */
  static hasLight = false;


  /**
   * 停止扫码
   */
  static stopScan() {
    if (this.barcode !== null) {
      this.barcode.close();
    }
  }

  /**
   * 切换闪光灯
   * @return {boolean} 切换后闪光灯是否开启
   */
  static switchLight() {
    if (this.barcode !== null) {
      this.hasLight = !this.hasLight;
      this.barcode.setFlash(this.hasLight);
    }
    return this.hasLight;
  }

  static typeToString(type) {
    const { plus } = window;
    // eslint-disable-next-line guard-for-in
    for (const key in plus.barcode) {
      const value = plus.barcode[key];
      if (type === value) {
        return key;
      }
    }
    return '未知';
  }

  /**
   * 开始扫码
   * @param { (type, code)=>void } successHandler 扫码成功的处理方法
   */
  static startScan(successHandler) {
    this.stopScan();
    let view = null;
    const { plus } = window;
    const height = plus.screen.resolutionHeight - plus.navigator.getStatusbarHeight();
    this.barcode = plus.barcode.create(
      'barcode',
      [
        plus.barcode.QR,
        plus.barcode.EAN13,
        plus.barcode.EAN8,
        plus.barcode.AZTEC,
        plus.barcode.DATAMATRIX,
        plus.barcode.UPCA,
        plus.barcode.UPCE,
        plus.barcode.CODABAR,
        plus.barcode.CODE39,
        plus.barcode.CODE93,
        plus.barcode.CODE128,
        plus.barcode.ITF,
        plus.barcode.MAXICODE,
        plus.barcode.PDF417,
        plus.barcode.RSS14,
        plus.barcode.RSSEXPANDED,
      ],
      {
        top: '0',
        left: '0px',
        width: '100%',
        buttom: '0',
        position: 'absolute',
        zIndex: '1',
      },
    );
    this.barcode.onmarked = (type, result) => {
      this.stopScan();
      newBaseView.close();
      successHandler(type, result);
    };

    view = new plus.nativeObj.View(
      'test',
      {
        top: '20px', left: '20px', height: '30px', width: '80px',
      },
      [
        {
          tag: 'font',
          id: 'font',
          text: '关闭',
          textStyles: { size: '18px', color: '#fff' },
          position: {
            top: '0px',
            left: '0px',
            width: '100%',
            height: '30px',
          },
        },
      ],
    );

    // view.interceptTouchEvent(true);
    // plus.webview.currentWebview().append(view);

    const id = Date.now().toString();
    const newBaseView = plus.webview.create('', id, { top: '0px', bottom: '0px', height });
    newBaseView.append(this.barcode);
    newBaseView.append(view);
    newBaseView.show();
    this.barcode.start();
    view.addEventListener('click', () => {
      newBaseView.close();
      this.barcode.close();
    }, false);
  }

  /* 调用手机摄像头并拍照 */
  static getImage() {
    const { plus } = window;
    const cmr = plus.camera.getCamera();
    cmr.captureImage((p) => {
      plus.io.resolveLocalFileSystemURL(p, (entry) => {
        this.uploadImage(entry.toLocalURL(), entry.name); // 上传拍照的图片
      }, (e) => {
        plus.nativeUI.toast(`读取拍照文件错误：${e.message}`);
      });
    }, (e) => {
      plus.nativeUI.toast(`调用摄像头错误：${e.message}`);
    }, {
      filter: 'image',
    });
  }

  /* 相册选择图片 */
  static getPhoto() {
    const { plus } = window;
    plus.gallery.pick((path) => {
      const name = path.substring(path.lastIndexOf('/') + 1);
      this.uploadImage(path, name);// 上传选择的图片
    }, (e) => {
      plus.nativeUI.toast(`选取图片错误：${e.message}`);
    }, { filter: 'image' });
  }

  /* 图片上传入库 */
  static uploadImage(url, filename) {
    console.log('uploadImage：', url, filename);
  }
}

export default BarCodeUtil;
