/*******************************************************************************
 * (C) Copyright 2016 Jérôme Comte and Dorian Cransac
 *  
 *  This file is part of djigger
 *  
 *  djigger is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  djigger is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *  
 *  You should have received a copy of the GNU Affero General Public License
 *  along with djigger.  If not, see <http://www.gnu.org/licenses/>.
 *
 *******************************************************************************/
package io.djigger.ui.connectiondialog;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import io.djigger.ui.SessionConfiguration;
import io.djigger.ui.SessionConfiguration.SessionParameter;

public class HostConnectionParameter implements CaretListener, ConnectionParameterFrame {

	private boolean defaultName;
	
	private final JPanel hostParametersPane;
	
	private final JTextField name;

	private final JTextField host;

	private final JTextField port;
	
	private final JTextField username;

	private final JTextField password;
	
	private ConnectionType connectionType;

	public HostConnectionParameter() {
		super();
				
		defaultName = true;
		
		JPanel namePane = new JPanel(new GridLayout(0, 2));
		namePane.add(new JLabel("Name"));
		name = new JTextField(20);
		name.addCaretListener(new CaretListener() {
			@Override
			public void caretUpdate(CaretEvent e) {
				defaultName = false;
			}
		});
		namePane.add(name);
		
		
		hostParametersPane = new JPanel(new GridLayout(0, 2));
		hostParametersPane.add(new JLabel("Host"));
		host = new JTextField(20);
		host.addCaretListener(this);
		hostParametersPane.add(host);
		hostParametersPane.add(new JLabel("Port"));
		port = new JTextField(5);
		port.addCaretListener(this);
		hostParametersPane.add(port);		
		hostParametersPane.add(new JLabel("Username"));
		username = new JTextField(20);
		hostParametersPane.add(username);
		hostParametersPane.add(new JLabel("Password"));
		password = new JTextField(20);
		hostParametersPane.add(password);
		
		host.requestFocusInWindow();
		
		
	}
	

	@Override
	public void caretUpdate(CaretEvent e) {
		if(defaultName) {
			name.setText(host.getText()+":"+port.getText());
			defaultName = true;
		}
	}
	
	public String getName() {
		return name.getText();
	}

	public String getHost() {
		return host.getText();
	}

	public String getPort() {
		return port.getText();
	}

	public String getUsername() {
		return username.getText();
	}

	public String getPassword() {
		return password.getText();
	}
	
	public SessionConfiguration getSessionConfiguration() {
		SessionConfiguration config = new SessionConfiguration(getName(), connectionType.getSessionType());
		
		config.getParameters().put(SessionParameter.HOSTNAME, getHost());
		config.getParameters().put(SessionParameter.PORT, getPort());
		config.getParameters().put(SessionParameter.USERNAME, getUsername());
		config.getParameters().put(SessionParameter.PASSWORD, getPassword());
		
		return config;
	}


	@Override
	public JPanel getPanel() {
		return hostParametersPane;
	}


	@Override
	public void setConnectionType(ConnectionType type) {
		connectionType = type;
	}


	@Override
	public void setReloadListener(ReloadListener listener) {}
	
}

@@ -991,17 +991,16 @@
 Layout;%0A
-%0A
 import j
@@ -1006,50 +1006,32 @@
 java
-x.swing.JLabel;%0Aimport javax.swing.JPanel;
+.awt.event.ActionEvent;%0A
 %0Aimp
@@ -1046,26 +1046,17 @@
 x.swing.
-JTextField
+*
 ;%0Aimport
@@ -1547,28 +1547,32 @@
 vate final J
-Text
+Password
 Field passwo
@@ -2493,36 +2493,40 @@
 password = new J
-Text
+Password
 Field(20);%0A%09%09hos
@@ -2556,18 +2556,388 @@
 sword);%0A
-%09%09
+%0A%0A%09%09JPopupMenu passwordMenu = new JPopupMenu();%0A%09%09password.setComponentPopupMenu(passwordMenu);%0A%09%09password.setEchoChar('*');%0A%0A%09%09passwordMenu.add(new JMenuItem(new AbstractAction(%22Show/hide%22) %7B%0A%09%09%09@Override%0A%09%09%09public void actionPerformed(ActionEvent e) %7B%0A%09%09%09%09char echo = password.getEchoChar();%0A%09%09%09%09echo = (echo == 0) ? '*' : 0;%0A%09%09%09%09password.setEchoChar(echo);%0A%09%09%09%7D%0A%09%09%7D));%0A
 %0A%09%09host.

Mask passwords by default, with context option to unmask