/**
 * @mergeTarget
 * @module Src/Screens/Account
 */
import {
  LisioCategoryNames,
  LisioParameterNames,
  LisioProfileNames,
} from "@lisio/lisio-profils";
import { LisioScreen } from "../lisio-screen";
import {
  BooleanParameterRenderObject,
  StringParameterRenderObject,
  NumberParameterRenderObject,
  CategoryRenderObject,
  ProfileRenderObject,
  ScreenNames,
} from "../screen-types";
import { CustomButton } from "../../components/custom-button/custom-button";
import { CustomText } from "../../components/custom-text/custom-text";
import { ElementsContainer } from "../../components/containers/elements-container/elements-container";
import { LisioComponent } from "@lisio/lisio-engine";
import {
  getMessageManager,
  getScreenManager,
} from "../../../managers/manager-registry";
import { accountScreenStyles } from "./account-screen.css";
import { LisioComponentStyles } from "@lisio/lisio-engine";

/**
 * Class representing the manage account screen component extending LisioScreen.\
 * It aims to represent a screen letting the user change their active key or disconnect.\
 * It is reached from the manage account button of the header, only relevant for extension build.\
 */
class AccountScreen extends LisioScreen {
  /**
   * Protected attribute to store category render objects
   * @source
   */
  protected _categoryRenderObjects: Map<
    LisioCategoryNames,
    CategoryRenderObject
  > = new Map<LisioCategoryNames, CategoryRenderObject>();

  /**
   * Protected attribute to store profile render objects
   * @source
   */
  protected _profileRenderObjects: Map<LisioProfileNames, ProfileRenderObject> =
    new Map<LisioProfileNames, ProfileRenderObject>();

  /**
   * Protected attribute to store parameter render objects
   * @source
   */
  protected _parameterRenderObjects: Map<
    LisioParameterNames,
    | BooleanParameterRenderObject
    | StringParameterRenderObject
    | NumberParameterRenderObject
  > = new Map<
    LisioParameterNames,
    | BooleanParameterRenderObject
    | StringParameterRenderObject
    | NumberParameterRenderObject
  >();

  /**
   * Private attribute to store the link to the change key screen
   * @source
   */
  private _changeKeyButton: CustomButton;

  /**
   * Private attribute to store the current key value text
   * @source
   */
  private _currentKeyText: CustomText;

  /**
   * @async
   * Public method implementing abstract method render of LisioComponent
   * @returns Returns a promise containing the representation of the account screen in HTML string
   * @source
   */
  public async render(): Promise<string> {
    return `
    <section id="${this._id}">
      <children></children>
    </section>
    `;
  }

  /**
   * Public method to show or hide the "change key" button depending on how many keys the account has
   * @param {boolean} hasMultipleKeys - Indicates if the account has more than one key
   * @returns Returns nothing
   * @source
   */
  public setHasMultipleKeys(hasMultipleKeys: boolean): void {
    this._changeKeyButton.showHideElement(hasMultipleKeys);
  }

  /**
   * Public method to update the current key value displayed on the screen
   * @param {string | undefined} keyValue - Value of the currently active key
   * @returns Returns nothing
   * @source
   */
  public setCurrentKeyValue(keyValue?: string): void {
    this._currentKeyText.changeText(`Clé actuelle : ${keyValue ?? "—"}`, true);
  }

  /**
   * Constructor of class {@link AccountScreen | AccountScreen}
   * @source
   */
  constructor() {
    ScreenNames[AccountScreen.name] = "account-screen";
    super(AccountScreen.name, AccountScreen.name);

    this._screenTitle = new CustomText(
      "h2",
      "Gérer mon compte",
      true,
      ["tall", "bold", "custom-color"],
      undefined,
      "screen-account-title",
    );
    const elementsTitleContainer: LisioComponent = new ElementsContainer(
      [this._screenTitle],
      undefined,
      ["row", "full", "transparent", "align-items-center", "justify-content-center"],
    );

    this._changeKeyButton = new CustomButton(
      "account-change-key-button",
      [new CustomText("span", "Changer de clé", true, ["white"])],
      async (e) => {
        getScreenManager().changeScreen("change-key-screen", e.detail);
      },
      ["btn-pill", "blue", "width-100", "hidden"],
      {
        label: {
          id: "Changer de clé",
          noTranslate: true,
        },
      },
    );

    const disconnectButton = new CustomButton(
      "account-disconnect-button",
      [new CustomText("span", "Se déconnecter", true, ["white"])],
      () => {
        getMessageManager().sendDisconnect();
      },
      ["btn-pill", "red", "width-100"],
      {
        label: {
          id: "Se déconnecter",
          noTranslate: true,
        },
      },
    );

    const actions: LisioComponent = new ElementsContainer(
      [this._changeKeyButton, disconnectButton],
      undefined,
      ["column", "full", "secondary", "align-items-center", "justify-content-center"],
    );

    this._currentKeyText = new CustomText(
      "p",
      "Clé actuelle : —",
      true,
      ["info-text", "text-center"],
      undefined,
      "current-key-value",
    );
    const currentKeyContainer: LisioComponent = new ElementsContainer(
      [this._currentKeyText],
      undefined,
      ["column", "full", "secondary"],
    );

    this._children.push(elementsTitleContainer, actions, currentKeyContainer);
    new LisioComponentStyles(ScreenNames[AccountScreen.name], accountScreenStyles);
  }
}

export { AccountScreen };
