/**
 * SettingsPanel.java
 * @date Jan 19, 2011
 * @author ricky barrette
 * @author Twenty Codes, LLC
 */
package com.TwentyCodes.java.OrderProcessor.UI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 * this panel will be used to display and handle application settings
 * @author ricky barrette
 */
public class SettingsPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 2568209868143577096L;
	public static final String IS_USING_REMOTE_SERVER = "use_remote_server";
	public static final String SERVER_LOCATION = "server_location";
	public static final String SERVER_PORT_NUMBER = "server_port_number";
	public static final String SERVER_USERNAME = "server_user_name";
	public static final String SERVER_PASSWORD = "server_parrword";
	private JTextField mServerLocation;
	private JTextField mPortNumber;
	private JTextField mUserName;
	private JPasswordField mUserPassword;
	private boolean isUsingRemoteServer;
	private JButton mSaveButton;
	private JCheckBox mUseRemoteDatabase;
	private Properties mProps;

	/**
	 * Creates a new SettingsPanel
	 * @author ricky barrette
	 */
	public SettingsPanel() {
		
		JLabel lblMysqlServerLocation = new JLabel("MYsql server location:");
		
		mServerLocation = new JTextField();
		mServerLocation.setColumns(10);
		
		mUseRemoteDatabase = new JCheckBox("Use Remote Database?");
		mUseRemoteDatabase.addActionListener(this);
		
		JLabel lblMysqlServerPort = new JLabel("MYsql server port:");
		
		mPortNumber = new JTextField();
		mPortNumber.setColumns(10);
		mPortNumber.setText("3306");
		
		JLabel lblMysql = new JLabel("MYsql serevr username:");
		
		JLabel lblMysqlServerPassword = new JLabel("MYsql server password:");
		
		mUserName = new JTextField();
		mUserName.setColumns(10);
		
		mUserPassword = new JPasswordField();
		mUserPassword.setColumns(10);
		
		mServerLocation.setEnabled(false);
		mPortNumber.setEnabled(false);
		mUserName.setEnabled(false);
		mUserPassword.setEnabled(false);
		
		mSaveButton = new JButton("Save");
		mSaveButton.addActionListener(this);
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(mUseRemoteDatabase)
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblMysqlServerLocation)
								.addComponent(lblMysqlServerPort)
								.addComponent(lblMysql)
								.addComponent(lblMysqlServerPassword))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(mServerLocation)
								.addComponent(mSaveButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(mUserPassword)
								.addComponent(mUserName)
								.addComponent(mPortNumber))
							.addContainerGap(139, Short.MAX_VALUE))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(mUseRemoteDatabase)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMysqlServerLocation)
						.addComponent(mServerLocation, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMysqlServerPort)
						.addComponent(mPortNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMysql)
						.addComponent(mUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMysqlServerPassword)
						.addComponent(mUserPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(mSaveButton)
					.addContainerGap(142, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

		// create and load default properties
		mProps = new Properties();
		

		
		mProps = new java.util.Properties();
		try {
			FileInputStream in = new FileInputStream(".settings.propertys");
			mProps.load(in);
			in.close();
			isUsingRemoteServer = Boolean.parseBoolean(mProps.getProperty(IS_USING_REMOTE_SERVER, "false"));
			
			if(isUsingRemoteServer){
				mServerLocation.setEnabled(true);
				mPortNumber.setEnabled(true);
				mUserName.setEnabled(true);
				mUserPassword.setEnabled(true);
				mUseRemoteDatabase.setSelected(true);
			}
				
			mServerLocation.setText(mProps.getProperty(SERVER_LOCATION));
			mPortNumber.setText(mProps.getProperty(SERVER_PORT_NUMBER));;
			mUserName.setText(mProps.getProperty(SERVER_USERNAME));;
		
				
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == mSaveButton){
			mProps.put(SERVER_LOCATION, mServerLocation.getText());
			mProps.put(SERVER_PORT_NUMBER, mPortNumber.getText());
			mProps.put(SERVER_USERNAME, mUserName.getText());
			mProps.put(SERVER_PASSWORD, new String(mUserPassword.getPassword()));
			try {
				 FileOutputStream out = new FileOutputStream(".settings.propertys");
				mProps.store(out, "---No Comment---");
				out.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-gene1rated catch block
				e1.printStackTrace();
			}
			
		}
		
		if(e.getSource() == mUseRemoteDatabase){
			mProps.put(IS_USING_REMOTE_SERVER, Boolean.toString(mUseRemoteDatabase.isSelected()));
			try {
				 FileOutputStream out = new FileOutputStream(".settings.propertys");
				mProps.store(out, "---No Comment---");
				out.close();
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			if(mUseRemoteDatabase.isSelected()){
				mServerLocation.setEnabled(true);
				mPortNumber.setEnabled(true);
				mUserName.setEnabled(true);
				mUserPassword.setEnabled(true);
			} else {
				mServerLocation.setEnabled(false);
				mPortNumber.setEnabled(false);
				mUserName.setEnabled(false);
				mUserPassword.setEnabled(false);
			}
		} 
		
		MainWindow.loadDatabase();
		
	}
}