package com.airlock.iam.admin.application.configuration.generic.ui;

import com.airlock.iam.plugin.api.config.ConfigurationContext;
import com.airlock.iam.plugin.api.config.ConfigurationEnvironment;
import com.airlock.iam.plugin.api.config.PluginConfiguration;
import com.airlock.iam.plugin.api.exception.PropertyConfigurationException;
import com.airlock.iam.plugin.api.valuereader.BooleanPropertyValueReader;

public interface TokenControllerUiElementConfig {
   String LABEL_DESCRIPTION = "Label for the field. The UI treats it as a key to translate.\nIf there is no translation, the label is shown in the UI as is.";
   String PROPERTY_DESCRIPTION = "The property to use as value for this field.\n<p>The referenced property must be available in the <code>attributes</code> value of the generic token REST call response.\nIf the property is nested, e.g. inside the <code>contextData</code> key, it can be referenced with dot notation (see example values).</p>";
   String PROPERTY_ID_DESCRIPTION = "<p>The ID of the response is referenced by using the reserved value <code>@id</code>.</p>";
   String PLACEHOLDER_DESCRIPTION = "Displays a placeholder when the field has no value.\nThe placeholder is not interpreted as value and disappears when typing in the field.";
   String REQUIRED_DESCRIPTION = "Whether this field must have a value when the token is added or updated.\nRequired fields are marked with an asterisk.";
   String READ_ONLY_DESCRIPTION = "If enabled, the field is read-only and cannot be altered by administrators via the UI.";
   String READ_ONLY_DISPLAY_NAME = "Read-only";
   String HIDE_IF_EMPTY_DESCRIPTION = "If enabled, this UI element is hidden if it has no value.";
   String HIDE_IF_EMPTY_DISPLAY_NAME = "Hide If Empty";
   String VALIDATIONS_DESCRIPTION = "Validates the input against the defined expression.\nIf validation fails, an error on this field is shown to the administrator\nand the token cannot be added or updated.";

   static void validateHideIfEmptyAllowed(
      PluginConfiguration pc, ConfigurationEnvironment env, ConfigurationContext context, String readOnlyProperty, String hideIfEmptyProperty
   ) throws PropertyConfigurationException {
      boolean isReadOnly = BooleanPropertyValueReader.effectiveBooleanPropertyValue(pc, readOnlyProperty, env, context);
      boolean isHideIfEmpty = BooleanPropertyValueReader.effectiveBooleanPropertyValue(pc, hideIfEmptyProperty, env, context);
      if (!isReadOnly && isHideIfEmpty) {
         throw new PropertyConfigurationException(
            pc, env, context, "\"Hide If Empty\" can only be enabled if \"Read-only\" is enabled.", new String[]{hideIfEmptyProperty}
         );
      }
   }
}
