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

public class Controls extends JPanel implements Observer
{
  private Model model;
  private playSlider playback;

  // Bob the Builder this shit
  public Controls(Model model, Handler handler)
  {
    // Hook up this observer so that it will be notified when the model
    // changes.
    this.model = model;
    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    this.setPreferredSize(new Dimension(1280,75));
    this.setMinimumSize(new Dimension(320, 75));
    this.playback = new playSlider(0, 0, 0);
    playback.addChangeListener(new ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        if (!playback.getSettingLen() && playback.getValue() != model.getTotalVisibleLen())
        {
          model.changeThingy(playback.getValue());
        }
      }
    });
    this.playback.setPreferredSize(new Dimension(280, 75));
    this.add(playback);
    model.addObserver(this);
  }

  public void update(Object observable)
  {
    int len = model.getTotalLen();
    if (len > 0)
    {
      playback.toggleSettingLen();
      playback.setMaximum(len);
      playback.toggleSettingLen();
      playback.setValue(model.getTotalVisibleLen());
    }
    this.setBackground(model.controlColour);
    revalidate();
    repaint();
  }
}

class playSlider extends JSlider
{
  private boolean settingLen;
  
  public playSlider(int min, int max, int val)
  {
    super(min, max, val);
    this.settingLen = false;
  }
  
  public void toggleSettingLen()
  {
    this.settingLen = !settingLen;
  }

  public boolean getSettingLen()
  {
    return settingLen;
  }
}
