/*******************************************************************************
 * DISCLAIMER: The sample code or utility or tool described herein
 *    is provided on an "as is" basis, without warranty of any kind.
 *    UIDAI does not warrant or guarantee the individual success
 *    developers may have in implementing the sample code on their
 *    environment. 
 *    
 *    UIDAI does not warrant, guarantee or make any representations
 *    of any kind with respect to the sample code and does not make
 *    any representations or warranties regarding the use, results
 *    of use, accuracy, timeliness or completeness of any data or
 *    information relating to the sample code. UIDAI disclaims all
 *    warranties, express or implied, and in particular, disclaims
 *    all warranties of merchantability, fitness for a particular
 *    purpose, and warranties related to the code, or any service
 *    or software related thereto. 
 *    
 *    UIDAI is not responsible for and shall not be liable directly
 *    or indirectly for any direct, indirect damages or costs of any
 *    type arising out of use or any action taken by you or others
 *    related to the sample code.
 *    
 *    THIS IS NOT A SUPPORTED SOFTWARE.
 ******************************************************************************/

package in.gov.uidai.tools;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

/**
 * Main UI client class
 * 
 * @author Anujit
 */
public class ToolsFrame extends javax.swing.JFrame {

	private String appVersion = "Auth Tools 1.0.0";

	// Menu components
	private javax.swing.JMenuBar jMenuBar;
	private javax.swing.JMenu jMenuFile, jMenuOptions;
	private javax.swing.JMenuItem jMenuItem1;

	// Parent panel
	private JPanel mainPanel, isoTestPanel;
	// Parent tab
	private JTabbedPane tabbedPane;

	// Device Test tab components
	private JButton btnIsoCheck, btnIirTest, btnTemplateCheck;
	private JButton btnIir2Test;
	private JLabel lblImage;
	private JTextPane textPane;

	/**
	 * Main class constructor
	 * 
	 * @throws URISyntaxException
	 */
	public ToolsFrame(final int tab) throws URISyntaxException {
		setResizable(false);
		getContentPane().setBackground(SystemColor.inactiveCaption);
		setTitle(appVersion);
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {

		mainPanel = new javax.swing.JPanel();

		jMenuBar = new javax.swing.JMenuBar();
		jMenuFile = new javax.swing.JMenu();
		jMenuFile.setForeground(Color.BLACK);
		jMenuFile.setFont(new Font("Tahoma", Font.PLAIN, 12));
		jMenuItem1 = new javax.swing.JMenuItem();
		jMenuItem1.setFont(new Font("Tahoma", Font.PLAIN, 12));
		jMenuOptions = new javax.swing.JMenu();
		jMenuOptions.setFont(new Font("Tahoma", Font.PLAIN, 12));

		mainPanel.setBackground(new Color(51, 153, 153));

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setFont(new Font("Tahoma", Font.BOLD, 11));
		tabbedPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		tabbedPane.setBackground(new Color(51, 153, 153));

		javax.swing.GroupLayout gl_mainPanel = new javax.swing.GroupLayout(mainPanel);
		gl_mainPanel.setHorizontalGroup(gl_mainPanel.createParallelGroup(Alignment.LEADING).addGroup(
				gl_mainPanel.createSequentialGroup().addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 1006, GroupLayout.PREFERRED_SIZE).addContainerGap(2210, Short.MAX_VALUE)));
		gl_mainPanel.setVerticalGroup(gl_mainPanel.createParallelGroup(Alignment.LEADING).addGroup(
				gl_mainPanel.createSequentialGroup().addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 546, GroupLayout.PREFERRED_SIZE).addContainerGap(175, Short.MAX_VALUE)));

		isoTestPanel = new JPanel();
		isoTestPanel.setBackground(new Color(51, 153, 153));
		isoTestPanel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(70, 130, 180)));
		tabbedPane.addTab("ISO Test", null, isoTestPanel, null);
		tabbedPane.setBackgroundAt(0, SystemColor.inactiveCaptionText);
		tabbedPane.setForegroundAt(0, Color.BLACK);
		isoTestPanel.setLayout(null);

		JLabel logo = new JLabel("");
		logo.setIcon(new ImageIcon("resources/adhaarLogoSmall.jpg"));
		logo.setBounds(929, 11, 62, 40);
		isoTestPanel.add(logo);

		textPane = new JTextPane();
		textPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		textPane.setFont(new Font("Tahoma", Font.PLAIN, 20));
		textPane.setBackground(new Color(255, 255, 255));
		textPane.setEditable(false);
		textPane.setVisible(true);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(25, 111, 949, 378);
		isoTestPanel.add(scrollPane);
		scrollPane.setViewportView(textPane);

		btnIsoCheck = new JButton("FIR Test");
		btnIsoCheck.setFont(new Font("Tahoma", Font.PLAIN, 18));
		btnIsoCheck.setBounds(25, 29, 139, 32);
		btnIsoCheck.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnIsoCheckActionPerformed("FIR", e);
			}
		});
		isoTestPanel.add(btnIsoCheck);

		btnTemplateCheck = new JButton("FMR Test");
		btnTemplateCheck.setFont(new Font("Tahoma", Font.PLAIN, 18));
		btnTemplateCheck.setBounds(186, 29, 139, 32);
		btnTemplateCheck.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnIsoCheckActionPerformed("FMR", e);
			}
		});
		isoTestPanel.add(btnTemplateCheck);

		lblImage = new JLabel("");
		lblImage.setBackground(new Color(0, 0, 0));
		lblImage.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblImage.setForeground(new Color(255, 255, 255));
		lblImage.setHorizontalAlignment(SwingConstants.CENTER);
		lblImage.setBounds(794, 11, 81, 103);
		isoTestPanel.add(lblImage);

		btnIirTest = new JButton("IIR Test");
		btnIirTest.setFont(new Font("Tahoma", Font.PLAIN, 18));
		btnIirTest.setBounds(348, 29, 139, 32);
		isoTestPanel.add(btnIirTest);
		btnIirTest.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnIsoCheckActionPerformed("IIR", e);
			}
		});

		btnIir2Test = new JButton("IIR 2011 Test");
		btnIir2Test.setEnabled(false);
		btnIir2Test.setFont(new Font("Tahoma", Font.PLAIN, 18));
		btnIir2Test.setBounds(507, 29, 149, 32);
		isoTestPanel.add(btnIir2Test);

		btnIir2Test.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnIsoCheckActionPerformed("IIR2011", e);
			}
		});

		mainPanel.setLayout(gl_mainPanel);
		jMenuFile.setText("File");
		jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK));
		jMenuItem1.setText("Exit");
		jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				System.exit(0);
			}
		});

		jMenuFile.add(jMenuItem1);
		jMenuBar.add(jMenuFile);
		setJMenuBar(jMenuBar);
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
		getContentPane().add(mainPanel);
		pack();
	}

	private void btnIsoCheckActionPerformed(String type, java.awt.event.ActionEvent evt) {
		JFileChooser chooser;
		chooser = new JFileChooser();
		chooser.setCurrentDirectory(new java.io.File("."));
		chooser.setDialogTitle("Choose ISO image file");
		int rc = chooser.showOpenDialog(this);
		textPane.setText(null);
		lblImage.setIcon(null);

		try {
			while (rc == JFileChooser.APPROVE_OPTION && !chooser.getSelectedFile().getName().endsWith(".iso")) {
				JOptionPane.showMessageDialog(null, "The file " + chooser.getSelectedFile() + " is not iso image file.\nThe selected file should have .iso extension.", "Open Error",
						JOptionPane.ERROR_MESSAGE);
				rc = chooser.showOpenDialog(this);
			}
			if (rc == JFileChooser.APPROVE_OPTION) {
				if ("FIR".equalsIgnoreCase(type)) {
					AppUtil.getIsoFirChecked(textPane, chooser.getSelectedFile(), chooser.getSelectedFile().getName(), lblImage);
				} else if ("FMR".equalsIgnoreCase(type)) {
					AppUtil.getIsoFmrChecked(textPane, chooser.getSelectedFile(), chooser.getSelectedFile().getName());
				} else if ("IIR".equalsIgnoreCase(type)) {
					AppUtil.getIsoIIRChecked(textPane, chooser.getSelectedFile(), chooser.getSelectedFile().getName());
				} else if (type.equalsIgnoreCase("IIR2011")) {
					AppUtil.getIsoIIR2011Checked(textPane, chooser.getSelectedFile(), chooser.getSelectedFile().getName());
				}
			}
		} catch (FileNotFoundException fx) {
			textPane.setText(fx.getMessage());
			fx.printStackTrace();
		} catch (IOException iex) {
			textPane.setText(iex.getMessage());
			iex.printStackTrace();
		}
	}
}