package com.gx.obe.management.dialog;

import java.text.MessageFormat;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.VerifyPasswordListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.ValidateUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.management.Lisener.AddTempExperCallBack;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class AddTempExpertNumDialog extends EDialog {
	
	private AddTempExperCallBack addTempExperCallBack;
	private Text text;
	private Logger LOG = Logger.getLogger(AddTempExpertNumDialog.class);
	private Text password_text;
	private ELink showPassword;	
	
	{
		LogUtils.logClass(AddTempExpertNumDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public AddTempExpertNumDialog(Shell parentShell, AddTempExperCallBack addTempExperCallBack) {
		super(parentShell);
		setTitle(Messages.Button_createTempAccount);
		this.addTempExperCallBack = addTempExperCallBack;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite topContainer = (Composite) super.createDialogArea(parent);
		GridLayout gl_topContainer = new GridLayout(1, false);
		gl_topContainer.marginWidth = 15;
		gl_topContainer.marginHeight = 15;
		topContainer.setLayout(gl_topContainer);
		
		Composite composite_1 = new Composite(topContainer, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		GridLayout gl_composite_1 = new GridLayout(3, false);
		composite_1.setLayout(gl_composite_1);
		
		Label label_1 = new Label(composite_1, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_1.setText(Messages.Account_temp+"：");
//		label_1.setForeground(ColorConstants.FONT_BLACK_COLOR);
		
		text = new Text(composite_1, SWT.CENTER);
		text.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		GridData gd_text = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_text.widthHint = 40;
		text.setLayoutData(gd_text);
		text.setTextLimit(1);
		text.addVerifyListener(new VerifyListener(){     
			public void verifyText(VerifyEvent e) {     
				if(text.getText().length() == 0 && "0".equals(e.text)){
					e.doit = false;
					return;
				}
				e.doit = "0123456789".indexOf(e.text)>=0;     
			}     
		});
//		text.addVerifyListener(new VerifyNumberListener("请输入正确的专家数！"));
//		text.addVerifyListener(new VerifyMinEqListener(1, "请输入正确的专家数！"));
		
		
		Label numLabel = new Label(composite_1, SWT.NONE);
		numLabel.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, false, 1, 2));
		ESepator label_2 = new ESepator(composite_1, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, false, 1, 1));
		label_2.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Label label_password = new Label(composite_1, SWT.NONE);
		label_password.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
		label_password.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_password.setText(Messages.Initialize_password+"：");
//		label_password.setForeground(ColorConstants.FONT_BLACK_COLOR);
		
		password_text = new Text(composite_1, SWT.CENTER);
		password_text.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		GridData gd_password_text = new GridData(SWT.FILL, SWT.BOTTOM, false, true, 1, 1);
		gd_password_text.widthHint = 150;
		password_text.setLayoutData(gd_password_text);
		password_text.addVerifyListener(new VerifyRequiredListener(password_text, MessageFormat.format(Messages.Tips_required, Messages.Initialize_password)));
		password_text.addVerifyListener(new VerifyPasswordListener());
		password_text.setTextLimit(20);
		
		showPassword = new ELink(composite_1, SWT.NONE);
		showPassword.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
		showPassword.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				showOrHiddenPassword();
			}
		});
		showPassword.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/pwd_hidden.png"));
		
//		text.addVerifyListener(new VerifyNumberListener("请输入正确的专家数！"));
//		text.addVerifyListener(new VerifyMinEqListener(1, "请输入正确的专家数！"));
		
		ESepator sep_password = new ESepator(composite_1, SWT.NONE);
		sep_password.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		sep_password.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Label passwordTips = new Label(topContainer, SWT.WRAP);
		passwordTips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		passwordTips.setText(VerifyPasswordListener.PASSWOR_TIPS);
		passwordTips.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		EButton button = new EButton(topContainer, SWT.NONE);
		button.setBackground(ColorConstants.SKIN_BG);
		button.setForeground(ColorConstants.SKIN_FOREGROUND);
		button.setActiveBorderColor(ColorConstants.SKIN_ACTIVE_BG);
		button.setActiveBgColor(ColorConstants.SKIN_ACTIVE_BG);
		button.setActiveForeground(ColorConstants.SKIN_ACTIVE_FOREGROUND);
		button.setArcWidth(6);
		button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		button.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				save();
			}
		});
		button.setText(Messages.Button_ok);
		init();
		return topContainer;
	}

	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		password_text.setText(PropertiesUtils.getProperty(ConfigConstants.EXPERT_DEFAULT_PASSWORD, "123456"));
		showPassword.setData("HIDE", "01");
		password_text.setEchoChar('*');
		showOrHiddenPassword();
	}
	
	private void showOrHiddenPassword() {
		if(null == showPassword.getData("HIDE")) {
			showPassword.setData("HIDE", "01");
			password_text.setEchoChar(text.getEchoChar());
			password_text.redraw();
			showPassword.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/pwd_hidden.png"));
		}else {
			password_text.setEchoChar('*');
			showPassword.setData("HIDE", null);
			showPassword.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/pwd_view.png"));
		}
	}
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(424, 238);
	}
	
	/**
	 * @Description: 点击确定按钮操作
	 * @author guoyr 
	 */
	private void save(){
		String value = text.getText().trim();
		int num = ObjectUtils.getIntValue(value, 0);
		if(num > 0){
			if (!ValidateUtils.checkInput(password_text.getParent())) {
				return;
			}
			addTempExperCallBack.addTempExper(num, password_text.getText().trim());	
			this.getShell().close();
		}else {
			MessageDialog.openShortMessage("请输入有效的临时账号数！");
		}
	}
}
