/*
	Description:
	Author: Students in CS231
	Date: April 2003
 */

import java.awt.*;
import java.applet.*;
import java.util.StringTokenizer;

public class MusicDBApplet extends Applet
{
    MusicDataBase myDB = new MusicDataBase();
    
	public void init()
	{
		// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		symantec.itools.lang.Context.setApplet(this);
	
		// 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
		setLayout(null);
		setBackground(new java.awt.Color(223,9,186));
		setSize(583,420);
		addArtistButton.setLabel("Add");
		add(addArtistButton);
		addArtistButton.setBackground(java.awt.Color.lightGray);
		addArtistButton.setForeground(java.awt.Color.blue);
		addArtistButton.setBounds(120,48,73,32);
		add(artistTF);
		artistTF.setBackground(java.awt.Color.white);
		artistTF.setBounds(12,48,96,37);
		artistLabel.setText("Artist Name:");
		add(artistLabel);
		artistLabel.setBackground(java.awt.Color.red);
		artistLabel.setForeground(java.awt.Color.blue);
		artistLabel.setFont(new Font("Dialog", Font.BOLD, 14));
		artistLabel.setBounds(12,24,88,18);
		add(label1);
		label1.setBackground(java.awt.Color.red);
		label1.setBounds(0,12,244,114);
		addAlbumButton.setLabel("Add");
		add(addAlbumButton);
		addAlbumButton.setBackground(java.awt.Color.lightGray);
		addAlbumButton.setForeground(new java.awt.Color(166,58,211));
		addAlbumButton.setBounds(180,168,73,32);
		deleteArtistButton.setLabel("Delete Artist");
		add(deleteArtistButton);
		deleteArtistButton.setBackground(java.awt.Color.lightGray);
		deleteArtistButton.setForeground(java.awt.Color.blue);
		deleteArtistButton.setBounds(36,300,84,32);
		deleteAlbumButton.setLabel("Delete Album");
		add(deleteAlbumButton);
		deleteAlbumButton.setBackground(java.awt.Color.lightGray);
		deleteAlbumButton.setForeground(java.awt.Color.blue);
		deleteAlbumButton.setBounds(156,300,84,32);
		albumLabel.setText("Albums");
		add(albumLabel);
		albumLabel.setBackground(java.awt.Color.yellow);
		albumLabel.setBounds(12,144,106,25);
		add(albumNameTF);
		albumNameTF.setBackground(java.awt.Color.white);
		albumNameTF.setBounds(60,168,99,34);
		add(albumDateTF);
		albumDateTF.setBackground(java.awt.Color.white);
		albumDateTF.setBounds(60,216,99,34);
		artistChoice.setBackground(java.awt.Color.white);
		artistChoice.setFont(new Font("Dialog", Font.PLAIN, 16));
		add(artistChoice);
		artistChoice.setBounds(60,264,156,25);
		label2.setText("Name:");
		add(label2);
		label2.setBackground(java.awt.Color.yellow);
		label2.setBounds(12,180,48,24);
		label3.setText("Date:");
		add(label3);
		label3.setBackground(java.awt.Color.yellow);
		label3.setBounds(12,216,45,25);
		label4.setText("Artist:");
		add(label4);
		label4.setBackground(java.awt.Color.yellow);
		label4.setBounds(12,264,45,25);
		add(label5);
		label5.setBackground(new java.awt.Color(166,58,211));
		label5.setBounds(0,132,267,204);
		add(outputTA);
		outputTA.setBackground(java.awt.Color.white);
		outputTA.setBounds(300,12,256,311);
		listArtistsButton.setLabel("List Artists");
		add(listArtistsButton);
		listArtistsButton.setBackground(java.awt.Color.black);
		listArtistsButton.setForeground(java.awt.Color.white);
		listArtistsButton.setBounds(300,348,97,38);
		add(textField1);
		textField1.setBounds(12,168,99,34);
		listAlbumsButton.setLabel("List Albums");
		add(listAlbumsButton);
		listAlbumsButton.setBackground(java.awt.Color.black);
		listAlbumsButton.setForeground(java.awt.Color.white);
		listAlbumsButton.setBounds(420,348,97,38);
		saveButton.setLabel("Save");
		add(saveButton);
		saveButton.setBackground(new java.awt.Color(62,116,54));
		saveButton.setForeground(java.awt.Color.white);
		saveButton.setBounds(12,348,89,38);
		loadButton.setLabel("Load");
		add(loadButton);
		loadButton.setBackground(new java.awt.Color(62,116,54));
		loadButton.setForeground(java.awt.Color.white);
		loadButton.setBounds(120,348,89,38);
		//}}
	
		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		addArtistButton.addActionListener(lSymAction);
		addAlbumButton.addActionListener(lSymAction);
		deleteArtistButton.addActionListener(lSymAction);
		loadButton.addActionListener(lSymAction);
		saveButton.addActionListener(lSymAction);
		//}}
	}
	
	//{{DECLARE_CONTROLS
	java.awt.Button addArtistButton = new java.awt.Button();
	java.awt.TextField artistTF = new java.awt.TextField();
	java.awt.Label artistLabel = new java.awt.Label();
	java.awt.Label label1 = new java.awt.Label();
	java.awt.Button addAlbumButton = new java.awt.Button();
	java.awt.Button deleteArtistButton = new java.awt.Button();
	java.awt.Button deleteAlbumButton = new java.awt.Button();
	java.awt.Label albumLabel = new java.awt.Label();
	java.awt.TextField albumNameTF = new java.awt.TextField();
	java.awt.TextField albumDateTF = new java.awt.TextField();
	java.awt.Choice artistChoice = new java.awt.Choice();
	java.awt.Label label2 = new java.awt.Label();
	java.awt.Label label3 = new java.awt.Label();
	java.awt.Label label4 = new java.awt.Label();
	java.awt.Label label5 = new java.awt.Label();
	java.awt.TextArea outputTA = new java.awt.TextArea();
	java.awt.Button listArtistsButton = new java.awt.Button();
	java.awt.TextField textField1 = new java.awt.TextField();
	java.awt.Button listAlbumsButton = new java.awt.Button();
	java.awt.Button saveButton = new java.awt.Button();
	java.awt.Button loadButton = new java.awt.Button();
	//}}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == addArtistButton)
				addArtistButton_ActionPerformed(event);
			else if (object == addAlbumButton)
				addAlbumButton_ActionPerformed(event);
			else if (object == deleteArtistButton)
				deleteArtistButton_ActionPerformed(event);
			else if (object == loadButton)
				loadButton_ActionPerformed(event);
			else if (object == saveButton)
				saveButton_ActionPerformed(event);
		}
	}

	void addArtistButton_ActionPerformed(java.awt.event.ActionEvent event)
	{
		String s = artistTF.getText();
		artistChoice.addItem(s);
		Artist myArtist = new Artist(s);
		myDB.addArtist(myArtist);
		outputTA.append("\n\nYou just added Artist:\n " + myArtist.toString());
		outputTA.append("\n\n"+myDB);
	}

	void addAlbumButton_ActionPerformed(java.awt.event.ActionEvent event)
	{
		String name = albumNameTF.getText();
		String date = albumDateTF.getText();
		String artistString = artistChoice.getSelectedItem();
		
		Artist artist = myDB.getArtist(artistString);
		Album album = new Album(artist, name,date);
		myDB.addAlbum(album);
		artist.addAlbum(album);
		outputTA.append("\n\nYou just added Album:\n " + album.toString());
		outputTA.append("\n\n"+myDB);
	}

	void deleteArtistButton_ActionPerformed(java.awt.event.ActionEvent event)
	{
		String artistString = artistChoice.getSelectedItem();
		Artist a = myDB.deleteArtist(artistString);
		if (a != null) 
		    outputTA.append("\n\n"+a.getName() + " removed.");
		else 
		    outputTA.append("\n\n"+artistString + " not in database.");
		
		outputTA.append("\n\n"+myDB);
		
	}

	void loadButton_ActionPerformed(java.awt.event.ActionEvent event)
	{
		//DBStorage dbStore = new DBStorage();
		//myDB = dbStore.read();
		//myDB.setChoices(artistChoice);
		readASCII();
	}

	void saveButton_ActionPerformed(java.awt.event.ActionEvent event)
	{
		//DBStorage dbStore = new DBStorage();
		//dbStore.write(myDB);
		myDB.save();
		
	}
	// Assumes that file has on each line:
	// AlbumName AlbumDate ArtistName
	public void readASCII() {
	    try {
	        MyReader reader = new MyReader() ;
	        myDB = new MusicDataBase();
	        while (reader.hasMoreData()) {
	            String artistName = reader.giveMeTheNextLine();
	            Artist a = new Artist(artistName);
	            String albumlistString = reader.giveMeTheNextLine();
	            StringTokenizer st = new StringTokenizer(albumlistString);
	            while(st.hasMoreTokens()) {
	                  String albumName = st.nextToken();
	                  String albumDate = st.nextToken(); 
	                  Album album = new Album(a, albumName, albumDate);
	                  a.addAlbum(album);
	                  myDB.addAlbum(album);
	            }
	            myDB.addArtist(a);
	            
	            artistChoice.addItem(a.getName());
	        }
	    } catch (Exception e) {
	        System.out.println("Error reading ascii input file. " + e);
	    }
	    
		outputTA.append("\n\n"+myDB);
	        
	            
	    
	}
}