package jgrasp_viewers_python;


import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import jgrasp.PluginOptOut;
import jgrasp.viewer.Util;
import jgrasp.viewer.ViewerCreateData;
import jgrasp.viewer.ViewerInfo;
import jgrasp.viewer.ViewerPriorityData;
import jgrasp.viewer.ViewerValueData;
import jgrasp.viewer.gui.BarGraphView;
import jgrasp.viewer.jgrdi.DebugContext;
import jgrasp.viewer.jgrdi.Value;


/** A viewer that displays a bar graph for arrays and lists of integral or floating point type. **/
public class _X_BarGraphView extends BarGraphView {

   /** Supported element types. **/
   private static final Collection<String> pythonSupportedTypes = new HashSet<>();

   /** Supported float types. **/
   private static final Collection<String> pythonFloatTypes = new HashSet<>();

   static {
      pythonSupportedTypes.add("(builtins).int");
      pythonSupportedTypes.add("(builtins).float");
   }


   /** Creates a new bar graph viewer.
    *
    * @param vcd viewer creation data. **/
   public _X_BarGraphView(final ViewerCreateData vcd) {
      this(vcd, EnumSet.noneOf(Flags.class));
   }


   /** Creates a new bar graph viewer.
    *
    * @param vcd viewer creation data.
    *
    * @param flags creation flags. **/
   public _X_BarGraphView(final ViewerCreateData vcd, final EnumSet<Flags> flags) {
      super(vcd, pythonSupportedTypes, flags);
      Value v = vcd.getValue();
      DebugContext dc = vcd.getDebugContext();
      if (!v.isArray(dc)) {
         throw new PluginOptOut();
      }
      // Type t = v.getType(dc);
      // try {
         // if (!t.isArray(dc)) {
            // if (v.isInstanceOf(dc, "java.util.List")) {
               // return;
            // }
         // }
         // else {
            // Type elementType = t.getArrayElementType(dc);
            // String tName = elementType.getName(dc);
            // if (supportedTypes.contains(tName) || "java.lang.Object".equals(tName)) {
               // return;
            // }
         // }
      // }
      // catch (ViewerException ignored) {
      // }
      //throw new PluginOptOut();
   }


   /** {@inheritDoc} **/
   @Override
   public void getInfo(final ViewerInfo vi) {
      vi.setShortDescription("Viewer for int and float containers");
      vi.setLongDescription("This viewer displays int and float container elements as a bar graph.");
   }


   /** {@inheritDoc} **/
   @Override
   public int getPriority(final ViewerPriorityData vpd) {
      // Value v = vpd.getValue();
      // DebugContext context = vpd.getDebugContext();
      // try {
         // if (v == null) {
            // return Integer.MIN_VALUE;
         // }
         // Type type = v.getType(context);
         // if (!type.isArray(context)) {
            // if (v.isInstanceOf(context, "java.util.List")) {
               // return -10;
            // }
            // return Integer.MIN_VALUE;
         // }
         // Type elementType = type.getArrayElementType(context);
         // String elementTypeName = elementType.getName(context);
         // if (!supportedTypes.contains(elementTypeName) && !"java.lang.Object".equals(elementTypeName)) {
            // return Integer.MIN_VALUE;
         // }
      // }
      // catch (ViewerException e) {
         // return Integer.MIN_VALUE;
      // }
      return -10;
   }
   
   
   /** {@inheritDoc} **/
   @Override
   public String getExpr(final ViewerValueData valueData, final int index, final DebugContext context) {
      return Util.getPythonExpr(getVIData(), valueData, index, context);
   }
}
