package risk.messagetool;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.Box;

public class DoublePanel extends JPanel implements ActionListener {

	private JLabel name;
	private Mtcomm mycomm;
	private MyNode message;

	private YuraTextEditor top;
	private YuraTextEditor bottom;

	private Locale localetop;
	private Locale localebottom;

	public DoublePanel() {

		setLayout( new BorderLayout() );

		name = new JLabel();



		top = new YuraTextEditor();
		bottom = new YuraTextEditor();



		addButtons(top);
		addButtons(bottom);

		top.addActionListener(this);
		bottom.addActionListener(this);

		top.setLabel( "default" );

		JPanel box2 = new JPanel( new GridLayout(2,1,10,10) );
		box2.add(top);
		box2.add(bottom);
		box2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 10, 10, 10));

		add(name,BorderLayout.NORTH);
		add(box2);

	}

	private void addButtons(YuraTextEditor ytp) {

		JButton save = new JButton("Save");
		save.setActionCommand("save");

		JButton revert = new JButton("Revert");
		revert.setActionCommand("revert");

		JButton setnull = new JButton("Remove");
		setnull.setActionCommand("setnull");

		ytp.add(save);
		ytp.add(revert);
		ytp.add(setnull);
	}

	public void reuse(MyNode a,Locale l1,Locale l2,Mtcomm com) throws Exception {

		name.setText("<html><b>&nbsp;&nbsp;"+a.getName()+"</b></html>");
		mycomm = com;
		message = a;

		localetop = l1;
		top.setText( message.getMessage(mycomm,localetop) );

		resetLocale(l2);

	}

	public void resetLocale(Locale a) throws Exception {

		localebottom = a;

		bottom.setLabel( "in "+localebottom );
		bottom.setText( message.getMessage(mycomm,localebottom) );

	}












	public void revert() {

		top.revert();
		bottom.revert();
	}


	public boolean checkChange() {

		return (

			top.checkChange() ||
			bottom.checkChange()

		);

	}

	public void saveChanges() throws Exception {


		saveChanges(top);
		saveChanges(bottom);

	}

	public void setWrap(boolean a) {

		top.setWrap(a);
		bottom.setWrap(a);

	}












	public void actionPerformed(ActionEvent ae) {


		if (ae.getActionCommand().equals("revert")) {

			((YuraTextEditor)ae.getSource()).revert();

		}
		else if (ae.getActionCommand().equals("save")) {

			try {

				saveChanges( (YuraTextEditor)ae.getSource() );

			}
			catch(Exception e) {

				showError( "unable to save: "+e.getMessage() );

			}


		}
		else if (ae.getActionCommand().equals("setnull")) {

			try {


				if (ae.getSource() == top) {

					message.saveMessage(mycomm, localetop, null );

					top.setText( null );

				}
				else if (ae.getSource() == bottom) {

					message.saveMessage(mycomm, localebottom, null );

					bottom.setText( null );

				}


			}
			catch(Exception e) {

				showError( "unable to set to null: "+e.getMessage() );
			}

		}
		else {

			throw new RuntimeException( "unknown command "+ ae.getActionCommand() );

		}
	}






	public void showError(String a) {

		JOptionPane.showMessageDialog(this, a , "error!", JOptionPane.ERROR_MESSAGE );

	}






	public void saveChanges(YuraTextEditor a) throws Exception {

		if ( a.checkChange() ) {

			message.saveMessage(mycomm, (a == top)?(localetop):(localebottom), a.getText() );

			// resets the background color to white
			a.setText( a.getText() );

		}

	}





}
