package com.gx.obe.management.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
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.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.auth.AuthUser;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class ExpertAvoidDialog extends EDialog {
	
	private Text revokeReasonText;
	private AuthUser user;
	private String avoidReason = "";
	private EButton revokeBtn;
	private ELabel memberName;
	private boolean avoidYes = false;
	
	{
		LogUtils.logClass(ExpertAvoidDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public ExpertAvoidDialog(Shell parentShell, AuthUser user) {
		super(parentShell);
		setTitle(Messages.Button_expertAvoid);
		this.user = user;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite top = (Composite) super.createDialogArea(parent);
		GridLayout gl_top = new GridLayout(1, false);
		gl_top.verticalSpacing = 0;
		gl_top.horizontalSpacing = 0;
		gl_top.marginHeight = 0;
		gl_top.marginWidth = 0;
		top.setLayout(gl_top);
		
		Composite container = new Composite(top, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.marginHeight = 10;
		gl_container.marginWidth = 10;
		container.setLayout(gl_container);
		
		memberName = new ELabel(container, SWT.WRAP);
		memberName.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		memberName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		memberName.setText("");
		
		ELabel lblsbd = new ELabel(container, SWT.WRAP);
		lblsbd.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		lblsbd.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		lblsbd.setText("回避后将不能参与当前项目的任何评审工作，并请在下面输入要回避的原因。");
		
		{
			ESepator label = new ESepator(top, SWT.SEPARATOR | SWT.HORIZONTAL);
			label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
			label.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		}
		Composite composite = new Composite(top, SWT.NONE);
		FillLayout fl_composite = new FillLayout(SWT.HORIZONTAL);
		fl_composite.marginWidth = 2;
		composite.setLayout(fl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_composite.heightHint = 80;
		composite.setLayoutData(gd_composite);
		
		revokeReasonText = new Text(composite, SWT.WRAP | SWT.V_SCROLL);
		revokeReasonText.setTextLimit(200);
		
		{
			ESepator label = new ESepator(top, SWT.SEPARATOR | SWT.HORIZONTAL);
			label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
			label.setBackground(ColorConstants.SEP_LIGHT_COLOR);
			
		}
		Composite composite_1 = new Composite(top, SWT.NONE);
		composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);
		
		revokeBtn = new EButton(composite_1, SWT.NONE);
		revokeBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				confirmAction();
			}
		});
		GridData gd_revokeBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		revokeBtn.setLayoutData(gd_revokeBtn);
		revokeBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		revokeBtn.setText(Messages.Button_confirm);
		
		EButton cancel_btn = new EButton(composite_1, SWT.NONE);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		GridData gd_cancleBtn = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		cancel_btn.setLayoutData(gd_cancleBtn);
		cancel_btn.setText(Messages.Button_close);

		init();
		return container;
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		if(null == user){
			revokeBtn.setEnabled(false);
		}else {
			memberName.setText(Messages.FullName+"："+ObjectUtils.getObjString(user.getUserName()));
		}
	}

	/**
	 * @Description: 确认回避
	 * @author guoyr
	 */
	private void confirmAction(){
		if(revokeBtn.getEnabled()){
			if(revokeReasonText.getText().trim().length() > 0){
				if(MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, Messages.Tips_expertAvoid, new String[]{Messages.Button_confirm, Messages.Button_cancel})){
					avoidReason = revokeReasonText.getText().trim();
					avoidYes = true;
				}else {
					avoidYes = false;
				}
			}else {
				MessageDialog.openWarning(getShell(), Messages.Dialog_warmTips, Messages.Tips_avoidReason);
				return;
			}
			this.getShell().close();
		}
	}
	
	private void cancelAction(){
		avoidYes = false;
		this.getShell().close();
	}

	public boolean isAvoidYes() {
		return avoidYes;
	}

	public void setAvoidYes(boolean avoidYes) {
		this.avoidYes = avoidYes;
	}

	public String getAvoidReason() {
		return avoidReason;
	}

	public void setAvoidReason(String avoidReason) {
		this.avoidReason = avoidReason;
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(387, 253);
	}
}
