package rec11;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.*;

@SuppressWarnings("serial")
/** PairSorter is a toy GUI program to help get you used to using
 * anonymous functions. It has a sort button, a list of pairs, and
 * a radio button that decides whether the pairs are sorted by their
 * first element or second element.
 */
public class PairSorter extends JFrame {


	ArrayList<Pair> pairs; // list of Pairs; always displayed in listDisplay
	
	// true iff leftButton is enabled, false iff rightButton is enabled.
	// if true, the sort button sorts pairs by the first element of each pair
	// if false, it sorts by the second element
	boolean sortByFirst= true; 

	/** create the gui */
	public static void main(String[] pars) {
		PairSorter gui= new PairSorter();
	}

	/** constructor: set up the GUI and an empty tuple list */
	public PairSorter() {
		super("PairSorter");
		
		// setup the list
		pairs = new ArrayList<Pair>();
		
		// text area to display the list
		JTextArea listDisplay = new JTextArea("", 10, 5);
		
		// set up the radio buttons to choose which element to sort by
		JPanel chooserPanel= new JPanel();
		JRadioButton leftButton = new JRadioButton("Left", true);
		JRadioButton rightButton = new JRadioButton("Right");
		ButtonGroup buttons = new ButtonGroup();
		buttons.add(leftButton);
		buttons.add(rightButton);
		leftButton.setEnabled(sortByFirst);
		chooserPanel.add(leftButton);
		chooserPanel.add(rightButton);
		
		// TODO 1: set up action listeners for the two radio buttons to
		// maintain the class invariant (in particular, see sortByFirst)
		
		
		// set up the add fields/button
		JPanel addPanel= new JPanel();
		JTextField firstField = new JTextField(3);
		JTextField secondField = new JTextField(3);
		JButton addButton = new JButton("Add");
		addPanel.add(firstField);
		addPanel.add(secondField);
		addPanel.add(addButton);
		// TODO 2: set up an actionlistener for the addButton.
		// It should add a new pair with the values given in firstField
		// and secondField to the list, then re-display the updated list.

		
		// set up the sort button
		JButton sortButton= new JButton("Sort");
		
		// TODO 4: set up an actionlistener for the sort button.
		// it should sort the array and re-display the updated list.


		
		// add components to gui and display
		Container cp= getContentPane();
		cp.add(addPanel, BorderLayout.WEST);
		cp.add(listDisplay, BorderLayout.CENTER);
		cp.add(sortButton, BorderLayout.SOUTH);
		cp.add(chooserPanel, BorderLayout.NORTH);
		
		setResizable(false);
		pack();
		setVisible(true);
	}

	/** sort pairs by the first if sortByFirst is true, by
	 * second otherwise */
	private void sort() {
		// TODO 3: sort the pairs array according to the spec.
		// Use Collections.sort and an anonymous function as the comparator
		
	}
	
	/** display the list elements separated by newlines in the given JTextArea */
	private void displayList(JTextArea ta) {
		String s = "";
		for (Pair p : pairs) {
			s = s + p + "\n";
		}
		ta.setText(s);
	}
	
	/** inner class representing pairs of integers */
	class Pair {
		private int first;
		private int second;
		
		/** constructor: create the pair (a, b) */
		public Pair(int a, int b) {
			first= a;
			second= b;
		}
		
		/** constructor: create pair (a,b), parsing integers from strings a,b
		 * precondition: a and b are strings that can be parsed to integers */
		public Pair(String a, String b) {
			this(Integer.parseInt(a), Integer.parseInt(b));
		}
		
		/** return parenthesized representation of the pair (first, second) */
		public String toString() {
			return "(" + first +  ", " + second + ")";
		}
	}

}
