package edu.calpoly.csc480.Entomo.Seer;

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

import com.bcurry.www.lang.*;
import com.bcurry.www.swing.*;

import edu.calpoly.csc480.Entomo.Area.*;
import edu.calpoly.csc480.Entomo.Main.*;
import edu.calpoly.csc480.Entomo.Tile.*;

public class SeerView extends BasicPanel
{
	public SeerView() {
      super(new GridBagLayout());
//		setFont(font = new Font("SansSerif", Font.BOLD, 12));

		log     = new LogView();
		status  = new StatusView();
		legend  = new Legend();
		toolbar = new ToolbarView();

		options = new Options();
		
		log.setOwner(this);
		status.setOwner(this);
		toolbar.setOwner(this);
	}
	
	final public Seer getDocument()				{return seer;}
	final public Options getOptions()			{return options;}

	final public AreaView getAreaView()			{return area;}
	final public LogView getLogView()			{return log;}
	final public StatusView getStatusView()	{return status;}
	final public ToolbarView getToolbarView()	{return toolbar;}
	
	public void setDocument(Seer seer)	{
		this.seer = seer;
		this.area = seer.getArea().getView();
		
		area.setOwner(this);
		
		log.setDocument(seer);
		status.setDocument(seer);
		toolbar.setDocument(seer);

		options.setDocument(seer);

		makeAreaPanel();
		makeDetailPanel();
		makeToolbarPanel();

		updateComponents();
	}

	public void setOwner(Main main) {
		this.main = main;
	}
	
	public void updateComponents() {
		if (main != null) {
			if (!seer.isRunning()) {
				main.seerResting();
			}
			else {
				main.seerRunning();
			}
		}

		status.updateComponents();
		toolbar.updateComponents();
	}

	public void areaOpened() {
	}
	
	public void areaClosed() {
	}

	public void start() {
		log.start();
	}

	public void play() {
		updateComponents();
	}

	public void pause() {
		updateComponents();
	}

	public void next() {
		log.next();
		status.next();
	}
	
	public void stop() {
		log.stop();
		updateComponents();
	}

	public void finish() {
		updateComponents();
	}
	
	protected Main main;
	protected Seer seer;
	protected AreaView area;

	protected Legend legend;
	protected LogView log;
	protected StatusView status;
	protected ToolbarView toolbar;

	protected Options options;

	protected JPanel statusPanel, legendPanel, 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 makeDetailPanel() {
		GridBagConstraints curb = new GridBagConstraints();
		JPanel detailPanel;
		
		statusPanel = new JPanel(new GridBagLayout(), true);
		statusPanel.setBorder(BorderFactory.createTitledBorder(
		 BorderFactory.createEtchedBorder(), "Status",
		 TitledBorder.LEFT, TitledBorder.TOP));
		statusPanel.add(status, curb);
		
/*      curb.fill = GridBagConstraints.BOTH;
      curb.gridx = 0;
      curb.gridy = 1;
		curb.gridheight = 1;
      curb.gridwidth  = 1;
		curb.weightx = 0.1;
      curb.weighty = 1.0;     
      add(statusPanel, curb);
*/
		legendPanel = new JPanel(new GridBagLayout(), true);
		legendPanel.setBorder(BorderFactory.createTitledBorder(
		 BorderFactory.createEtchedBorder(), "Legend",
		 TitledBorder.LEFT, TitledBorder.TOP));
		legendPanel.add(legend, curb);
		
/*	   curb.fill = GridBagConstraints.HORIZONTAL;
	   curb.gridx = 0;
	   curb.gridy = 2;
		curb.gridheight = 1;
	   curb.gridwidth  = 1;
		curb.weightx = 0.1;
	   curb.weighty = 0.0;
	   add(legendPanel, curb);
*/
		detailPanel = new JPanel(true);
		detailPanel.setLayout(new BoxLayout(detailPanel, BoxLayout.Y_AXIS));
		detailPanel.add(statusPanel);
		detailPanel.add(legendPanel);

//		curb.fill = GridBagConstraints.HORIZONTAL;
		curb.gridx = 0;
		curb.gridy = 1;
		curb.gridheight = 1;
		curb.gridwidth  = 1;
		curb.weightx = 0.0;
		curb.weighty = 0.0;
		add(detailPanel, 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 = 2;
      curb.gridwidth  = GridBagConstraints.REMAINDER;
		curb.weightx = 0.8;
      curb.weighty = 1.0;
      add(areaPanel, curb);
	}
}