import java.io.*;
import java.awt.*;
import java.util.*;
import Vector3D;
import Surface;
import Ray;

class Triangle implements Renderable {
  protected Vector3D vert[];
  protected Surface surface;
  protected Vector3D normal;
  protected float offset;

  public Triangle(Vector3D v0, Vector3D v1, Vector3D v2) {
    vert = new Vector3D[3];
    vert[0] = v0;
    vert[1] = v1;
    vert[2] = v2;
    Vector3D edgev0v1 = vert[0].to(vert[1]);
    Vector3D edgev1v2 = vert[1].to(vert[2]);
    Vector3D edgev2v0 = vert[2].to(vert[0]);
    // Plane equation: normal . x = offset
    normal = Vector3D.cross(edgev0v1, edgev1v2);
    normal.normalize();
    // Use the average of the values
    offset = (normal.dot(vert[0])+normal.dot(vert[1])+normal.dot(vert[2]))/3.0f;
  }

  public Triangle(Vector3D v0, Vector3D v1, Vector3D v2, Surface s) {
    this(v0, v1, v2);
    surface = s;
  }

  public Vector3D getNormal() {
    return normal;
  }

  public void setSurface(Surface s) {
    surface = s;
  }

  public Surface getSurface() {
    return surface;
  }

  public boolean intersect(Ray ray) {

    // Implement this

  }

  public Color Shade(Ray ray, Vector lights, Vector objects, Color bgnd) {
    // An object shader doesn't really do too much other than
    // supply a few critical bits of geometric information
    // for a surface shader. It must must compute:
    //
    //   1. the point of intersection (p)
    //   2. a unit-length surface normal (n)
    //   3. a unit-length vector towards the ray's origin (v)
    //

    // Implement this


    // The illumination model is applied
    // by the surface's Shade() method
    return surface.Shade(p, n, v, lights, objects, bgnd);
  }

  public String toString() {
    return ("triangle v0="+vert[0]+" v1="+vert[1]+" v2="+vert[2]);
  }
}
