package gui;



import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import system.JadeManager;


/**
 * GUI zawierające logowanie do głównej platformy.
 * 
 * @author Mateusz Kaflowski <mkaflowski@gmail.com>
 */
public class LoginFrame extends JFrame {

	private JPanel contentPane;
	private JTextField txtProducentName;
	private JTextField txtPlatformAdress;
	private JTextField txtPortNumber;
	


	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		setTitle("Log In");


		setBounds(100, 100, 324, 313);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);

		JLabel lblProducentName = new JLabel("Producent name");
		lblProducentName.setHorizontalAlignment(SwingConstants.CENTER);
		lblProducentName.setBounds(82, 40, 128, 14);
		contentPane.add(lblProducentName);

		txtProducentName = new JTextField();
		txtProducentName.setBounds(82, 59, 128, 20);
		contentPane.add(txtProducentName);
		txtProducentName.setColumns(10);

		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
			}
		});
		btnLogin.setBounds(82, 200, 134, 50);
		contentPane.add(btnLogin);

		JLabel lblPlatformAdress = new JLabel("Platform adress");
		lblPlatformAdress.setHorizontalAlignment(SwingConstants.CENTER);
		lblPlatformAdress.setBounds(82, 90, 128, 14);
		contentPane.add(lblPlatformAdress);

		txtPlatformAdress = new JTextField();
		txtPlatformAdress.setText("localhost");
		txtPlatformAdress.setColumns(10);
		txtPlatformAdress.setBounds(82, 107, 128, 20);
		contentPane.add(txtPlatformAdress);

		txtPortNumber = new JTextField();
		txtPortNumber.setText("1099");
		txtPortNumber.setColumns(10);
		txtPortNumber.setBounds(82, 149, 128, 20);
		contentPane.add(txtPortNumber);

		JLabel lblPortNumber = new JLabel("Port number");
		lblPortNumber.setHorizontalAlignment(SwingConstants.CENTER);
		lblPortNumber.setBounds(82, 132, 128, 14);
		contentPane.add(lblPortNumber);
		
	}

	/** Metoda logująca użytkownika do platformy.*/
	protected void login() {
		String tab[] = { "-host", txtPlatformAdress.getText(), "-port", txtPortNumber.getText(), "-exitwhenempty", "true" };
		
		String username = txtProducentName.getText();
		if(username==null || username.equals("")){
			JOptionPane.showMessageDialog(null, "Incorrect username.","Warning",
				    JOptionPane.WARNING_MESSAGE);
			return;
		}

		final JadeManager jadeManager = new JadeManager();

		jadeManager.startRuntime(tab);
		jadeManager.startAgent(username);
		
		MyFrame frame = new MyFrame(jadeManager);
		frame.setVisible(true);
		dispose();
	}
}