package net.miginfocom.layout;

import java.beans.Beans;
import java.beans.ExceptionListener;
import java.beans.Introspector;
import java.beans.PersistenceDelegate;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.OutputStream;
import java.util.IdentityHashMap;
import java.util.TreeSet;
import java.util.WeakHashMap;

public final class LayoutUtil {
   public static final int INF = 2097051;
   static final int NOT_SET = -2147471302;
   public static final int MIN = 0;
   public static final int PREF = 1;
   public static final int MAX = 2;
   private static volatile WeakHashMap<Object, String> CR_MAP = null;
   private static volatile WeakHashMap<Object, Boolean> DT_MAP = null;
   private static int eSz = 0;
   private static int globalDebugMillis = 0;
   public static final boolean HAS_BEANS = hasBeans();
   private static ByteArrayOutputStream writeOutputStream = null;
   private static byte[] readBuf = null;
   private static final IdentityHashMap<Object, Object> SER_MAP = new IdentityHashMap(2);

   private static boolean hasBeans() {
      try {
         LayoutUtil.class.getClassLoader().loadClass("java.beans.Beans");
         return true;
      } catch (Throwable var1) {
         return false;
      }
   }

   private LayoutUtil() {
   }

   public static String getVersion() {
      return "4.2";
   }

   public static int getGlobalDebugMillis() {
      return globalDebugMillis;
   }

   public static void setGlobalDebugMillis(int var0) {
      globalDebugMillis = var0;
   }

   public static void setDesignTime(ContainerWrapper var0, boolean var1) {
      if (DT_MAP == null) {
         DT_MAP = new WeakHashMap();
      }

      DT_MAP.put(var0 != null ? var0.getComponent() : null, var1);
   }

   public static boolean isDesignTime(ContainerWrapper var0) {
      if (DT_MAP == null) {
         return HAS_BEANS && Beans.isDesignTime();
      } else {
         if (var0 != null && !DT_MAP.containsKey(var0.getComponent())) {
            var0 = null;
         }

         Boolean var1 = (Boolean)DT_MAP.get(var0 != null ? var0.getComponent() : null);
         return var1 != null && var1;
      }
   }

   public static int getDesignTimeEmptySize() {
      return eSz;
   }

   public static void setDesignTimeEmptySize(int var0) {
      eSz = var0;
   }

   static void putCCString(Object var0, String var1) {
      if (var1 != null && var0 != null && isDesignTime((ContainerWrapper)null)) {
         if (CR_MAP == null) {
            CR_MAP = new WeakHashMap(64);
         }

         CR_MAP.put(var0, var1);
      }

   }

   static synchronized void setDelegate(Class var0, PersistenceDelegate var1) {
      try {
         Introspector.getBeanInfo(var0, 3).getBeanDescriptor().setValue("persistenceDelegate", var1);
      } catch (Exception var3) {
      }

   }

   static String getCCString(Object var0) {
      return CR_MAP != null ? (String)CR_MAP.get(var0) : null;
   }

   static void throwCC() {
      throw new IllegalStateException("setStoreConstraintData(true) must be set for strings to be saved.");
   }

   static int[] calculateSerial(int[][] var0, ResizeConstraint[] var1, Float[] var2, int var3, int var4) {
      float[] var5 = new float[var0.length];
      float var6 = 0.0F;

      int var7;
      for(var7 = 0; var7 < var0.length; ++var7) {
         if (var0[var7] != null) {
            float var8 = var0[var7][var3] != -2147471302 ? (float)var0[var7][var3] : 0.0F;
            int var9 = getBrokenBoundary(var8, var0[var7][0], var0[var7][2]);
            if (var9 != -2147471302) {
               var8 = (float)var9;
            }

            var6 += var8;
            var5[var7] = var8;
         }
      }

      var7 = Math.round(var6);
      if (var7 != var4 && var1 != null) {
         boolean var24 = var7 < var4;
         TreeSet var25 = new TreeSet();

         for(int var10 = 0; var10 < var0.length; ++var10) {
            ResizeConstraint var11 = (ResizeConstraint)getIndexSafe(var1, var10);
            if (var11 != null) {
               var25.add(var24 ? var11.growPrio : var11.shrinkPrio);
            }
         }

         Integer[] var26 = (Integer[])var25.toArray(new Integer[var25.size()]);

         for(int var27 = 0; var27 <= (var24 && var2 != null ? 1 : 0); ++var27) {
            for(int var12 = var26.length - 1; var12 >= 0; --var12) {
               int var13 = var26[var12];
               float var14 = 0.0F;
               Float[] var15 = new Float[var0.length];

               for(int var16 = 0; var16 < var0.length; ++var16) {
                  if (var0[var16] != null) {
                     ResizeConstraint var17 = (ResizeConstraint)getIndexSafe(var1, var16);
                     if (var17 != null) {
                        int var18 = var24 ? var17.growPrio : var17.shrinkPrio;
                        if (var13 == var18) {
                           if (var24) {
                              var15[var16] = var27 != 0 && var17.grow == null ? var2[var16 < var2.length ? var16 : var2.length - 1] : var17.grow;
                           } else {
                              var15[var16] = var17.shrink;
                           }

                           if (var15[var16] != null) {
                              var14 += var15[var16];
                           }
                        }
                     }
                  }
               }

               boolean var28;
               if (var14 > 0.0F) {
                  do {
                     float var29 = (float)var4 - var6;
                     var28 = false;
                     float var30 = 0.0F;

                     for(int var19 = 0; var19 < var0.length && var14 > 1.0E-4F; ++var19) {
                        Float var20 = var15[var19];
                        if (var20 != null) {
                           float var21 = var29 * var20 / var14;
                           float var22 = var5[var19] + var21;
                           if (var0[var19] != null) {
                              int var23 = getBrokenBoundary(var22, var0[var19][0], var0[var19][2]);
                              if (var23 != -2147471302) {
                                 var15[var19] = null;
                                 var28 = true;
                                 var30 += var20;
                                 var22 = (float)var23;
                                 var21 = var22 - var5[var19];
                              }
                           }

                           var5[var19] = var22;
                           var6 += var21;
                        }
                     }

                     var14 -= var30;
                  } while(var28);
               }
            }
         }
      }

      return roundSizes(var5);
   }

   static Object getIndexSafe(Object[] var0, int var1) {
      return var0 != null ? var0[var1 < var0.length ? var1 : var0.length - 1] : null;
   }

   private static int getBrokenBoundary(float var0, int var1, int var2) {
      if (var1 != -2147471302) {
         if (var0 < (float)var1) {
            return var1;
         }
      } else if (var0 < 0.0F) {
         return 0;
      }

      return var2 != -2147471302 && var0 > (float)var2 ? var2 : -2147471302;
   }

   static int sum(int[] var0, int var1, int var2) {
      int var3 = 0;
      int var4 = var1;

      for(int var5 = var1 + var2; var4 < var5; ++var4) {
         var3 += var0[var4];
      }

      return var3;
   }

   static int sum(int[] var0) {
      return sum(var0, 0, var0.length);
   }

   public static int getSizeSafe(int[] var0, int var1) {
      if (var0 != null && var0[var1] != -2147471302) {
         return var0[var1];
      } else {
         return var1 == 2 ? 2097051 : 0;
      }
   }

   static BoundSize derive(BoundSize var0, UnitValue var1, UnitValue var2, UnitValue var3) {
      return var0 != null && !var0.isUnset() ? new BoundSize(var1 != null ? var1 : var0.getMin(), var2 != null ? var2 : var0.getPreferred(), var3 != null ? var3 : var0.getMax(), var0.getGapPush(), (String)null) : new BoundSize(var1, var2, var3, (String)null);
   }

   public static boolean isLeftToRight(LC var0, ContainerWrapper var1) {
      if (var0 != null && var0.getLeftToRight() != null) {
         return var0.getLeftToRight();
      } else {
         return var1 == null || var1.isLeftToRight();
      }
   }

   static int[] roundSizes(float[] var0) {
      int[] var1 = new int[var0.length];
      float var2 = 0.0F;

      for(int var3 = 0; var3 < var1.length; ++var3) {
         int var4 = (int)(var2 + 0.5F);
         var2 += var0[var3];
         var1[var3] = (int)(var2 + 0.5F) - var4;
      }

      return var1;
   }

   static boolean equals(Object var0, Object var1) {
      return var0 == var1 || var0 != null && var1 != null && var0.equals(var1);
   }

   static UnitValue getInsets(LC var0, int var1, boolean var2) {
      UnitValue[] var3 = var0.getInsets();
      return var3 != null && var3[var1] != null ? var3[var1] : (var2 ? PlatformDefaults.getPanelInsets(var1) : UnitValue.ZERO);
   }

   static void writeXMLObject(OutputStream var0, Object var1, ExceptionListener var2) {
      ClassLoader var3 = Thread.currentThread().getContextClassLoader();
      Thread.currentThread().setContextClassLoader(LayoutUtil.class.getClassLoader());
      XMLEncoder var4 = new XMLEncoder(var0);
      if (var2 != null) {
         var4.setExceptionListener(var2);
      }

      var4.writeObject(var1);
      var4.close();
      Thread.currentThread().setContextClassLoader(var3);
   }

   public static synchronized void writeAsXML(ObjectOutput var0, Object var1) throws IOException {
      if (writeOutputStream == null) {
         writeOutputStream = new ByteArrayOutputStream(16384);
      }

      writeOutputStream.reset();
      writeXMLObject(writeOutputStream, var1, new ExceptionListener() {
         public void exceptionThrown(Exception var1) {
            var1.printStackTrace();
         }
      });
      byte[] var2 = writeOutputStream.toByteArray();
      var0.writeInt(var2.length);
      var0.write(var2);
   }

   public static synchronized Object readAsXML(ObjectInput var0) throws IOException {
      if (readBuf == null) {
         readBuf = new byte[16384];
      }

      Thread var1 = Thread.currentThread();
      ClassLoader var2 = null;

      try {
         var2 = var1.getContextClassLoader();
         var1.setContextClassLoader(LayoutUtil.class.getClassLoader());
      } catch (SecurityException var6) {
      }

      Object var3 = null;

      try {
         int var4 = var0.readInt();
         if (var4 > readBuf.length) {
            readBuf = new byte[var4];
         }

         var0.readFully(readBuf, 0, var4);
         var3 = (new XMLDecoder(new ByteArrayInputStream(readBuf, 0, var4))).readObject();
      } catch (EOFException var5) {
      }

      if (var2 != null) {
         var1.setContextClassLoader(var2);
      }

      return var3;
   }

   public static void setSerializedObject(Object var0, Object var1) {
      synchronized(SER_MAP) {
         SER_MAP.put(var0, var1);
      }
   }

   public static Object getSerializedObject(Object var0) {
      synchronized(SER_MAP) {
         return SER_MAP.remove(var0);
      }
   }
}
