import { Subject } from "rxjs";
import { Http } from "../Http";
import { ExportAsync } from "./ExportAsync";
import { Image } from "../models/Image";
import { Geometry } from "../models/Geometry";
import { PrintSdk } from "../printsdk/printSdk";
import { AstHelper } from "@gvol-org/geovis-brain-core";
/**
 * @class Export.image
 * @since Export
 * @todo image.toDrive
 */
export class image extends ExportAsync {
  /**
   * @hideconstructor
   * @see Export.image
   * @returns Export.image
   */
  constructor(...param: any[]) {
    super();
    if (!(this instanceof image)) {
      return new image(param);
    }
  }

  //   /**
  //  * 导出Geometry对象为Geojson 参数分别为:
  //  *  image:导出影像
  //  *  description:导出任务描述也就是任务名称
  //  *  assetId:用户空间的资源Id
  //  *  region:导出范围
  //  *  scale:分辨率300米
  //  *  fileFormat: 可选参数,默认GeoTIFF
  //  *  @param {Object} obj 批处理对象
  // *   @tutorial Export.image
  //  * */
  //   static toDrive(obj: {
  //     image: Image;
  //     description?: string;
  //     assetId?: string;
  //     region?: Geometry;
  //     scale?: number;
  //     fileFormat?: string;
  //   }) {
  //     //拼接代码
  //     let codeContent = "Export.image.toDrive({";
  //     for (const key in obj) {
  //       if (obj.hasOwnProperty(key)) {
  //         if(obj[key].expressions){
  //           codeContent = codeContent + `${key}: ${obj[key].expressions},`; 
  //         }else{
  //           if(typeof obj[key] == 'string'){
  //             codeContent = codeContent + `${key}: "${obj[key]}",`; 
  //           }else{
  //             codeContent = codeContent + `${key}: ${obj[key]},`; 
  //           }

  //         }
  //       }
  //     }
  //     //删掉多余的逗号
  //     if (codeContent.endsWith(",")) {
  //       codeContent = codeContent.slice(0, -1);
  //     }
  //     codeContent = codeContent + "});"
  //     let exportObj = new ExportAsync();
  //     exportObj.execute(async () => {
  //       let data = await Http.getRequest(codeContent);
  //       Http.responseSubject$.next(data);
  //       if(data){
  //         exportObj.param = data;
  //         console.log("subjecttttt", this.exportSubject$);
  //         this.exportSubject$.next(data.editor.exportObj);
  //       }else{
  //         console.log("应当删除export")
  //       }
  //     });
  //     return exportObj;
  //   }

  /**
   * 导出影像数据
   * @param {Object} args 导出对象
   * @tutorial Export.image
   */
  static toDrive(args: {
    image: Image | any;
    description?: string;
    assetId?: string;
    region?: Geometry;
    scale?: number;
    fileFormat?: string;
  }) {
    if (args.scale) {
      Http.scale = args.scale.toString();
    }
    let newObj = JSON.parse(JSON.stringify(args));
    let { newChain, newOrder } = AstHelper.objectParamChain(newObj);

    newChain.push({
      "functionName": "Export.image.toDrive",
      "arguments": {
        "args": newObj
      },
      "order": newOrder
    })
    let content = {
      type: 'map',
      chain: newChain
    };
    if ((window as any).astCallback) {
      (window as any).astCallback(content);
      return;
    }
    let exportObj = new ExportAsync();
    exportObj.execute(async () => {
      let data = await Http.getRequest(newChain, false, true);
      Http.responseSubject$.next(data);
      if (data && data.data.status == "success") {
        exportObj.param = data;
        // console.log("subjecttttt", this.exportSubject$);
        this.exportSubject$.next(data.data.detail);
      } else {
        // console.log("应当删除export")
      }
    });
    return exportObj;
  }

}