package smart_gs.swingui;

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

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneLayout;

import smart_gs.drawing_tool.view.MemoPadView;
import smart_gs.logical.Region;
import smart_gs.logical.RegionOperation;
import smart_gs.logical.Spread;
import smart_gs.logical.region.MemoPadRegion;
import smart_gs.smleditor.swingui.GSEditorTextPane;
import smart_gs.swingui.WorkspaceWindow;
import smart_gs.util.Pair;

@SuppressWarnings("serial")
public class MemoPadDialog extends JDialog {
	
	private JScrollPane sPane;
	private JTextArea textArea;
	private JButton ok;
	private JButton cancel;

	public MemoPadDialog(Region region) {
		super(WorkspaceWindow.getInstance());
		this.setModal(true);
		String text = ((MemoPadView)region.getView()).getMemoPadText();
		this.textArea = new JTextArea(1,20);
		textArea.setText(text);
		
		this.sPane = new JScrollPane(textArea);
		this.ok = new JButton("OK");
		ok.addActionListener(new OKActionListener(this, region));
		this.cancel = new JButton("Cancel");
		cancel.addActionListener(new CancelActionListener(this));
		this.build();
		if(region.getName()!=""){
			this.setTitle("MemoPad: " + region.getName());
		}
		//
		this.setSize(300,200);		
	}

	private void build() {
		Container pane = this.getContentPane();
		pane.removeAll();
		this.setTitle("MemoPad");
		this.sPane.setLayout(new ScrollPaneLayout());
		this.getContentPane().add(this.sPane);
		this.getContentPane().add(this.ok);
		this.getContentPane().add(this.cancel);
		
		GroupLayout layout = new GroupLayout(pane);
		pane.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		layout.setHorizontalGroup(layout.createSequentialGroup()
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(this.sPane)
				.addGroup(layout.createSequentialGroup()
						.addComponent(this.ok)
						.addComponent(this.cancel))
			)
		);
	
	
		layout.setVerticalGroup(layout.createSequentialGroup()
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(this.sPane))
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(this.ok)
				.addComponent(this.cancel)
			)
		);
	}

	public JTextArea getTextArea() {
		return this.textArea;
	}
	
	class OKActionListener implements ActionListener {

		private MemoPadDialog dialog;
		private Region region;
		
		public OKActionListener(MemoPadDialog dialog,Region region) {
			this.dialog = dialog;
			this.region = region;
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String old_text = ((MemoPadView)region.getView()).getMemoPadText();
			String new_text = dialog.textArea.getText();
			((MemoPadView)region.getView()).setMemoPadText(new_text);
			Spread spread = region.getSpread();
			spread.pushRegionUndoStack(new RegionOperation(RegionOperation.CHANGE_MEMOPAD_TEXT, region, new Pair<String,String>(old_text,new_text),spread));
			dialog.dispose();
			WorkspaceWindow.setUpdated(true);
		}		
	}
	
	class CancelActionListener implements ActionListener {

		private MemoPadDialog dialog;
		
		public CancelActionListener(MemoPadDialog dialog) {
			this.dialog = dialog;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			dialog.dispose();	
		}
		
	}
}
