import java.util.*;
import java.awt.*;
import javax.swing.*;

public class View extends JPanel implements Observer
{
  private Model model;

  // Bob the Builder this shit
  public View(Model model, Handler handler)
  {
    // Hook up this observer so that it will be notified when the model
    // changes.
    this.model = model;
    this.setBackground(Color.white);
    model.addObserver(this);
    this.addMouseMotionListener(handler);
    this.addMouseListener(handler);
  }

  /**
  * Update with data from the model.
  */
  public void update(Object observable)
  {
    revalidate();
    repaint();
  }
  
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    Graphics2D gtemp = (Graphics2D) g;
    ArrayList<Thingy> thingies = model.getThingies();
    
    for (Thingy t: thingies)
    {
      ArrayList<Point> points = t.getPoints();
      gtemp.setColor(t.getColour());
      gtemp.setStroke(new BasicStroke(t.getThiccness(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
      
      if (t.getVisibleLen() == 0) {}
      else if (t.getVisibleLen() ==  1)
      {
        gtemp.drawLine(points.get(0).x, points.get(0).y, points.get(0).x, points.get(0).y);
      }
      else
      {
        for (int i = 1; i < t.getVisibleLen(); i++)
        {
          gtemp.drawLine(points.get(i - 1).x, points.get(i - 1).y, points.get(i).x, points.get(i).y);
        }
      }
    }
  }
}
