/**
 * Main Dialog for adding a tutorial page
 * 
 * @author Kevin Strong
 */

package cstutor;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.Box;
import javax.swing.ScrollPaneConstants;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddPageUI extends JFrame implements ActionListener {
	protected final int maxWidth;
	protected final double maxHeight;
	
	public AddPageUI() {
		maxWidth = 3000;
		maxHeight = 1.9;
		
		setSize(666,528);
		setTitle("CSTutor");
                //modified to work with the create tutorial GUI
		//setDefaultCloseOperation(EXIT_ON_CLOSE);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		
		JLabel heading = new JLabel("<html><h1>Add a Tutorial Page</h1></html>", JLabel.CENTER);
		
		Box body = Box.createVerticalBox();
		body.add(Box.createVerticalStrut(10));
		body.add(buildTitleRow());
		body.add(Box.createVerticalStrut(15));
		body.add(buildBody());
		body.add(Box.createVerticalStrut(20));
		
		Box sideBar = buildSideBar();
		sideBar.add(Box.createVerticalStrut(20));
		
		Box bottomRow = buildBottomRow();
		
		Box tutframe = Box.createHorizontalBox();
		tutframe.add(Box.createHorizontalStrut(70));
		tutframe.add(body);
		tutframe.add(Box.createHorizontalStrut(10));
		tutframe.add(sideBar);
		
		JTabbedPane tabPane = new JTabbedPane();
		tabPane.add("Tutorial", tutframe);
		tabPane.add("Options", buildOptions());
		
		mainPanel.add(heading, BorderLayout.PAGE_START);
		mainPanel.add(tabPane, BorderLayout.CENTER);
		mainPanel.add(bottomRow, BorderLayout.PAGE_END);
		
		add(mainPanel);
	}
	
	protected Box buildSideBar() {
		Box sideFiles = Box.createVerticalBox();
		
		JButton addPictureButton = new JButton("Add Picture");
		addPictureButton.setActionCommand("picture");
		addPictureButton.addActionListener(this);
		sideFiles.add(Box.createVerticalStrut(50));
		sideFiles.add(addPictureButton);
		sideFiles.add(Box.createVerticalGlue());
		
		sideFiles.add(buildRelFiles());
		
		return sideFiles;
	}
	
	protected Box buildRelFiles() {
		Box vbox = Box.createVerticalBox();
		
		JLabel rel = new JLabel("Related Files: ", JLabel.LEFT);
		JTextArea reltext = new JTextArea();
		JScrollPane relpane = new JScrollPane(reltext);
		relpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		relpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		vbox.add(rel);
		vbox.add(relpane);
		
		return vbox;
	}
	
	protected Box buildTitleRow() {
		Box rbox = Box.createHorizontalBox();
		
		JLabel title = new JLabel("Title: ");
		JTextField titletext = new JTextField();
		titletext.setMaximumSize(new Dimension(maxWidth, (int)(maxHeight * titletext.getFont().getSize())));
		rbox.add(title);
		rbox.add(titletext);
		
		return rbox;
	}
	
	protected Box buildBody() {
		Box vbox = Box.createVerticalBox();
		
		// put label in its own box to left-align further
		Box bb = Box.createHorizontalBox();
		JLabel body = new JLabel("Body: ", JLabel.LEFT);
		bb.add(body); bb.add(Box.createHorizontalStrut(400));
		
		JTextArea bodytext = new JTextArea();
		JScrollPane bodypane = new JScrollPane(bodytext);
		bodypane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		bodypane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		bodypane.setPreferredSize(new Dimension(400, 450));
		
		vbox.add(bb);
		vbox.add(Box.createVerticalStrut(5));
		vbox.add(bodypane);
		
		return vbox;
	}
	
	protected Box buildBottomRow() {
		Box rbox = Box.createHorizontalBox();
		
		JButton publish = new JButton("Publish Page");
		publish.setActionCommand("publish");
		publish.addActionListener(this);
		JButton save = new JButton("Save Page");
		save.setActionCommand("save");
		save.addActionListener(this);
		JButton add = new JButton("Add Page");
		add.setActionCommand("add");
		add.addActionListener(this);
		
		rbox.add(Box.createHorizontalGlue());
		rbox.add(publish);
		rbox.add(Box.createHorizontalStrut(10));
		rbox.add(save);
		rbox.add(Box.createHorizontalStrut(10));
		rbox.add(add);
		rbox.add(Box.createHorizontalGlue());
		
		return rbox;
	}
	
	protected Box buildOptions() {
		Box optPanel = Box.createVerticalBox();
		Box row1 = Box.createHorizontalBox();
		Box row2 = Box.createHorizontalBox();
		Box row3 = Box.createHorizontalBox();
		
		JCheckBox useShell = new JCheckBox("Use Interactive Shell");
		
		JLabel lang = new JLabel("Language:           ", JLabel.RIGHT);
		
		String[] languages = {"C/C++", "Java", "Python", "Ruby"};
		JComboBox langbox = new JComboBox(languages);
		langbox.setSelectedIndex(1);
		langbox.setMaximumSize(new Dimension(maxWidth, (int)(maxHeight * langbox.getFont().getSize())));
		//lang.setMaximumSize(langbox.getMaximumSize());
		JLabel def = new JLabel("Default Code: ", JLabel.RIGHT);
		
		JTextArea defcode = new JTextArea();
		JScrollPane codepane = new JScrollPane(defcode);
		codepane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		codepane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		row1.add(Box.createHorizontalGlue()); 
		row1.add(useShell); 
		row1.add(Box.createHorizontalGlue());
		
		row2.add(lang);
		row2.add(langbox);
		row2.add(Box.createHorizontalGlue());
		
		row3.add(def); 
		row3.add(Box.createHorizontalStrut(10));
		row3.add(codepane);
		
		optPanel.add(row1);
		optPanel.add(Box.createVerticalStrut(15));
		optPanel.add(row2);
		optPanel.add(Box.createVerticalStrut(15));
		optPanel.add(row3);
		
		return optPanel;
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("publish")) {
			JOptionPane.showMessageDialog(this, "Tutorial Page Saved to Server");
		}
		else if (e.getActionCommand().equals("save")) {
			JOptionPane.showMessageDialog(this, "Tutorial Page Saved to Disk");
		}
		else if (e.getActionCommand().equals("add")) {
			JOptionPane.showMessageDialog(this, "Tutorial Page Successfully Added");
		}
		else if (e.getActionCommand().equals("picture")) {
			// open add picture dialog
			AddPictureDlg addpic = new AddPictureDlg(this);
			addpic.setVisible(true);
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame frame = new AddPageUI();
		frame.setVisible(true);
	}

}