import { LisioBooleanParameterNames, LisioNumericParameterNames, LisioParameterNames, LisioStringParameterNames } from "@lisio/lisio-profils";

import LisioAdapter from "../../adapters/lisio-adapter";
import { LisioBlurController } from "../../controllers/lisio-blur-controller";
import { Lisio } from "../../lisio";

function lisioFeatureMessageHandler(this: Lisio, datas: string) {
  const {
    feature,
    value,
    defaultValue,
    isReallyActive,
    isConfigured,
  }: {
    feature: LisioParameterNames;
    value: boolean | number | string;
    defaultValue: boolean | number | string;
    isReallyActive: boolean;
    isConfigured: boolean;
  } = JSON.parse(datas);
  if (
    (
      [
        LisioStringParameterNames.UNDERLINE_RED,
        LisioStringParameterNames.UNDERLINE_GREEN,
        LisioStringParameterNames.UNDERLINE_BLUE,
      ] as unknown as LisioParameterNames
    ).includes(feature) &&
    !(this.isBookPage || this.isReadingMode)
  ) {
    return;
  }
  // const convertedLangG = data.value.replace("_", "-");
  // this.params.lang = this.html.lang.toLocaleLowerCase().includes(convertedLangG) ? "default" : convertedLangG;
  // const convertedLangD = data.value.replace("_", "-");
  // this.params.lang = convertedLangD.includes(this.html.lang.toUpperCase()) ? "default" : convertedLangD;
  
  const currentFeatureValue = this.params.params.get(feature);
  if (this.textTreeWalker.texts.size == 0) {
    this.textTreeWalker.getTextes();
  }
  if (
    currentFeatureValue === value ||
    (feature !== LisioBooleanParameterNames.IS_ACTIVE &&
      currentFeatureValue == undefined &&
      currentFeatureValue === defaultValue)
  ) {
    return;
  }
  if (feature === LisioBooleanParameterNames.IS_ACTIVE) {
    this.popin?.refreshLabel(isReallyActive, isConfigured);
    this.setDisableBand(isReallyActive);
    if (value) {
      this.apply(false);
      this.mutationObserverController.canReapply = true;
    } else {
      this.setDefault(isConfigured, true);
      window.dispatchEvent(new CustomEvent("lisio-disabled"));
    }
  } else if (
    feature === LisioBooleanParameterNames.BOOK_PAGE &&
    !this.isBookPage
  ) {
    this.params.updateParam(feature, value);
    // this.confirmationModal.setDataFocus(
    //   this.confirmationModal.closeButton!,
    //   feature,
    // );
    // this.confirmationModal.setDataFocus(
    //   this.confirmationModal.buttonNo,
    //   feature,
    // );
    this.confirmationModal.attachEventListenerOnButtons(
      this.confirmationModal.buttonYes,
      "click",
      (event: Event) => {
        const bookPageAdapter: LisioAdapter<boolean> | undefined =
          this.adapters.get(LisioBooleanParameterNames.BOOK_PAGE);
        if (bookPageAdapter != undefined) {
          this.confirmationModal.yesHandler(event, this.widget, feature);
        }
        LisioBlurController.current.enableBlur();
      },
    );

    this.confirmationModal.attachEventListenerOnButtons(
      this.confirmationModal.buttonNo,
      "click",
      (event: Event) => {
        const bookPageAdapter: LisioAdapter<boolean> | undefined =
          this.adapters.get(LisioBooleanParameterNames.BOOK_PAGE);
        if (bookPageAdapter != undefined) {
          this.confirmationModal.noHandler(event, this.widget, feature);
        }
        LisioBlurController.current.disableBlur();
      },
    );
  } else if (
    feature === LisioBooleanParameterNames.READING_MODE &&
    !this.isReadingMode &&
    typeof value === "boolean"
  ) {
    this.params.updateParam(feature, value);
    if (window.localStorage.getItem("lisio_reading_mode") === "1") {
      const bookPageAdapter: LisioAdapter<boolean> | undefined =
        this.adapters.get(LisioBooleanParameterNames.READING_MODE);
      bookPageAdapter?.adapt(this.textTreeWalker, value);
    } else {
      // this.confirmationModal.setDataFocus(
      //   this.confirmationModal.closeButton!,
      //   feature,
      // );
      // this.confirmationModal.setDataFocus(
      //   this.confirmationModal.buttonNo,
      //   feature,
      // );
      this.confirmationModal.attachEventListenerOnButtons(
        this.confirmationModal.buttonYes,
        "click",
        (event: Event) => {
          const bookPageAdapter: LisioAdapter<boolean> | undefined =
            this.adapters.get(LisioBooleanParameterNames.READING_MODE);
          if (bookPageAdapter != undefined) {
            this.confirmationModal.yesHandler(event, this.widget, feature);
          }
          LisioBlurController.current.enableBlur();
        },
      );

      this.confirmationModal.attachEventListenerOnButtons(
        this.confirmationModal.buttonNo,
        "click",
        (event: Event) => {
          const bookPageAdapter: LisioAdapter<boolean> | undefined =
            this.adapters.get(LisioBooleanParameterNames.READING_MODE);
          if (bookPageAdapter != undefined) {
            this.confirmationModal.noHandler(event, this.widget, feature);
          }
          LisioBlurController.current.disableBlur();
        },
      );
    }
  } else {
    this.params.updateParam(feature, value);
    if (this.mutationObserverController.observer == undefined) {
      this.mutationObserverController.setObserver();
    } else {
      this.mutationObserverController.connectObserver();
    }
    this.textTreeWalker.resetWalker();
    this.textTreeWalker.getTextes();
    if(([LisioNumericParameterNames.DALTONISM_R, LisioNumericParameterNames.DALTONISM_G, LisioNumericParameterNames.DALTONISM_B, LisioNumericParameterNames.DALTONISM_HUE, LisioNumericParameterNames.DALTONISM_SATURATION, LisioNumericParameterNames.DALTONISM_BRIGHTNESS] as unknown as LisioParameterNames).includes(feature)){
      this.applyFeature(feature, value, defaultValue);
    } else {
      this.applyFeature(feature, value);
    }
  }
  this.popin?.refreshLabel(this.params.params.size > 0, isConfigured);
  this.setDisableBand(this.params.params.size > 0);
}

export { lisioFeatureMessageHandler };
