import React from 'react';
import { Renderer, RendererProps } from '../../../../factory';
import { BaseSchema } from '../../../../Schema';
import MSG from '../../utils/msgsub';
import { Shell } from '../../../../utils/shell/index';
import { render as renderAmis } from '../../../../index'

// export const handleTempFiles = (tempFiles: any[], state: any): any[] => {
//   let mediaDataList: any[] = [];
//   if (tempFiles?.length > 0) {
//     mediaDataList = tempFiles.map(tempFileItem => (handleTempFile(tempFileItem, state)))
//   }
//   return mediaDataList
// }

// export const handleTempFile = (tempFile: any, state: any): any => {
//   return {
//     sourceFileName: tempFile.name,
//     sourceFileUrl: tempFile.path,
//     size: tempFile.size,
//     state
//   }
// }

// 将类型导出，命名需规范
export interface LionScanCodeSchema extends BaseSchema {
  type: 'lion-scan-code';
  title: string;
}

export interface LionScanCodeProps extends RendererProps, Omit<LionScanCodeSchema, 'type' | 'className'> { }

export class LionScanCode extends React.Component<LionScanCodeProps, object>{
  static defaultProps: Partial<LionScanCodeProps> = {
  }

  private handleOpenCamera = () => {
    Shell.getScanCode().then(res => {
      const { render, title } = this.props;
      if (res.success) {
        let content = res.data.content;
        if (content.includes('.weixin.qq.com')) {
          MSG._info('请用微信扫码', this.props.env?.getModalContainer)
        } else if (content.includes('qr_type') && content.includes('qr_key')) {
          // 页面数据分享
          // FLRule.toShareView(content);
          MSG._success(content, this.props.env?.getModalContainer)
        } else if (typeof content === 'string' && content?.length === 5) {
          // 终端互联连接
          // this.connect(content);
          MSG._success(content, this.props.env?.getModalContainer)
        } else {
          MSG._success(content, this.props.env?.getModalContainer)
        }
      } else {
        MSG._info(res?.msg, this.props.env?.getModalContainer)
      }
    })
  }

  // private openCamera = () => {
  //   Shell.openCamera(1).then(res => {
  //     if (res.success === true) {
  //       console.log('camera', res)
  //       let waitUploadList = handleTempFiles(res.data.tempFiles, 'needUpload');
  //       console.log('waitUploadList', waitUploadList)
  //       Shell.uploadFile({
  //         path: waitUploadList[0].sourceFileUrl,
  //         url: this.props.url,
  //         chunkUpload: false,
  //         formData: {

  //         }
  //       }, (info) => {
  //         console.log('info', info)
  //       }, (res) => {
  //         console.log('res', res)
  //       }, (fail) => {
  //         console.log('fail', fail)
  //       })
  //       this.setState({
  //         waitUploadList: [...this.state.waitUploadList, ...waitUploadList]
  //       })
  //     }
  //   })
  // }

  render() {
    const { title } = this.props;
    // 自定义你的代码逻辑
    return renderAmis({
      "type": "action",
      "label": title,
      "level": "primary",
      "onAction": this.handleOpenCamera
    })
  }
}

@Renderer({
  type: 'lion-scan-code'
})
export class LionScanCodeRenderer extends LionScanCode { }

