/* DVDGUI.java        Authors: Koffman & Wolz
 * GUI for class DVDCollection.
 * Uses DVDCollection, swing, io.
 */
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class DVDGUI extends JApplet
                    implements ActionListener, ItemListener {
  private JTextField titleText = new JTextField(20);
  private JTextField yearText = new JTextField(10);
  private JTextField timeText = new JTextField(10);
  private String[] categories = {"Drama", "Comedy", "Love Story",
             "Suspense", "Action", "Science Fiction", "Horror",
             "Animation"};
  private JComboBox catChooser = new JComboBox(categories);
  private String[] operations = {"Select an operation >>", "Add DVD",
             "Search for title", "Modify DVD", "Delete DVD",
             "List by year", "List by category", "List all titles"};
  private JComboBox opChooser = new JComboBox(operations);
  private JButton open = new JButton("open file");
  private JButton save = new JButton("save file");
  private JTextArea output = new JTextArea(8, 30);
  DVDCollection DVDdata = new DVDCollection();  // DVDCollection

  // Builds the GUI.
  public void init () {
    // Define the layout manager for the applet
    getContentPane().setLayout(
       new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

    // Fill the panels and add them to the applet. 
    JPanel titlePanel = new JPanel();
    JLabel titleLab = new JLabel("title");
    titlePanel.add(titleLab);
    titlePanel.add(titleText);
    getContentPane().add(titlePanel);

    JPanel yearPanel = new JPanel();
    JLabel yearLab = new JLabel("year");
    yearPanel.add(yearLab);
    yearPanel.add(yearText);
    getContentPane().add(yearPanel);

    JPanel timePanel = new JPanel();
    JLabel timeLab = new JLabel("time");
    timePanel.add(timeLab);
    timePanel.add(timeText);
    getContentPane().add(timePanel);

    JPanel catPanel = new JPanel();
    JLabel catLab = new JLabel("category");
    catPanel.add(catLab);
    catPanel.add(catChooser);
    getContentPane().add(catPanel);

    JPanel filePanel = new JPanel();
    filePanel.add(open);
    filePanel.add(save);
    getContentPane().add(filePanel);

    // Add output with scroll bars and operation chooser
    JScrollPane outputScroll= new JScrollPane(output,
           ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
           ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    getContentPane().add(outputScroll);
    getContentPane().add(opChooser);

    // Register applet as listener for action events
    //   on all buttons
    open.addActionListener(this);
    save.addActionListener(this);


    // Register applet as listener for item events
    //   on combo box opChooser
    opChooser.addItemListener(this);
  }

// postcondition: Responds to selection from opChooser.
public void itemStateChanged(ItemEvent op) {
   if (op.getSource() == opChooser) {
      switch (opChooser.getSelectedIndex()) {
          case 0: // Select an operation >>
              break;
          case 1: // Add DVD
              DVD aDVD;
              String title = titleText.getText();
              String cat = (String) catChooser.getSelectedItem();
              try {
                int year = Integer.parseInt(yearText.getText());
                double time = Double.parseDouble(timeText.getText());
                if (!title.equals("")) {
                  aDVD = new DVD(title, cat, year, time);
                  DVDdata.addDVD(aDVD);
                  output.setText("Added: " + aDVD.toString());
                } else {
                  output.setText("Title is missing");
                }
              } catch (NumberFormatException nfe) {
                output.setText(
                   "year or time is an invalid numeric string");
              }
              break;
          case 2: // Search for title
              title = titleText.getText();
              aDVD = DVDdata.searchDVD(title);
              if (aDVD == null) {
                output.setText(title + " not in collection");
              } else {
                catChooser.setSelectedItem(aDVD.getCategory());
                yearText.setText("" + aDVD.getYear());
                timeText.setText("" + aDVD.getTime());
                output.setText(title + " found");
              }
              break;
          case 3: case 4: case 5:
              output.setText("not implemented yet");
              break;
          case 6: // List DVDs in selected category
              cat = (String) catChooser.getSelectedItem();
              output.setText("DVDs in category " + cat + ":\n" +
                             DVDdata.listInCategory(cat));
              break;
          case 7: // List all DVDs
              output.setText("List of all DVDs:\n" +
                             DVDdata.toString());
              break;
          default:
              output.setText("Invalid operation");
      }  // end switch
      if (opChooser.getSelectedIndex() != 0)
        opChooser.setSelectedIndex(0);  // Reset opChooser
   }  // end if
}

// postcondition: If open was pressed, opens data file and
//   reads it. If save was pressed, writes DVD information to
//   output file and saves the output file.
public void actionPerformed(ActionEvent aE) {
   JFileChooser fileChooser =
        new JFileChooser();
   Object command = aE.getSource();
   try {
     if (command == open) {
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
           File aFile = fileChooser.getSelectedFile();
           String inFileName = aFile.getName();
           FileReader inStream = new FileReader(inFileName);
           BufferedReader ins = new BufferedReader(inStream);
           DVDdata.readDVDs(ins);
           output.setText("Reading data from file " +
                          inFileName);
        }
        else {
           output.setText("File open cancelled");
        }
     } // end open
     else if (command == save) {
        int result = fileChooser.showSaveDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
           File aFile = fileChooser.getSelectedFile();
           String outFileName = aFile.getName();
           FileWriter outStream = new FileWriter(outFileName);
           PrintWriter outs = new PrintWriter(outStream);
           DVDdata.writeDVDs(outs);
           output.setText("Writing data to file " +
                          outFileName);
        }
        else {
           output.setText("File save cancelled");
        }
     }  // end save
   }
   catch (IOException ex) {
     System.out.println(ex.getMessage());
     ex.printStackTrace();
   }
}
}


