package csa.tiletools.worldgen.mapgen.miny.types;

import csa.tiletools.worldgen.mapgen.processing.CSAProcessing;
import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;

public class MinyGradient
{
  private int _defaultValue;
  private boolean _useLookupTable;
  private int[] _lookupTable;
  private int _interpolationMethod;
  private ArrayList<VarKey> _keysList;

  public MinyGradient(int paramInt)
  {
    this._defaultValue = paramInt;
    this._useLookupTable = false;
    this._keysList = new ArrayList();
    this._interpolationMethod = 0;
  }

  public int getInterpolation()
  {
    return this._interpolationMethod;
  }

  public void setInterpolation(int paramInt)
  {
    this._interpolationMethod = (paramInt % 5);
  }

  public int size()
  {
    return this._keysList.size();
  }

  public void clear()
  {
    this._keysList.clear();
    update();
  }

  public void remove(int paramInt)
  {
    this._keysList.remove(paramInt);
    update();
  }

  public int add(float paramFloat, int paramInt)
  {
    paramFloat = CSAProcessing.constrain(paramFloat, 0.0F, 1.0F);
    VarKey localVarKey1 = new VarKey();
    localVarKey1.position = paramFloat;
    localVarKey1.value = paramInt;
    int i = this._keysList.size();
    for (int j = 0; j < i; j++)
    {
      VarKey localVarKey2 = (VarKey)this._keysList.get(j);
      if (localVarKey2.position == paramFloat)
      {
        localVarKey2.value = paramInt;
        update();
        return j;
      }
    }
    this._keysList.add(localVarKey1);
    update();
    return i;
  }

  public void set(int paramInt1, float paramFloat, int paramInt2)
  {
    paramFloat = CSAProcessing.constrain(paramFloat, 0.0F, 1.0F);
    VarKey localVarKey = (VarKey)this._keysList.get(paramInt1);
    localVarKey.position = paramFloat;
    localVarKey.value = paramInt2;
    update();
  }

  public void setValue(int paramInt1, int paramInt2)
  {
    ((VarKey)this._keysList.get(paramInt1)).value = paramInt2;
    update();
  }

  public void setPosition(int paramInt, float paramFloat)
  {
    paramFloat = CSAProcessing.constrain(paramFloat, 0.0F, 1.0F);
    ((VarKey)this._keysList.get(paramInt)).position = paramFloat;
    update();
  }

  public float getPosition(int paramInt)
  {
    return ((VarKey)this._keysList.get(paramInt)).position;
  }

  public int getValue(int paramInt)
  {
    return ((VarKey)this._keysList.get(paramInt)).value;
  }

  public int getPrev(float paramFloat)
  {
    int i = this._keysList.size();
    int j = -1;
    float f1 = paramFloat;
    for (int k = 0; k < i; k++)
    {
      float f2 = getPosition(k);
      if ((f2 < paramFloat) && ((f1 <= f2) || (f1 == paramFloat)))
      {
        f1 = f2;
        j = k;
      }
    }
    return j;
  }

  public int getPrev(int paramInt)
  {
    return getPrev(getPosition(paramInt));
  }

  public int getNext(float paramFloat)
  {
    int i = this._keysList.size();
    int j = -1;
    float f1 = paramFloat;
    for (int k = 0; k < i; k++)
    {
      float f2 = getPosition(k);
      if ((f2 > paramFloat) && ((f1 >= f2) || (f1 == paramFloat)))
      {
        f1 = f2;
        j = k;
      }
    }
    return j;
  }

  public int getNext(int paramInt)
  {
    return getNext(getPosition(paramInt));
  }

  protected int lerpColor(int paramInt1, int paramInt2, float paramFloat)
  {
    int i = paramInt1 >> 24 & 0xFF;
    int j = paramInt2 >> 24 & 0xFF;
    int k = paramInt1 >> 16 & 0xFF;
    int m = paramInt2 >> 16 & 0xFF;
    int n = paramInt1 >> 8 & 0xFF;
    int i1 = paramInt2 >> 8 & 0xFF;
    int i2 = paramInt1 & 0xFF;
    int i3 = paramInt2 & 0xFF;
    float f = 1.0F - paramFloat;
    int i4 = (int)(i * f + j * paramFloat);
    int i5 = (int)(k * f + m * paramFloat);
    int i6 = (int)(n * f + i1 * paramFloat);
    int i7 = (int)(i2 * f + i3 * paramFloat);
    return (i4 << 24) + (i5 << 16) + (i6 << 8) + i7;
  }

  public int get(float paramFloat)
  {
    int i = this._keysList.size();
    if (i == 0)
      return this._defaultValue;
    if (i == 1)
      return ((VarKey)this._keysList.get(0)).value;
    if (this._useLookupTable)
    {
      int j = CSAProcessing.constrain(CSAProcessing.floor(paramFloat * this._lookupTable.length), 0, this._lookupTable.length - 1);
      return this._lookupTable[j];
    }
    TreeMap localTreeMap = getSortedMap();
    if ((this._interpolationMethod != 4) && (localTreeMap.containsKey(Float.valueOf(paramFloat))))
      return ((Integer)localTreeMap.get(Float.valueOf(paramFloat))).intValue();
    SortedMap localSortedMap1 = localTreeMap.headMap(Float.valueOf(paramFloat));
    if (localSortedMap1.size() == 0)
      return ((Integer)localTreeMap.get(localTreeMap.firstKey())).intValue();
    SortedMap localSortedMap2 = localTreeMap.tailMap(Float.valueOf(paramFloat));
    if (localSortedMap2.size() == 0)
      return ((Integer)localTreeMap.get(localTreeMap.lastKey())).intValue();
    float f1 = ((Float)localSortedMap1.lastKey()).floatValue();
    float f2 = ((Float)localSortedMap2.firstKey()).floatValue();
    float f3 = (paramFloat - f1) / (f2 - f1);
    int k = ((Integer)localTreeMap.get(Float.valueOf(f1))).intValue();
    int m = ((Integer)localTreeMap.get(Float.valueOf(f2))).intValue();
    switch (this._interpolationMethod)
    {
    case 0:
    default:
      return lerpColor(k, m, (1.0F - CSAProcessing.cos(f3 * 3.141593F)) / 2.0F);
    case 1:
      return lerpColor(k, m, f3);
    case 2:
      return m;
    case 3:
      return k;
    case 4:
    }
    return f3 < 0.5D ? k : m;
  }

  public void useLookupTable(int paramInt)
  {
    this._lookupTable = new int[paramInt];
    this._useLookupTable = true;
    update();
  }

  public int[] getTable(int paramInt)
  {
    int[] arrayOfInt = new int[paramInt];
    boolean bool = this._useLookupTable;
    this._useLookupTable = false;
    for (int i = 0; i < paramInt; i++)
    {
      float f = i / (paramInt - 1);
      arrayOfInt[i] = get(f);
    }
    this._useLookupTable = bool;
    return arrayOfInt;
  }

  private TreeMap<Float, Integer> getSortedMap()
  {
    int i = this._keysList.size();
    if (i == 0)
      return null;
    TreeMap localTreeMap = new TreeMap();
    for (int j = 0; j < i; j++)
    {
      VarKey localVarKey = (VarKey)this._keysList.get(j);
      localTreeMap.put(Float.valueOf(localVarKey.position), Integer.valueOf(localVarKey.value));
    }
    return localTreeMap;
  }

  private void update()
  {
    if (!this._useLookupTable)
      return;
    this._useLookupTable = false;
    for (int i = 0; i < this._lookupTable.length; i++)
    {
      float f = i / (this._lookupTable.length - 1);
      this._lookupTable[i] = get(f);
    }
    this._useLookupTable = true;
  }

  class VarKey
  {
    float position;
    int value;

    VarKey()
    {
    }
  }
}
