package com.airlock.iam.plugin.configeditor.rendering;

import com.airlock.iam.plugin.configeditor.data.PropertyAdapter;
import com.airlock.iam.plugin.framework.misc.ioc.PluginLoader;
import com.google.common.base.Joiner;
import com.vaadin.v7.ui.Field;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Set;

public abstract class VaadinPropertyRenderer<PROPERTY extends Annotation, VALUE> {
   public abstract Field<VALUE> createField(PropertyAdapter<PROPERTY> var1);

   @SuppressFBWarnings("POTENTIAL_XML_INJECTION")
   public static String renderDescription(
      String propertyDisplayName, String text, String[] exampleValues, String defaultValue, Set<String> requiredLicenseTags, String propertyName
   ) {
      StringBuilder sb = new StringBuilder(57);
      sb.append("<h3>Configuration Property</h3>");
      sb.append(propertyDisplayName);
      sb.append("<h3>Description</h3>");
      sb.append(text);
      if (exampleValues != null && exampleValues.length > 0) {
         sb.append("<h3>Example values</h3><ul>");

         for (String example : exampleValues) {
            sb.append("<li>");
            sb.append(example);
            sb.append("</li>");
         }

         sb.append("</ul>");
      }

      if (defaultValue != null && !defaultValue.isEmpty()) {
         sb.append("<p><b>Default value:</b> ");
         sb.append(defaultValue);
         sb.append("</p>");
      }

      sb.append("<h3>Property name</h3>");
      sb.append(propertyName);
      if (!requiredLicenseTags.isEmpty()) {
         sb.append("<p><b>Requires one of the following license tags:</b> ");
         sb.append(Joiner.on(", ").join(requiredLicenseTags));
         sb.append("</p>");
      }

      return sb.toString();
   }

   @SuppressFBWarnings("POTENTIAL_XML_INJECTION")
   public static String renderDescription(
      String propertyDisplayName, String text, int[] exampleValues, int defaultValue, Set<String> requiredLicenseTags, String propertyName
   ) {
      return renderDescription(
         propertyDisplayName,
         text,
         Arrays.stream(exampleValues).mapToObj(Integer::toString).toArray(String[]::new),
         defaultValue == 2147483646 ? null : Integer.toString(defaultValue),
         requiredLicenseTags,
         propertyName
      );
   }

   @SuppressFBWarnings("POTENTIAL_XML_INJECTION")
   public static String renderDescription(
      String propertyDisplayName, String text, long[] exampleValues, long defaultValue, Set<String> requiredLicenseTags, String propertyName
   ) {
      return renderDescription(
         propertyDisplayName,
         text,
         Arrays.stream(exampleValues).mapToObj(Long::toString).toArray(String[]::new),
         defaultValue == 9223372036854775806L ? null : Long.toString(defaultValue),
         requiredLicenseTags,
         propertyName
      );
   }

   @SuppressFBWarnings("POTENTIAL_XML_INJECTION")
   public static String renderDescription(
      String propertyDisplayName, String text, double[] exampleValues, double defaultValue, Set<String> requiredLicenseTags, String propertyName
   ) {
      return renderDescription(
         propertyDisplayName,
         text,
         Arrays.stream(exampleValues).mapToObj(Double::toString).toArray(String[]::new),
         Double.isNaN(defaultValue) ? null : Double.toString(defaultValue),
         requiredLicenseTags,
         propertyName
      );
   }

   @SuppressFBWarnings("POTENTIAL_XML_INJECTION")
   public static String renderDescription(String propertyDisplayName, String text, Class<?> defaultValue, Set<String> requiredLicenseTags, String propertyName) {
      String defaultValueString = null;
      if (defaultValue != null && !defaultValue.getName().equals(Object.class.getName())) {
         defaultValueString = PluginLoader.getDisplayName(defaultValue);
      }

      return renderDescription(propertyDisplayName, text, new String[0], defaultValueString, requiredLicenseTags, propertyName);
   }
}
