import styles from "../assets/css/shadowroot/lisio-widget.css?raw";
import LisioLightMessageManager from "../managers/lisio-light-message-manager";
import { LisioBlurController } from "./lisio-blur-controller";
import LisioStatsController from "./lisio-stats-controller";
import LisioTranslationController from "./lisio-translation-controller";

class LisioWidgetController {
  private _isLoaded = false;
  private _isOpen = false;
  private _isReady = false;
  private _widget?: HTMLIFrameElement;

  constructor(side?: "right" | "left") {
    if (__BUILD_TARGET__ === "extension") {
      console.log("extension");
    } else {
      this._widget = document.createElement("iframe");
      this._widget.id = "lisio-widget";
      this._widget.title =
        LisioTranslationController.current.getTranslation("iframeTitle");
      this._widget.classList.add(side || "L");
      this._widget.setAttribute("aria-hidden", "true");
      this._widget.setAttribute("tabindex", "-1");
    }
    window.addEventListener("lisio-lang-change", (event) => {
      if (event instanceof CustomEvent && this._widget != undefined) {
        LisioTranslationController.current.translateWidget(this._widget);
      }
    });
  }

  public get isOpen() {
    return this._isOpen;
  }

  public get isReady() {
    return this._isReady;
  }

  public set isReady(value: boolean) {
    this._isReady = value;
  }

  public get isLoaded() {
    return this._isLoaded;
  }

  public set isLoaded(value: boolean) {
    this._isLoaded = value;
  }

  public get widget(): HTMLIFrameElement | undefined {
    return this._widget;
  }

  public closeWidget() {
    if (__BUILD_TARGET__ === "extension") {
      // LisioMessageManager.current.sendCloseExtensionPopup();
    } else {
      if (this._isOpen && this._widget != undefined) {
        window.dispatchEvent(new Event("lisio-widget-close"));
        this._isOpen = false;
        this._widget.setAttribute("aria-hidden", "true");
        this._widget.classList.remove("open");
        document.getElementById("lisio-shadow-env")?.focus();
        LisioBlurController.current.disableBlur();
        this._widget.tabIndex = -1;
      }
    }
  }

  public async loadWidget(iframeSrc: string) {
    if (this._widget != undefined) {
      const promiseWidgetLoad = new Promise((res) => {
        this._widget?.addEventListener("load", () => {
          this._isReady = true;
          res(true);
        });
      });
      this._widget.src = iframeSrc;
      return promiseWidgetLoad;
    }
  }

  public openWidget() {
    if (!this._isOpen && this._widget != undefined) {
      this._isOpen = true;
      window.dispatchEvent(new Event("lisio-widget-open"));
      LisioBlurController.current.enableBlur();
      this._widget.setAttribute("aria-hidden", "false");
      this._widget.classList.add("open");
      if (__BUILD_SUB_TARGET__ !== "dev") {
        LisioStatsController.current.incrementNbOpen();
      }
      this._widget.tabIndex = 0;
      if (!this._isLoaded) {
        LisioLightMessageManager.current.sendLoadWidget();
        this._isLoaded = true;
      }
    }
  }
}

export { LisioWidgetController, styles };
