import { EMPTY_STRING } from '../../../../mel_metapage/js/lib/constants/constants.js';
import {
  SELECTOR_CHECKBOX_CHANNEL,
  SELECTOR_DIV_CHANNEL,
  SELECTOR_DIV_WSP,
} from '../consts.js';
import { ACheckBox } from './abstract/ACheckBox.js';
export { ChannelPart };

/**
 * Classes lié au choix des canaux lié à la visio si un plugin compatible existe
 * @module Visio/Parts/Channel
 * @local ChannelPart
 */

/**
 * @class
 * @classdesc Récupère le champs qui correspond à l'état de la checkbox
 * @extends ACheckBox
 * @frommodule Visio/Abstract/Checkbox
 */
class ChannelPart extends ACheckBox {
  constructor() {
    super(SELECTOR_CHECKBOX_CHANNEL);
    this._init()._setup()._main();
  }

  /**
   * @private
   * @returns
   */
  _init() {
    /**
     * Champ qui correspond à l'état de la checkbox
     * @type {external:jQuery}
     * @readonly
     * @member
     */
    this.$field = null;

    return this;
  }

  /**
   * @private
   * @returns
   */
  _setup() {
    Object.defineProperty(this, '$field', {
      get: () =>
        this.is_checked()
          ? $(`${SELECTOR_DIV_WSP} select`)
          : $(`${SELECTOR_DIV_CHANNEL} select`),
    });

    return this;
  }

  /**
   * @private
   */
  _main() {
    this._p_on_change.push(this._on_checkbox_change.bind(this));
    this._p_on_change.call(this.is_checked());

    //Gestion des champs si les plugins n'éxistent pas
    if (
      rcmail.env['visio.has_channel'] !== true ||
      rcmail.env['visio.has_wsp'] !== true
    ) {
      this._p_$checkbox.parent().hide();

      if (rcmail.env['visio.has_channel'] !== true) {
        $(SELECTOR_DIV_CHANNEL).remove();
        this._p_$checkbox.prop('checked', true);
        $(SELECTOR_DIV_WSP)
          .css('display', EMPTY_STRING)
          .find('.title-conf-item')
          .text('Attacher à un espace de travail ?')
          .parent()
          .find('select')
          .prepend('<option value="" selected>Ne pas attacher</option>');
      }

      if (rcmail.env['visio.has_wsp'] !== true) {
        this._p_$checkbox.prop('checked', false);
        $(SELECTOR_DIV_WSP).remove();
      }

      try {
        this.value();
      } catch (error) {
        this.value = () => EMPTY_STRING;
      }
    }
  }

  /**
   * Action à faire lorsque la checkbox change d'état
   * @param {!boolean} state
   * @package
   */
  _on_checkbox_change(state) {
    if (state) {
      $(SELECTOR_DIV_WSP).show();
      $(SELECTOR_DIV_CHANNEL).hide();
    } else {
      $(SELECTOR_DIV_WSP).hide();
      $(SELECTOR_DIV_CHANNEL).show();
    }
  }

  /**
   * Désactive les champs
   * @override
   */
  disable() {
    super.disable();
    this._p_disable(this.$field);
  }

  /**
   * Active les champs
   * @override
   */
  enable() {
    super.enable();
    this._p_enable(this.$field);
  }

  /**
   * Récupère la valeur du champ
   * @override
   * @returns {string}
   */
  value() {
    return this.$field?.val?.() || EMPTY_STRING;
  }
}