package edu.calpoly.csc480.Corral.Seer;

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;

import com.bcurry.www.swing.*;

import edu.calpoly.csc480.Corral.Agent.*;
import edu.calpoly.csc480.Corral.Area.*;
import edu.calpoly.csc480.Corral.Main.*;
import edu.calpoly.csc480.Corral.Tile.*;
import edu.calpoly.csc480.Corral.Seer.*;

abstract public class BaseSeerView extends BasicPanel
{
	static final protected int sliderTicksPerDelay = 100;
	
	public BaseSeerView() {
      super(new GridBagLayout());
		setFont(font = new Font("SansSerif", Font.BOLD, 12));

		log     = makeLogView();
		status  = makeStatusView();
		toolbar = makeToolbarView();
		
		log.setOwner(this);
		status.setOwner(this);
		toolbar.setOwner(this);
	}
	
	final public BaseSeer getDocument()			{return seer;}
	final public BaseMain getOwner()				{return main;}
	final public Font getFont()					{return font;}

	final public BaseLogView getLogView()		{return log;}
	
	public void setDocument(BaseSeer seer)	{
		this.seer = seer;
		this.area = seer.getArea().getView();
		this.area.setOwner(this);
		
		log.setDocument(seer);
		status.setDocument(seer);
		toolbar.setDocument(seer);

		makeStatusPanel();
		makeToolbarPanel();
		makeAreaPanel();
	}

	public void setOwner(BaseMain main) {
		this.main = main;
	}
	
	public void areaOpened() {
	}
	
	public void areaClosed() {
	}

	public void agentSelected(BaseMeans means) {
		status.agentSelected(means);
		toolbar.agentSelected();
	}
	
	public void agentUnselected() {
		status.agentUnselected();
		toolbar.agentUnselected();
	}
	
	public void start() {
		log.start();
		updateMain();
	}

	public void pause() {
		updateMain();
	}

	public void resume() {
		updateMain();
	}

	public void next() {
		area.next();
		log.next();
		status.next();
	}
	
	public void stop() {
		log.stop();
		toolbar.stop();
		updateMain();
	}
	
	protected BaseMain main;
	protected BaseSeer seer;
	protected BaseAreaView area;
	protected BaseLogView log;
	protected BaseStatusView status;
	protected BaseToolbarView toolbar;

	protected Font font;	
	protected JPanel statusPanel, toolbarPanel, areaPanel;
	
	protected void makeToolbarPanel() {
		GridBagConstraints curb = new GridBagConstraints();
		
		toolbarPanel = new JPanel(new GridBagLayout(), true);
      toolbarPanel.setBorder(BorderFactory.createRaisedBevelBorder());
		
		curb.anchor = GridBagConstraints.SOUTH;
      curb.fill = GridBagConstraints.HORIZONTAL;
      curb.gridx = 0;
      curb.gridy = 0;
		curb.gridheight = 1;
      curb.gridwidth  = GridBagConstraints.REMAINDER;
      curb.weightx = 1.0;
		curb.weighty = 0.0;
		toolbarPanel.add(toolbar, curb);
		add(toolbarPanel, curb);
	}

	protected void makeStatusPanel() {
		GridBagConstraints curb = new GridBagConstraints();

		curb.anchor = GridBagConstraints.NORTH;

		statusPanel = new JPanel(new GridBagLayout(), true);
		statusPanel.setBorder(BorderFactory.createTitledBorder(
		 BorderFactory.createEtchedBorder(), "Status",
		 TitledBorder.LEFT, TitledBorder.TOP));
		statusPanel.add(status, curb);
		
      curb.fill = GridBagConstraints.VERTICAL;
      curb.gridx = 0;
      curb.gridy = 1;
		curb.gridheight = 1;
      curb.gridwidth  = 1;
		curb.weightx = 0.0;
      curb.weighty = 1.0;     
      add(statusPanel, curb);
	}
	
	protected void makeAreaPanel() {
		GridBagConstraints curb = new GridBagConstraints();
		
		areaPanel = new JPanel(new GridBagLayout(), true);
      areaPanel.add(area, curb);
		
      curb.fill = GridBagConstraints.BOTH;
		curb.gridx = 1;
      curb.gridy = 1;
		curb.gridheight = 1;
      curb.gridwidth  = GridBagConstraints.REMAINDER;
		curb.weightx = 1.0;
      curb.weighty = 1.0;
      add(areaPanel, curb);
	}

	protected void updateMain() {
		if (main != null) {
			if (seer.isResting()) {
				main.seerResting();
			}
			else {
				main.seerRunning();
			}
		}
	}

	abstract protected BaseLogView makeLogView();
	abstract protected BaseStatusView makeStatusView();
	abstract protected BaseToolbarView makeToolbarView();
}