package transformation.su_transformationplugin.listeners;

import transformation.associationtags.AssociationTag;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class JoinButtonListener implements ActionListener {
	
	JPanel actionTagsPanel;
	JPanel roleTagsPanel;
	JPanel joinedTagsPanel;
	JFrame frame;
	
	List<AssociationTag> joinedTags;

	public JoinButtonListener(JPanel actionTagsPanel, JPanel roleTagsPanel, JPanel joinedTagsPanel, 
			List<AssociationTag> actionTags, List<AssociationTag> roleTags, JFrame frame,
			List<AssociationTag> joinedTags) {
		
		this.actionTagsPanel = actionTagsPanel;
		this.roleTagsPanel = roleTagsPanel;
		this.joinedTagsPanel = joinedTagsPanel;
		this.frame = frame;
		this.joinedTags = joinedTags;
	}
	
	@Override
	/**
	 * This method joins the selected partial association tags together
	 */
	public void actionPerformed(ActionEvent arg0) {
		
		AssociationTag joinedTag = new AssociationTag();
		
		// Finds the selected information about the protected actions
		for (Component checkedTag : actionTagsPanel.getComponents()) {
			
			JCheckBox checkedBox = (JCheckBox) checkedTag;
						
			if (checkedBox.isSelected()) {
				
				// Inserts the information into a new, clean association tag
				joinedTag.setAction(checkedBox.getText());
				// De-selects the selected checkbox
				checkedBox.setSelected(false);
			}
		}
		
		// Finds the selected information about the protected actions		
		for (Component checkedTag : roleTagsPanel.getComponents()) {
			
			JCheckBox checkedBox = (JCheckBox) checkedTag;
			
			if (checkedBox.isSelected()) {
				
				// Inserts the information into a new, clean association tag
				String[] role = checkedBox.getText().split(", ");
				joinedTag.setUser(role[0]);
				joinedTag.setRole(role[1]);
				
				// De-selects the selected checkbox
				checkedBox.setSelected(false);
			}
		}
		
		// Consistency check: if information is missing from an association tag, it cannot be created
		String errorMessage = "";
		
		if (joinedTag.getAction() == null) {
			errorMessage += "action";
		}
		
		if (joinedTag.getUser() == null) {
			if (errorMessage.length() > 0) {
				errorMessage += ", ";
			}
			errorMessage += "user";
		}
		
		if (joinedTag.getRole() == null) {
			if (errorMessage.length() > 0) {
				errorMessage += ", ";
			}
			errorMessage += "role";
		}
		
		if (errorMessage.length() != 0) {
			JOptionPane.showMessageDialog(null, "The following elements are missing " +
					"and an association tag cannot be formed: "	+ errorMessage);
			return;
		}
		
		// Removes the messages stating that there are no joined association tags
		if (joinedTagsPanel.getComponent(0) instanceof JLabel) {
			joinedTagsPanel.remove(0);
		}
		
		// Adds the joined association tag to the list of complete tags
		joinedTags.add(joinedTag);
		
		JTextArea tagArea = new JTextArea(joinedTag.toString());
		tagArea.setBackground(Color.getColor("DAD8D1"));
		tagArea.setEditable(false);
		
		JPanel tagPanel = new JPanel();
		tagPanel.setLayout(new BoxLayout(tagPanel, BoxLayout.X_AXIS));
		tagPanel.add(tagArea);
		
		JButton unjoinButton = new JButton("Unjoin");
		unjoinButton.addActionListener(new UnjoinListener(frame, joinedTagsPanel, tagPanel, joinedTags));
		tagPanel.add(unjoinButton);
		
		frame.setVisible(false);
		joinedTagsPanel.add(tagPanel);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
	}

}
