package com.gx.obe.evaluation.step.dialog;

import java.text.MessageFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.listener.TextHintListener;
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.ImageButton;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.common.widget.button.ImageRaido;
import com.gx.obe.message.Messages;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 审核
 * @author chenxw 
 */
public class AuditDialog extends EDialog {
	private Text messageText;
	private Composite buttonComposite;
	private Composite messageComposite;
	private int pointY = 120;
	private boolean isAdopt = true;
	private AuditListener listener;
	
	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param callBack 
	 */
	public AuditDialog(Shell parentShell) {
		super(parentShell);
		setTitle(Messages.Button_approval);
	}
	
	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false));
		
		buttonComposite = new Composite(container, SWT.NONE);
		buttonComposite.setLayout(new GridLayout(2, false));
		buttonComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		
		createImageRaido(Messages.Audit_adopt, true).setSelection(true);
		createImageRaido(Messages.Audit_return, false);
		
		messageComposite = new Composite(container, SWT.NONE);
		messageComposite.setLayout(new GridLayout(1, false));
		messageComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		messageComposite.setVisible(false);
		((GridData) messageComposite.getLayoutData()).exclude = true;
		
		messageText = new Text(messageComposite, SWT.BORDER | SWT.V_SCROLL);
		messageText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		messageText.setTextLimit(500);
		messageText.addPaintListener(new TextHintListener(MessageFormat.format(Messages.Tips_pleaseEnter, Messages.Title_reBackReason), 500));
		messageText.addVerifyListener(new VerifyRequiredListener(messageText, MessageFormat.format(Messages.Tips_pleaseEnter, Messages.Title_reBackReason)));
		
		Composite composite_2 = new Composite(container, SWT.NONE);
		composite_2.setLayout(new GridLayout(1, false));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		EButton confirmButton = new EButton(composite_2, SWT.NONE);
		confirmButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				confirmAction();
			}
		});
		confirmButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		confirmButton.setText(Messages.Button_confirm);
		confirmButton.setLeftMargin(50);
		
		return container;
	}
	
	/**
	 * @Description: 添加按钮
	 * @author chenxw
	 * @param name
	 * @param isAdopt
	 * @return 
	 */
	private ImageButton createImageRaido(String name, final boolean isAdopt) {
		final ImageRaido imageButton = new ImageRaido(buttonComposite, SWT.NONE);
		imageButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));
		imageButton.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		imageButton.setText(name);
		imageButton.setImageSize(24);
		imageButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				imageButton.setSelection(true);
				selectionAction(isAdopt);
			}
		});
		return imageButton;
	}
	
	/**
	 * @Description: 选择事件
	 * @author chenxw
	 * @param isAdopt 
	 */
	private void selectionAction(boolean isAdopt) {
		this.isAdopt = isAdopt;
		
		messageComposite.setVisible(!isAdopt);
		((GridData) messageComposite.getLayoutData()).exclude = isAdopt;
		messageComposite.getParent().layout();
		Point location = getShell().getLocation();
		if (isAdopt) {
			messageText.setText("");
			getShell().setSize(getInitialSize().x, pointY);
		} else {
			getShell().setSize(getInitialSize().x, 300);
		}
		getShell().setLocation(location);
		getShell().layout();
	}
	
	/**
	 * @Description: 确认事件
	 * @author chenxw 
	 */
	private void confirmAction() {
		if (!isAdopt) {
			if (!ValidateUtils.checkInput(messageText.getParent())) {
				return;
			}
		}
		if (null != listener) {
			listener.audit(isAdopt, messageText.getText().trim());
		}
		getShell().close();
	}
	
	public interface AuditListener {
		void audit(boolean isAdopt, String message);
		boolean isSuccess();
	}
	
	public void addAuditListener(AuditListener listener) {
		this.listener = listener;
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(400, pointY);
	}
	
}
