/*
 * Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.
 *
 * EXPORT CONTROLLED
 * ECCN#   EAR99
 * Warning: This document contains technical data which is controlled under the
 * Bureau of Industry & Security's Export Administration Regulations and may not be
 * exported or re-exported without authorization by the Bureau of Industry & Security.
 * Diversion contrary to US law is prohibited.
 *
 * (c) 2011 United Technologies Corp.
 * Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this data,
 * including any software or models in source or binary form, as well as any drawings, 
 * specifications, and documentation (collectively "the Data"), to deal in the Data without 
 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Data.
 *
 * THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE
 * USE OR OTHER DEALINGS IN THE DATA.
*/
package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import proxy.clients.RepositoryProxy;
import exception.ControllerException;

/**
 * The application view object
 */
public class ApplicationView extends JDialog 
	implements ItemListener, ActionListener {
	
	private static final long serialVersionUID = 1L;
	private final JPanel      contentPanel;
	private Set<String>       selections;
	private RepositoryProxy   repository;
	private String            platform;
	private String            level;

	/**
	 * Create the dialog.
	 */
	public ApplicationView(Set<Integer> archs,
			  	               RepositoryProxy repository,
                         String platform,
                         String level) {
		contentPanel    = new JPanel();
		this.repository = repository;
		this.platform   = platform;
		this.level      = level;
		selections      = new HashSet<String>();
		
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
			
		JButton okButton = new JButton("OK");
		okButton.setActionCommand("OK");
		okButton.addActionListener(this);
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
			
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		cancelButton.addActionListener(this);
		buttonPane.add(cancelButton);
		
		populateArchList(archs);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	/**
	 * Populate the list of architectures on the view
	 * @param archs	List of architecture id's
	 */
	private void populateArchList(Set<Integer> archs) {
		for(Integer arch : archs) {
			JCheckBox jcb = new JCheckBox(String.format("ID: %d", arch));
			jcb.setActionCommand(String.format("%d", arch));
			jcb.addItemListener(this);
			contentPanel.add(jcb);
		}		
	}

	/**
	 * Handler for when a check box is pressed on the view
	 */
	@Override
	public void itemStateChanged(ItemEvent cbevent) {
		String selection = ((JCheckBox) cbevent.getItemSelectable())
		                                       .getActionCommand();
		System.out.println(String.format("%s Pressed", selection));
		
		if(cbevent.getStateChange() == ItemEvent.DESELECTED) {
			selections.remove(selection);
		} else if (cbevent.getStateChange() == ItemEvent.SELECTED) {
			selections.add(selection);
		}
	}

	/**
	 * Handler for when a button is pressed on the view
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand() == "OK") {
			try {
				repository.storeMarkedArch(selections, platform, level);
				this.dispose();
			} catch (ControllerException cex) {
				JOptionPane.showMessageDialog(contentPanel,
				    cex.getMessage(),
				    "Repository error",
				    JOptionPane.ERROR_MESSAGE);
			}
			
		} else if (arg0.getActionCommand() == "Cancel") {
			this.dispose();
		}
	}
}
