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 PolyLine extends Drawable
  implements MultiPoints
{
  float weight;
  float fallout;
  int c;
  AABB bb;
  boolean closed;
  boolean recomputeBB;
  ArrayList<Float> px;
  ArrayList<Float> py;

  public PolyLine(float paramFloat1, float paramFloat2, int paramInt, boolean paramBoolean)
  {
    this.weight = (paramFloat1 / 2.0F);
    this.fallout = paramFloat2;
    this.c = paramInt;
    this.closed = paramBoolean;
    this.recomputeBB = true;
    this.px = new ArrayList();
    this.py = new ArrayList();
  }

  public PolyLine(float paramFloat1, float paramFloat2, int paramInt, Polygon paramPolygon)
  {
    this.weight = (paramFloat1 / 2.0F);
    this.fallout = paramFloat2;
    this.c = paramInt;
    this.closed = true;
    this.recomputeBB = true;
    this.px = ((ArrayList)paramPolygon.px.clone());
    this.py = ((ArrayList)paramPolygon.py.clone());
  }

    @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;
  }

  public void setPoints(Polygon paramPolygon)
  {
    this.px = ((ArrayList)paramPolygon.px.clone());
    this.py = ((ArrayList)paramPolygon.py.clone());
    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 f6 = ((Float)this.px.get(j)).floatValue();
        float f7 = ((Float)this.py.get(j)).floatValue();
        if (f6 < f1)
          f1 = f6;
        if (f6 > f2)
          f2 = f6;
        if (f7 < f3)
          f3 = f7;
        if (f7 > f4)
          f4 = f7;
      }
      float f5 = this.weight + this.fallout;
      this.bb = new AABB(f1 - f5, f3 - f5, f2 + f5, f4 + f5);
      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 < 2)
      return CSAProcessing.color(0, 0);
    float f1 = 1.0E+010F;
    int j = i;
    if (!this.closed)
      j--;
    for (int k = 0; k < j; k++)
    {
      int m = (k + 1) % i;
      float f2 = RenderUtils.distanceToLine(paramFloat1, paramFloat2, ((Float)this.px.get(k)).floatValue(), ((Float)this.py.get(k)).floatValue(), ((Float)this.px.get(m)).floatValue(), ((Float)this.py.get(m)).floatValue());
      if (f2 < f1)
        f1 = f2;
    }
    return RenderUtils.multAlpha(this.c, 1.0F - RenderUtils.smoothstep(f1, this.weight, this.weight + this.fallout));
  }
}
