/*
 * This is the main class of Simphony. The Simphony process serves both 
 * as a server and a client by using multiple threads, and therefore 
 * is the only process the user needs to run and is meant to be 
 * running all the time on the user's desktop computer. When Simphony 
 * starts up, it first  spawns a "SipServer" as a background thread, 
 * which handles incoming calls. SipServer will in turn spawn a "UAServer"
 * (user agent server) thread every time it receives an incoming call. 
 *
 * The main process presents the user with a main menu and handles GUI.
 * Upon user selecting an option, an appropriate dialog box will pop up,
 * allowing the user to enter necessary information and/or to place a call.
 * The following is the main menu options and their corresponding GUI dialog
 * boxse.
 *	1) make a outgoing call (call box)  --> CallerDialog
 *	2) set media agents                 --> MediaDialog
 * 	3) register for call forwarding     --> RegisterDialog
 *	4) directory (speed dial - not implemented yet)
 *	5) help (not implemented yet)
 * 
 *
 * Author: Janet H. Park
 * Date: August, 1998
 *
 */

import java.awt.*;

public class Simphony extends Frame
{
	public Simphony()
	{
		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		GridBagLayout gridBagLayout;
		gridBagLayout = new GridBagLayout();
		setLayout(gridBagLayout);
		setVisible(false);
		setSize(insets().left + insets().right + 227,insets().top + insets().bottom + 289);
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(new Color(16777215));
		panel1 = new java.awt.Panel();
		panel1.setLayout(null);
		panel1.setBounds(insets().left + 15,insets().top + 15,197,259);
		panel1.setFont(new Font("Helvetica", Font.BOLD, 12));
		panel1.setForeground(new Color(-8266074));
		panel1.setBackground(new Color(-3545857));
		GridBagConstraints gbc;
		gbc = new GridBagConstraints();
		gbc.weightx = 100.0;
		gbc.weighty = 100.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(15,15,15,15);
		((GridBagLayout)getLayout()).setConstraints(panel1, gbc);
		add(panel1);
		buttonCall = new java.awt.Button();
		buttonCall.setActionCommand("button");
		buttonCall.setLabel("Call Box");
		buttonCall.setBounds(21,45,150,25);
		buttonCall.setFont(new Font("Dialog", Font.BOLD, 14));
		buttonCall.setForeground(new Color(-5963845));
		buttonCall.setBackground(new Color(-12565076));
		panel1.add(buttonCall);
		buttonExit = new java.awt.Button();
		buttonExit.setActionCommand("button");
		buttonExit.setLabel("Exit");
		buttonExit.setBounds(50,225,84,24);
		buttonExit.setFont(new Font("Dialog", Font.PLAIN, 12));
		buttonExit.setForeground(new Color(-16777125));
		buttonExit.setBackground(new Color(-6291522));
		panel1.add(buttonExit);
		labelTitle = new java.awt.Label("Simphony",Label.CENTER);
		labelTitle.setBounds(21,9,144,24);
		labelTitle.setFont(new Font("Dialog", Font.BOLD, 22));
		labelTitle.setForeground(new Color(0));
		panel1.add(labelTitle);
		button1 = new java.awt.Button();
		button1.setActionCommand("button");
		button1.setLabel("Media Agents");
		button1.setBounds(21,81,150,25);
		button1.setFont(new Font("Dialog", Font.BOLD, 14));
		button1.setForeground(new Color(-5963845));
		button1.setBackground(new Color(-12565076));
		panel1.add(button1);
		buttonCF = new java.awt.Button();
		buttonCF.setActionCommand("button");
		buttonCF.setLabel("Call Forwarding");
		buttonCF.setBounds(21,117,150,25);
		buttonCF.setFont(new Font("Dialog", Font.BOLD, 14));
		buttonCF.setForeground(new Color(-5963845));
		buttonCF.setBackground(new Color(-12565076));
		panel1.add(buttonCF);
		button3 = new java.awt.Button();
		button3.setActionCommand("button");
		button3.setLabel("Directory");
		button3.setBounds(21,153,150,25);
		button3.setFont(new Font("Dialog", Font.BOLD, 14));
		button3.setForeground(new Color(-5963845));
		button3.setBackground(new Color(-12565076));
		panel1.add(button3);
		button4 = new java.awt.Button();
		button4.setActionCommand("button");
		button4.setLabel("Help");
		button4.setBounds(21,189,150,25);
		button4.setFont(new Font("Dialog", Font.BOLD, 15));
		button4.setForeground(new Color(-5963845));
		button4.setBackground(new Color(-12565076));
		panel1.add(button4);
		setTitle("Simphony");
		
		setIconImage(Toolkit.getDefaultToolkit().getImage("phone.gif"));
		//}}

		//{{INIT_MENUS
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymAction lSymAction = new SymAction();
		buttonCall.addActionListener(lSymAction);
		buttonExit.addActionListener(lSymAction);
		button1.addActionListener(lSymAction);
		buttonCF.addActionListener(lSymAction);
		//}}
	}

	public Simphony(String title)
	{
		this();
		setTitle(title);
	}

	public synchronized void show()
	{
		move(50, 50);
		super.show();
	}
    
    static public void main(String args[])
	{
		System.out.println("Simphony started.");

		// create background server thread which will listen for incoming calls
		SipServer sipSrvr = new SipServer();
		sipSrvr.setDaemon(true);
		sipSrvr.start();
	    
		(new Simphony()).show();
	}
	
	public void addNotify()
	{
	    // Record the size of the window prior to calling parents addNotify.
	    Dimension d = getSize();
	    
		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the insets
		setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
		Component components[] = getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(insets().left, insets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

    // Used for addNotify check.
	boolean fComponentsAdjusted = false;

	//{{DECLARE_CONTROLS
	java.awt.Panel panel1;
	java.awt.Button buttonCall;
	java.awt.Button buttonExit;
	java.awt.Label labelTitle;
	java.awt.Button button1;
	java.awt.Button buttonCF;
	java.awt.Button button3;
	java.awt.Button button4;
	//}}

	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == Simphony.this)
				Frame1_WindowClosing(event);
		}
	}
	
	void Frame1_WindowClosing(java.awt.event.WindowEvent event)
	{
		hide();		 // hide the Frame
	}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == buttonCall)
				buttonCall_Action(event);
			else if (object == buttonExit)
				buttonExit_Action(event);
			else if (object == button1)
				button1_Action(event);
			else if (object == buttonCF)
				buttonCF_Action(event);
		}
	}

	void buttonCall_Action(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Create and show as modal
		if (cd == null)
		 	cd = new CallerDialog(this, true, mediaAgents);
		 cd.show();
		// (new CallerDialog(this, true, mediaAgents)).show();
		//}}
	}

	void buttonExit_Action(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Hide the Frame
		setVisible(false);
		System.exit(0);
		//}}
	}

	void button1_Action(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Create and show as modal
		if (md == null) {
			md = new MediaDialog(this, true);
		} 
		md.show();
		//}}
	}

	void setMediaAgents(Media[] ma) {
		mediaAgents = ma;
	}

	void buttonCF_Action(java.awt.event.ActionEvent event)
	{
			 
		//{{CONNECTION
		// Create and show as modal
		if (rd == null) {
			rd = new RegisterDialog(this, true);
		} 
		rd.show();
		//}}
		//}}
	}


	void setCallFwd(String uid, String fwdAddr) {
		this.uid = uid;
		this.fwdAddr = fwdAddr;
		callforwarding = true;
	}
	
	void setCallFwdOff() {
		callforwarding = false;
	}


	CallerDialog cd = null;
	MediaDialog md = null;
	RegisterDialog rd = null;

	static String uid = "";
	static String fwdAddr = "";
	static Media[] mediaAgents = null;
	static boolean callforwarding = false;
}
