/**
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jivesoftware.sparkimpl.settings.local;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jivesoftware.Spark;
import org.jivesoftware.resource.Default;
import org.jivesoftware.resource.Res;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.VerticalFlowLayout;
import org.jivesoftware.spark.util.ResourceUtils;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.plugin.manager.Enterprise;

/**
 * UI for editing Local Preferences.
 */
public class LocalPreferencePanel extends JPanel {
    private static final long serialVersionUID = -1675058807882383560L;

	private final JTextField _portField = new JTextField();
    private final JTextField _timeOutField = new JTextField();
	private final JTextField _reconnectDelayField = new JTextField();
    private final JTextField _idleField = new JTextField();
    private final JTextField _idleStatusText;

    private final JCheckBox _autoLoginBox = new JCheckBox();
    private final JCheckBox _savePasswordBox = new JCheckBox();
    private final JCheckBox _idleBox = new JCheckBox();
    private final JCheckBox _launchOnStartupBox = new JCheckBox();
    private final JCheckBox _startMinimizedBox = new JCheckBox();
    private final JCheckBox _useSingleTrayClick = new JCheckBox();
    

    /**
     * Construct Local Preference UI.
     */
    public LocalPreferencePanel() {
	setLayout(new VerticalFlowLayout());


	// Load local localPref
	LocalPreferences preferences = SettingsManager.getLocalPreferences();
	_portField.setText(Integer.toString(preferences.getXmppPort()));
	_timeOutField.setText(Integer.toString(preferences.getTimeOut()));
	_reconnectDelayField.setText(Integer.toString(preferences.getReconnectDelay()));
	_autoLoginBox.setSelected(preferences.isAutoLogin());
	_savePasswordBox.setSelected(preferences.isSavePassword());
	_startMinimizedBox.setSelected(preferences.isStartedHidden());
	_useSingleTrayClick.setSelected(preferences.isUsingSingleTrayClick());
	
	_idleStatusText = new JTextField(preferences.getIdleMessage());
	if (preferences.isSSOEnabled()) {
		_savePasswordBox.setSelected(false);
		_autoLoginBox.setEnabled(true);
	} else {
		_savePasswordBox.addActionListener(e -> {
			_autoLoginBox.setEnabled(_savePasswordBox.isSelected());
            if (_savePasswordBox.isSelected()) {
                String user = SparkManager.getSessionManager().getUserBareAddress().toString();
                String password = SparkManager.getSessionManager().getPassword();
                try {
                    preferences.setPasswordForUser(user,password);
                } catch (Exception ex) {
                    Log.error("Error storing encrypted password.", ex);
                }
            }
            if (!_savePasswordBox.isSelected()) {
				_autoLoginBox.setSelected(false);
				try {
					preferences.clearPasswordForAllUsers();
				} catch (Exception e1) {
					Log.debug("Unable to clear saved password..." + e1);
				}

			}
		});
	}

	_autoLoginBox.addActionListener( e -> {
    if ((_autoLoginBox.isSelected()) && (!preferences.isSSOEnabled())) {
        _savePasswordBox.setSelected(true);
    }
    } );

	_idleBox.addActionListener( e -> _idleField.setEnabled(_idleBox.isSelected()) );


	_idleBox.setSelected(preferences.isIdleOn());
	_idleField.setText(Integer.toString(preferences.getIdleTime()));

	final JPanel inputPanel = new JPanel();
	inputPanel.setLayout(new GridBagLayout());
	inputPanel.setBorder(BorderFactory.createTitledBorder(Res
		.getString("group.login.information")));

		JLabel _portLabel = new JLabel();
		ResourceUtils.resLabel( _portLabel, _portField,Res.getString("label.xmpp.port") + ":");
		JLabel _timeOutLabel = new JLabel();
		ResourceUtils.resLabel( _timeOutLabel, _timeOutField,
		Res.getString("label.response.timeout") + ":");

		JLabel _reconnectDelayLabel = new JLabel();
		ResourceUtils.resLabel( _reconnectDelayLabel, _reconnectDelayField,
		Res.getString("label.reconnect.delay") + ":");

		JLabel _idleStatusLabel = new JLabel();
		ResourceUtils.resLabel( _idleStatusLabel, _idleStatusText,
		Res.getString("label.time.till.idlemessage") + ":");
	
	ResourceUtils.resButton(_autoLoginBox,
		Res.getString("checkbox.auto.login"));
	ResourceUtils.resButton(_savePasswordBox,
		Res.getString("checkbox.save.password"));
		JLabel _idleLabel = new JLabel();
		ResourceUtils.resLabel( _idleLabel, _idleField,
		Res.getString("label.time.till.idle") + ":");
	ResourceUtils.resButton(_idleBox,
		Res.getString("checkbox.idle.enabled"));
	
	ResourceUtils.resButton(_launchOnStartupBox,
		Res.getString("checkbox.launch.on.startup"));
	ResourceUtils.resButton(_startMinimizedBox,
		Res.getString("checkbox.start.in.tray"));
	ResourceUtils.resButton(_useSingleTrayClick,
			Res.getString("checkbox.click.single.tray"));
	
	inputPanel.add( _portLabel,    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	inputPanel.add(_portField,    new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,new Insets(5, 5, 5, 5), 0, 0));
	inputPanel.add( _timeOutLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add(_timeOutField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add( _reconnectDelayLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add(_reconnectDelayField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add( _idleLabel,    new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add(_idleField,    new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add( _idleStatusLabel,new GridBagConstraints(0, 4, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add(_idleStatusText, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
    inputPanel.add(_idleBox, new GridBagConstraints(0, 5, 2, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));

    if(Default.getBoolean(Default.IDLE_LOCK) || !Enterprise.containsFeature(Enterprise.IDLE_FEATURE)) {
        _idleField.setEnabled(false);
        _idleStatusText.setEnabled(false);
        _idleBox.setEnabled(false);
    }
    
    if(!Default.getBoolean(Default.HIDE_SAVE_PASSWORD_AND_AUTO_LOGIN) && SettingsManager.getLocalPreferences().getPswdAutologin()) {
		if (!preferences.isSSOEnabled()) {
			inputPanel.add(_savePasswordBox, new GridBagConstraints(0, 6, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
		}
	inputPanel.add(_autoLoginBox,    new GridBagConstraints(0, 7, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
	}
	
	if (Spark.isWindows()) {
	    inputPanel.add(_launchOnStartupBox, new GridBagConstraints(0, 8, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50,0));
	    _launchOnStartupBox.addActionListener( e -> setStartOnStartup(_launchOnStartupBox.isSelected()) );

	    _launchOnStartupBox.setSelected(preferences.getStartOnStartup());
	}

	inputPanel.add(_startMinimizedBox, new GridBagConstraints(0, 9, 2, 1,0.0, 0.0, GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add(_useSingleTrayClick, new GridBagConstraints(0, 10, 2, 1,0.0, 0.0, GridBagConstraints.NORTHWEST,GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
	inputPanel.add(new JLabel(), new GridBagConstraints(0, 11, 2, 1, 1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,new Insets(5, 5, 5, 5), 50, 0));
	
	
	add(inputPanel);
    }

    /**
     * Sets the XMPP port to comminucate on.
     * 
     * @param port
     *            the XMPP port to communicate on.
     */
    public void setPort(String port) {
	_portField.setText(port);
    }

    /**
     * Return the XMPP Port to communicate on.
     * 
     * @return the XMPP Port to communicate on.
     */
    public String getPort() {
	return _portField.getText();
    }

    /**
     * Sets the XMPP Timeout(in seconds).
     * 
     * @param timeOut
     *            the XMPP Timeout(in seconds).
     */
    public void setTimeOut(String timeOut) {
	_timeOutField.setText(timeOut);
    }

    /**
     * Return the XMPP Timeout variable.
     * 
     * @return the XMPP Timeout variable.
     */
    public String getTimeout() {
	return _timeOutField.getText();
    }

	public void setReconnectDelay(String reconnectDelay) {
		_reconnectDelayField.setText(reconnectDelay);
	}

	public String getReconnectDelay() {
		return _reconnectDelayField.getText();
	}

    /**
     * Sets Auto Login on and off.
     * 
     * @param auto
     *            true if Auto Login is on.
     */
    public void setAutoLogin(boolean auto) {
	_autoLoginBox.setSelected(auto);
    }

    /**
     * Return true if Auto Login is on.
     * 
     * @return true if Auto Login is on.
     */
    public boolean getAutoLogin() {
	return _autoLoginBox.isSelected();
    }

    /**
     * Set true if the password should be encoded and saved.
     * 
     * @param save
     *            true if the password should be encoded and saved.
     */
    public void setSavePassword(boolean save) {
	_savePasswordBox.setSelected(save);
    }

    /**
     * Return true if the password should be saved.
     * 
     * @return true if the password should be saved.
     */
    public boolean isSavePassword() {
	return _savePasswordBox.isSelected();
    }

    /**
     * Returns true if IDLE is on.
     * 
     * @return true if IDLE is on.
     */
    public boolean isIdleOn() {
	return _idleBox.isSelected();
    }

    /**
     * Sets the IDLE on or off.
     * 
     * @param on
     *            true if IDLE should be on.
     */
    public void setIdleOn(boolean on) {
	_idleBox.setSelected(on);
    }

    /**
     * Sets the Idle Time in minutes.
     * 
     * @param time
     *            the Idle time in minutes.
     */
    public void setIdleTime(int time) {
	String idleTime = Integer.toString(time);
	_idleField.setText(idleTime);
    }

    /**
     * Return the time to IDLE.
     * 
     * @return the time to IDLE.
     */
    public String getIdleTime() {
	return _idleField.getText();
    }

    public void startInSystemTray(boolean startInTray) {
	_startMinimizedBox.setSelected(startInTray);
    }

    public boolean startInSystemTray() {
	return _startMinimizedBox.isSelected();
    }

    public void useSingleClickInTray(boolean clickInTray) {
	_useSingleTrayClick.setSelected(clickInTray);
    }

    public boolean useSingleClickInTray() {
	return _useSingleTrayClick.isSelected();
    }
    
    public boolean startOnStartup() {
	return _launchOnStartupBox.isSelected();
    }

    public void setStartOnStartup(boolean startup) {
	_launchOnStartupBox.setSelected(startup);
    }
   
    public String getIdleMessage(){
	return _idleStatusText.getText();
    }
    public void setIdleMessage(String text){
	_idleStatusText.setText(text);
    }

}
