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

import csa.tiletools.worldgen.mapgen.miny.render.BB.AABB;
import csa.tiletools.worldgen.mapgen.miny.render.BB.BoundingBox;
import csa.tiletools.worldgen.mapgen.processing.CSAProcessing;
import java.util.ArrayList;

public class Polygon extends Drawable
  implements MultiPoints
{
  int c;
  AABB bb;
  float fallout;
  boolean recomputeBB;
  ArrayList<Float> px;
  ArrayList<Float> py;

  public Polygon(float paramFloat, int paramInt)
  {
    this.fallout = paramFloat;
    this.c = paramInt;
    this.recomputeBB = true;
    this.px = new ArrayList();
    this.py = new ArrayList();
  }

    @Override
  public void addPoint(float paramFloat1, float paramFloat2)
  {
    if (       (!this.px.isEmpty())
            && (!this.py.isEmpty())
            && (paramFloat1 == ((Float)this.px.get(this.px.size() - 1)).floatValue())
            && (paramFloat2 == ((Float)this.py.get(this.py.size() - 1)).floatValue())
       )
      return;
    this.px.add(Float.valueOf(paramFloat1));
    this.py.add(Float.valueOf(paramFloat2));
    this.recomputeBB = true;
  }

    @Override
  public BoundingBox getBoundingBox()
  {
    if ((this.recomputeBB) || (this.bb == null))
    {
      if ((this.px.isEmpty()) || (this.py.isEmpty()))
        return new AABB(0.0F, 0.0F, 0.0F, 0.0F);
      float f2;
      float f1 = f2 = ((Float)this.px.get(0)).floatValue();
      float f4;
      float f3 = f4 = ((Float)this.py.get(0)).floatValue();
      int i = CSAProcessing.min(this.px.size(), this.py.size());
      for (int j = 1; j < i; j++)
      {
        float f5 = ((Float)this.px.get(j)).floatValue();
        float f6 = ((Float)this.py.get(j)).floatValue();
        if (f5 < f1)
          f1 = f5;
        if (f5 > f2)
          f2 = f5;
        if (f6 < f3)
          f3 = f6;
        if (f6 > f4)
          f4 = f6;
      }
      this.bb = new AABB(f1 - this.fallout, f3 - this.fallout, f2 + this.fallout, f4 + this.fallout);
      this.recomputeBB = false;
    }
    return this.bb;
  }

    @Override
  public int getColor(float paramFloat1, float paramFloat2)
  {
    int i = CSAProcessing.min(this.px.size(), this.py.size());
    if (i < 3)
      return CSAProcessing.color(0, 0);
    Float[] arrayOfFloat1 = new Float[this.px.size()];
    Float[] arrayOfFloat2 = new Float[this.py.size()];
    arrayOfFloat1 = (Float[])this.px.toArray(arrayOfFloat1);
    arrayOfFloat2 = (Float[])this.py.toArray(arrayOfFloat2);
    boolean bool = RenderUtils.pointInPolygon(paramFloat1, paramFloat2, arrayOfFloat1, arrayOfFloat2);
    if (bool)
      return this.c;
    float f1 = 1.0E+010F;
    for (int j = 0; j < i; j++)
    {
      int k = (j + 1) % i;
      float f2 = RenderUtils.distanceToLine(paramFloat1, paramFloat2, ((Float)this.px.get(j)).floatValue(), ((Float)this.py.get(j)).floatValue(), ((Float)this.px.get(k)).floatValue(), ((Float)this.py.get(k)).floatValue());
      if (f2 < f1)
        f1 = f2;
    }
    return RenderUtils.multAlpha(this.c, 1.0F - RenderUtils.smoothstep(f1, 0.0F, this.fallout));
  }
}

