import java.util.Observable;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;

public class Model extends Observable implements Serializable
{
   /** The observers that are watching this model for changes. */
  private ArrayList<Observer> observers;
  private ArrayList<PicData> pics;
  private int filter;
  private boolean grid;

  // Constructor
  public Model()
  {
    this.observers = new ArrayList();
    this.pics = new ArrayList();
    this.filter = 0;
    this.grid = true;
    PicData.model = this;
    setChanged();
  }
  
  public void newPic(String filepath, String name, long size, long lastmod)
  {
    PicData temp = new PicData(filepath, name, size, lastmod);
    pics.add(temp);
    setChanged();
    notifyObservers();
  }
  
  public ArrayList<PicData> getPics()
  {
    return pics;
  }
  
  public boolean getGrid()
  {
    return grid;
  }
  
  public int getFilter()
  {
    return filter;
  }
  
  public void changeFilter(int x)
  {
    this.filter = x;
    setChanged();
    notifyObservers();
  }
  
  public void switchLayout(boolean flag)
  {
    if (grid != flag)
    {
      grid = flag;
      System.out.println("Layout Toggled!");
      setChanged();
      notifyObservers();
    }
  }
  
  public void update()
  {
    setChanged();
    notifyObservers();
  }

  // Add observer to be notified on change
  public void addObserver(Observer observer)
  {
    this.observers.add(observer);
  }

  // Remove an observer from opdate list
  public void removeObserver(Observer observer)
  {
    this.observers.remove(observer);
  }

   // Notify all observers shit went down
  public void notifyObservers()
  {
    for (Observer observer: this.observers)
    {
      observer.update(this);
    }
  }
}

class PicData implements Serializable
{
  private int rating;
  private Image pic;
  private long size;
  private long lastmod;
  private String name;
  public static Model model;

  public PicData(String filepath, String name, long size, long lastmod)
  {
    try
    {
      this.pic = ImageIO.read(new File(filepath));
    } catch(IOException e) {}
    
    this.pic = pic.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
    this.size = size/1024;
    this.lastmod = lastmod;
    this.name = name;
    this.rating = 0;
  }
  
  public void setRating(int x)
  {
    this.rating = x;
    model.update();
  }
  
  public Image getPic()
  {
    return pic;
  }
  
  public int getRating()
  {
    return rating;
  }
  
  public long getSize()
  {
    return size;
  }
  
  public long getLastmod()
  {
    return lastmod;
  }
  
  public String getName()
  {
    return name;
  }
}
