package net.miginfocom.layout;

import java.beans.Encoder;
import java.beans.Expression;
import java.beans.PersistenceDelegate;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

public final class UnitValue implements Serializable {
   private static final HashMap<String, Integer> UNIT_MAP = new HashMap(32);
   private static final ArrayList<UnitConverter> CONVERTERS = new ArrayList();
   public static final int STATIC = 100;
   public static final int ADD = 101;
   public static final int SUB = 102;
   public static final int MUL = 103;
   public static final int DIV = 104;
   public static final int MIN = 105;
   public static final int MAX = 106;
   public static final int MID = 107;
   public static final int PIXEL = 0;
   public static final int LPX = 1;
   public static final int LPY = 2;
   public static final int MM = 3;
   public static final int CM = 4;
   public static final int INCH = 5;
   public static final int PERCENT = 6;
   public static final int PT = 7;
   public static final int SPX = 8;
   public static final int SPY = 9;
   public static final int ALIGN = 12;
   public static final int MIN_SIZE = 13;
   public static final int PREF_SIZE = 14;
   public static final int MAX_SIZE = 15;
   public static final int BUTTON = 16;
   public static final int LINK_X = 18;
   public static final int LINK_Y = 19;
   public static final int LINK_W = 20;
   public static final int LINK_H = 21;
   public static final int LINK_X2 = 22;
   public static final int LINK_Y2 = 23;
   public static final int LINK_XPOS = 24;
   public static final int LINK_YPOS = 25;
   public static final int LOOKUP = 26;
   public static final int LABEL_ALIGN = 27;
   private static final int IDENTITY = -1;
   static final UnitValue ZERO;
   static final UnitValue TOP;
   static final UnitValue LEADING;
   static final UnitValue LEFT;
   static final UnitValue CENTER;
   static final UnitValue TRAILING;
   static final UnitValue RIGHT;
   static final UnitValue BOTTOM;
   static final UnitValue LABEL;
   static final UnitValue INF;
   static final UnitValue BASELINE_IDENTITY;
   private final transient float value;
   private final transient int unit;
   private final transient int oper;
   private final transient String unitStr;
   private transient String linkId;
   private final transient boolean isHor;
   private final transient UnitValue[] subUnits;
   private static final float[] SCALE;
   private static final long serialVersionUID = 1L;

   public UnitValue(float var1) {
      this(var1, (String)null, 0, true, 100, (UnitValue)null, (UnitValue)null, var1 + "px");
   }

   public UnitValue(float var1, int var2, String var3) {
      this(var1, (String)null, var2, true, 100, (UnitValue)null, (UnitValue)null, var3);
   }

   UnitValue(float var1, String var2, boolean var3, int var4, String var5) {
      this(var1, var2, -1, var3, var4, (UnitValue)null, (UnitValue)null, var5);
   }

   UnitValue(boolean var1, int var2, UnitValue var3, UnitValue var4, String var5) {
      this(0.0F, "", -1, var1, var2, var3, var4, var5);
      if (var3 == null || var4 == null) {
         throw new IllegalArgumentException("Sub units is null!");
      }
   }

   private UnitValue(float var1, String var2, int var3, boolean var4, int var5, UnitValue var6, UnitValue var7, String var8) {
      this.linkId = null;
      if (var5 >= 100 && var5 <= 107) {
         if (var5 < 101 || var5 > 107 || var6 != null && var7 != null) {
            this.value = var1;
            this.oper = var5;
            this.isHor = var4;
            this.unitStr = var2;
            this.unit = var2 != null ? this.parseUnitString() : var3;
            this.subUnits = var6 != null && var7 != null ? new UnitValue[]{var6, var7} : null;
            LayoutUtil.putCCString(this, var8);
         } else {
            throw new IllegalArgumentException(var5 + " Operation may not have null sub-UnitValues.");
         }
      } else {
         throw new IllegalArgumentException("Unknown Operation: " + var5);
      }
   }

   public final int getPixels(float var1, ContainerWrapper var2, ComponentWrapper var3) {
      return Math.round(this.getPixelsExact(var1, var2, var3));
   }

   public final float getPixelsExact(float var1, ContainerWrapper var2, ComponentWrapper var3) {
      if (var2 == null) {
         return 1.0F;
      } else {
         float var4;
         if (this.oper == 100) {
            switch(this.unit) {
            case -1:
            case 10:
            case 11:
            case 17:
            default:
               throw new IllegalArgumentException("Unknown/illegal unit: " + this.unit + ", unitStr: " + this.unitStr);
            case 0:
               return this.value;
            case 1:
            case 2:
               return var2.getPixelUnitFactor(this.unit == 1) * this.value;
            case 3:
            case 4:
            case 5:
            case 7:
               var4 = SCALE[this.unit - 3];
               Float var10 = this.isHor ? PlatformDefaults.getHorizontalScaleFactor() : PlatformDefaults.getVerticalScaleFactor();
               if (var10 != null) {
                  var4 *= var10;
               }

               return (float)(this.isHor ? var2.getHorizontalScreenDPI() : var2.getVerticalScreenDPI()) * this.value / var4;
            case 6:
               return this.value * var1 * 0.01F;
            case 8:
            case 9:
               return (float)(this.unit == 8 ? var2.getScreenWidth() : var2.getScreenHeight()) * this.value * 0.01F;
            case 12:
               Integer var6 = LinkHandler.getValue(var2.getLayout(), "visual", this.isHor ? 0 : 1);
               Integer var7 = LinkHandler.getValue(var2.getLayout(), "visual", this.isHor ? 2 : 3);
               if (var6 != null && var7 != null) {
                  return this.value * ((float)Math.max(0, var7) - var1) + (float)var6;
               }

               return 0.0F;
            case 13:
               if (var3 == null) {
                  return 0.0F;
               }

               return this.isHor ? (float)var3.getMinimumWidth(var3.getHeight()) : (float)var3.getMinimumHeight(var3.getWidth());
            case 14:
               if (var3 == null) {
                  return 0.0F;
               }

               return this.isHor ? (float)var3.getPreferredWidth(var3.getHeight()) : (float)var3.getPreferredHeight(var3.getWidth());
            case 15:
               if (var3 == null) {
                  return 0.0F;
               }

               return this.isHor ? (float)var3.getMaximumWidth(var3.getHeight()) : (float)var3.getMaximumHeight(var3.getWidth());
            case 16:
               return (float)PlatformDefaults.getMinimumButtonWidth().getPixels(var1, var2, var3);
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            case 23:
            case 24:
            case 25:
               Integer var8 = LinkHandler.getValue(var2.getLayout(), this.getLinkTargetId(), this.unit - (this.unit >= 24 ? 24 : 18));
               if (var8 == null) {
                  return 0.0F;
               } else if (this.unit == 24) {
                  return (float)(var2.getScreenLocationX() + var8);
               } else {
                  if (this.unit == 25) {
                     return (float)(var2.getScreenLocationY() + var8);
                  }

                  return (float)var8;
               }
            case 26:
               float var9 = this.lookup(var1, var2, var3);
               if (var9 != -8.7654312E7F) {
                  return var9;
               }
            case 27:
               return PlatformDefaults.getLabelAlignPercentage() * var1;
            }
         } else {
            if (this.subUnits != null && this.subUnits.length == 2) {
               var4 = this.subUnits[0].getPixelsExact(var1, var2, var3);
               float var5 = this.subUnits[1].getPixelsExact(var1, var2, var3);
               switch(this.oper) {
               case 101:
                  return var4 + var5;
               case 102:
                  return var4 - var5;
               case 103:
                  return var4 * var5;
               case 104:
                  return var4 / var5;
               case 105:
                  return var4 < var5 ? var4 : var5;
               case 106:
                  return var4 > var5 ? var4 : var5;
               case 107:
                  return (var4 + var5) * 0.5F;
               }
            }

            throw new IllegalArgumentException("Internal: Unknown Oper: " + this.oper);
         }
      }
   }

   private float lookup(float var1, ContainerWrapper var2, ComponentWrapper var3) {
      float var4 = -8.7654312E7F;

      for(int var5 = CONVERTERS.size() - 1; var5 >= 0; --var5) {
         var4 = (float)((UnitConverter)CONVERTERS.get(var5)).convertToPixels(this.value, this.unitStr, this.isHor, var1, var2, var3);
         if (var4 != -8.7654312E7F) {
            return var4;
         }
      }

      return (float)PlatformDefaults.convertToPixels(this.value, this.unitStr, this.isHor, var1, var2, var3);
   }

   private int parseUnitString() {
      int var1 = this.unitStr.length();
      if (var1 == 0) {
         return this.isHor ? PlatformDefaults.getDefaultHorizontalUnit() : PlatformDefaults.getDefaultVerticalUnit();
      } else {
         Integer var2 = (Integer)UNIT_MAP.get(this.unitStr);
         if (var2 != null) {
            return var2;
         } else if (this.unitStr.equals("lp")) {
            return this.isHor ? 1 : 2;
         } else if (this.unitStr.equals("sp")) {
            return this.isHor ? 8 : 9;
         } else if (this.lookup(0.0F, (ContainerWrapper)null, (ComponentWrapper)null) != -8.7654312E7F) {
            return 26;
         } else {
            int var3 = this.unitStr.indexOf(46);
            if (var3 != -1) {
               this.linkId = this.unitStr.substring(0, var3);
               String var4 = this.unitStr.substring(var3 + 1);
               if (var4.equals("x")) {
                  return 18;
               } else if (var4.equals("y")) {
                  return 19;
               } else if (!var4.equals("w") && !var4.equals("width")) {
                  if (!var4.equals("h") && !var4.equals("height")) {
                     if (var4.equals("x2")) {
                        return 22;
                     } else if (var4.equals("y2")) {
                        return 23;
                     } else if (var4.equals("xpos")) {
                        return 24;
                     } else if (var4.equals("ypos")) {
                        return 25;
                     } else {
                        throw new IllegalArgumentException("Unknown keyword: " + this.unitStr);
                     }
                  } else {
                     return 21;
                  }
               } else {
                  return 20;
               }
            } else {
               throw new IllegalArgumentException("Unknown keyword: " + this.unitStr);
            }
         }
      }
   }

   final boolean isLinked() {
      return this.linkId != null;
   }

   final boolean isLinkedDeep() {
      if (this.subUnits == null) {
         return this.linkId != null;
      } else {
         UnitValue[] var1 = this.subUnits;
         int var2 = var1.length;

         for(int var3 = 0; var3 < var2; ++var3) {
            UnitValue var4 = var1[var3];
            if (var4.isLinkedDeep()) {
               return true;
            }
         }

         return false;
      }
   }

   final String getLinkTargetId() {
      return this.linkId;
   }

   final UnitValue getSubUnitValue(int var1) {
      return this.subUnits[var1];
   }

   final int getSubUnitCount() {
      return this.subUnits != null ? this.subUnits.length : 0;
   }

   public final UnitValue[] getSubUnits() {
      return this.subUnits != null ? (UnitValue[])this.subUnits.clone() : null;
   }

   public final int getUnit() {
      return this.unit;
   }

   public final String getUnitString() {
      return this.unitStr;
   }

   public final int getOperation() {
      return this.oper;
   }

   public final float getValue() {
      return this.value;
   }

   public final boolean isHorizontal() {
      return this.isHor;
   }

   public final String toString() {
      return this.getClass().getName() + ". Value=" + this.value + ", unit=" + this.unit + ", unitString: " + this.unitStr + ", oper=" + this.oper + ", isHor: " + this.isHor;
   }

   public final String getConstraintString() {
      return LayoutUtil.getCCString(this);
   }

   public final int hashCode() {
      return (int)(this.value * 12345.0F) + (this.oper >>> 5) + this.unit >>> 17;
   }

   public static synchronized void addGlobalUnitConverter(UnitConverter var0) {
      if (var0 == null) {
         throw new NullPointerException();
      } else {
         CONVERTERS.add(var0);
      }
   }

   public static synchronized boolean removeGlobalUnitConverter(UnitConverter var0) {
      return CONVERTERS.remove(var0);
   }

   public static synchronized UnitConverter[] getGlobalUnitConverters() {
      return (UnitConverter[])CONVERTERS.toArray(new UnitConverter[CONVERTERS.size()]);
   }

   /** @deprecated */
   public static int getDefaultUnit() {
      return PlatformDefaults.getDefaultHorizontalUnit();
   }

   /** @deprecated */
   public static void setDefaultUnit(int var0) {
      PlatformDefaults.setDefaultHorizontalUnit(var0);
      PlatformDefaults.setDefaultVerticalUnit(var0);
   }

   private Object readResolve() throws ObjectStreamException {
      return LayoutUtil.getSerializedObject(this);
   }

   private void writeObject(ObjectOutputStream var1) throws IOException {
      if (this.getClass() == UnitValue.class) {
         LayoutUtil.writeAsXML(var1, this);
      }

   }

   private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
      LayoutUtil.setSerializedObject(this, LayoutUtil.readAsXML(var1));
   }

   static {
      UNIT_MAP.put("px", 0);
      UNIT_MAP.put("lpx", 1);
      UNIT_MAP.put("lpy", 2);
      UNIT_MAP.put("%", 6);
      UNIT_MAP.put("cm", 4);
      UNIT_MAP.put("in", 5);
      UNIT_MAP.put("spx", 8);
      UNIT_MAP.put("spy", 9);
      UNIT_MAP.put("al", 12);
      UNIT_MAP.put("mm", 3);
      UNIT_MAP.put("pt", 7);
      UNIT_MAP.put("min", 13);
      UNIT_MAP.put("minimum", 13);
      UNIT_MAP.put("p", 14);
      UNIT_MAP.put("pref", 14);
      UNIT_MAP.put("max", 15);
      UNIT_MAP.put("maximum", 15);
      UNIT_MAP.put("button", 16);
      UNIT_MAP.put("label", 27);
      ZERO = new UnitValue(0.0F, (String)null, 0, true, 100, (UnitValue)null, (UnitValue)null, "0px");
      TOP = new UnitValue(0.0F, (String)null, 6, false, 100, (UnitValue)null, (UnitValue)null, "top");
      LEADING = new UnitValue(0.0F, (String)null, 6, true, 100, (UnitValue)null, (UnitValue)null, "leading");
      LEFT = new UnitValue(0.0F, (String)null, 6, true, 100, (UnitValue)null, (UnitValue)null, "left");
      CENTER = new UnitValue(50.0F, (String)null, 6, true, 100, (UnitValue)null, (UnitValue)null, "center");
      TRAILING = new UnitValue(100.0F, (String)null, 6, true, 100, (UnitValue)null, (UnitValue)null, "trailing");
      RIGHT = new UnitValue(100.0F, (String)null, 6, true, 100, (UnitValue)null, (UnitValue)null, "right");
      BOTTOM = new UnitValue(100.0F, (String)null, 6, false, 100, (UnitValue)null, (UnitValue)null, "bottom");
      LABEL = new UnitValue(0.0F, (String)null, 27, false, 100, (UnitValue)null, (UnitValue)null, "label");
      INF = new UnitValue(2097051.0F, (String)null, 0, true, 100, (UnitValue)null, (UnitValue)null, "inf");
      BASELINE_IDENTITY = new UnitValue(0.0F, (String)null, -1, false, 100, (UnitValue)null, (UnitValue)null, "baseline");
      SCALE = new float[]{25.4F, 2.54F, 1.0F, 0.0F, 72.0F};
      if (LayoutUtil.HAS_BEANS) {
         LayoutUtil.setDelegate(UnitValue.class, new PersistenceDelegate() {
            protected Expression instantiate(Object var1, Encoder var2) {
               UnitValue var3 = (UnitValue)var1;
               String var4 = var3.getConstraintString();
               if (var4 == null) {
                  throw new IllegalStateException("Design time must be on to use XML persistence. See LayoutUtil.");
               } else {
                  return new Expression(var1, ConstraintParser.class, "parseUnitValueOrAlign", new Object[]{var3.getConstraintString(), var3.isHorizontal() ? Boolean.TRUE : Boolean.FALSE, null});
               }
            }
         });
      }

   }
}
