import { Widget } from "./Widget";
import { AstHelper } from "@gvol-org/geovis-brain-core";
import { Http } from "../Http";
/**
 * @class ui.Button
 * @since ui
 */
export class Button extends Widget {
  disabled: boolean = false;
  /**
   * @hideconstructor
  * @param {string} [label] 显示文本
  * @param {Function} [onClick] 回调函数
  * @param {boolean} [disabled]  按钮是否可用
  * @param {object} [style] 样式
  * @param {string} [imageUrl] 图片url地址
   * @return ui.button
   */
  constructor(
    label?: string,
    onClick?: Function,
    disabled?: boolean,
    style?: object,
    imageUrl?: string
  ) {
    let btn;
    if (typeof process === 'undefined') {
      btn = document.createElement("div");
      btn.innerText = label ?? "";
      btn.classList.add("ui-button");
    }
    else {
      style = null;
    }

    super(btn, style);
    if (!(this instanceof Button)) {
      return new Button(label, onClick, disabled, style, imageUrl);
    }
    if (typeof process === 'undefined') {
      if (onClick) {
        let self = this;
        btn.onclick = function () {
          if (!self.disabled) {
            // let funcName = onClick['name'];
            // console.log('button函数体', onClick.toString());
            // console.log('button函数名', funcName);
            // if(!Http.fullCode){
            //   Http.fullCode=onClick.toString();
            // }
            // let codeStr = AstHelper.getCodeBlockByTypeAndName(Http.fullCode, funcName, 'FunctionDeclaration');
            // Http.codeContent = codeStr;
            Http.isRunSharePage = false;
            onClick();
          }
        };
      }
      this.setDisabled(disabled);
    }
  }
  /**
   * 返回按钮是否可用。
   * @return {Boolean} Boolean
   */
  getDisabled() {
    //Returns whether the button is disabled.
    return this.disabled;
  }
  /**
   * 返回图片url地址
   * @return String
   */
  getImageUrl() {
    //Returns the url of the image if it exists.
  }
  /**
   * 返回按钮上的显示文本
   * @return {String} String
   */
  getLabel() {
    //Returns the button's label.
    return this.el.innerText;
  }

  /**
   * 点击按钮回调方法
   * @param {Function} callback 按钮回调方法。
   */
  onClick(callback: Function) {
    //The callback to fire when the button is clicked. The callback is passed the button widget.
    let self = this;
    self.el.onclick = function () {
      if (!self.disabled) {
        callback();
      }
    };
  }
  /**
   * 设置按钮是否可用
   * @param {Boolean} disabled  按钮是否可用
   * @return  ui.Button
   */
  setDisabled(disabled: boolean) {
    //Sets whether the button is disabled.Returns this button.
    this.disabled = disabled;
    if (this.disabled) {
      this.el.classList.add("ui-button-disabled");
    } else {
      this.el.classList.remove("ui-button-disabled");
    }
    return this;
  }
  /**
   * 设置图片Url地址
   * @param {String} imageUrl 图片Url地址
   * @return  ui.Button
   */
  setImageUrl(imageUrl: string) {
    //Shows the button as image, which will render instead of the label text. Returns this button.
    return this;
  }
  /**
   * 设置label按钮标签
   * @param {String} label 按钮
   * @return  ui.Button
   */
  setLabel(label: string) {
    //Sets the button's label. Returns this button.
    this.el.innerText = label;
    return this;
  }
  /**
   * onEventType()函数在回调注册期间返回的ID
   * @param {String} idOrType  可选
   */
  unlisten(idOrType?: string) {
    //Either an ID returned by an onEventType() function during callback registration,
    // an event type, or nothing. If an ID is passed, the corresponding callback is deleted.
    // If an event type is passed, all callbacks for that type are deleted. If nothing is passed, all callbacks are deleted.
  }
}