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

import java.text.MessageFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
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.ESepator;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
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.common.widget.button.Tips;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
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.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class AddExpertSignDialog extends EDialog {
	
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private ImageResolver imageResolver = new ImageResolver();
	private Text stepNameText;
	private Text stepMemoText;
	private CallBack callBack;
	private EButton save_btn;
	private Composite signNoteComposite;
	private Button noteYesBtn;
	
	{
		LogUtils.logClass(AddExpertSignDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public AddExpertSignDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, EvaluationStep evaluationStep) {
		super(parentShell);
		setTitle(Messages.Step_step);
		this.tenderProjectEntity= tenderProjectEntity;
		this.evaluationStep= evaluationStep;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite topContainer = (Composite) super.createDialogArea(parent);
		topContainer.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		GridLayout gl_topContainer = new GridLayout(1, false);
		gl_topContainer.verticalSpacing = 0;
		gl_topContainer.horizontalSpacing = 0;
		gl_topContainer.marginHeight = 0;
		gl_topContainer.marginWidth = 0;
		topContainer.setLayout(gl_topContainer);
		
		Composite container = new Composite(topContainer, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.marginHeight = 10;
		gl_container.marginWidth = 10;
		container.setLayout(gl_container);
		
		Tips label_6 = new Tips(container, SWT.WRAP);
		GridData gd_label_6 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_label_6.widthHint = 254;
		label_6.setLayoutData(gd_label_6);
		label_6.setText(Messages.Tips_expertSignStep);
		label_6.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(EvaluationStepEnum.SIGIN)));
		label_6.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
		
		Composite composite = new Composite(container, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.verticalSpacing = 10;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite.widthHint = 268;
		composite.setLayoutData(gd_composite);
		
		Label label = new Label(composite, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText(Messages.StepName + "：");
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.marginWidth = 0;
		gl_composite_2.verticalSpacing = 1;
		gl_composite_2.marginHeight = 0;
		composite_2.setLayout(gl_composite_2);
		
		stepNameText = new Text(composite_2, SWT.NONE);
		stepNameText.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		stepNameText.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
		stepNameText.addPaintListener(new TextHintListener(Messages.Tips_EnterEvaluationStepName));
		stepNameText.addVerifyListener(new VerifyRequiredListener(stepNameText, MessageFormat.format(Messages.Tips_required, Messages.StepName)));
		stepNameText.setTextLimit(20);
		stepNameText.setText(Messages.Step_signIn);
		ESepator step_name_sep = new ESepator(composite_2, SWT.NONE);
		step_name_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		step_name_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Label label2 = new Label(composite, SWT.NONE);
		label2.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label2.setText(Messages.Button_evaluationCommitment);
		
		signNoteComposite = new Composite(composite, SWT.NONE);
		signNoteComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_filterComposite = new GridLayout(2, false);
		gl_filterComposite.marginHeight = 0;
		gl_filterComposite.marginWidth = 0;
		signNoteComposite.setLayout(gl_filterComposite);
		
		noteYesBtn = new Button(signNoteComposite, SWT.CHECK);
		noteYesBtn.setText(Messages.Tips_needSignCommitment);
		
		
		Label label_memo = new Label(composite, SWT.NONE);
		label_memo.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_memo.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, true, 1, 1));
		label_memo.setText(Messages.Note + "：");
		
		stepMemoText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		stepMemoText.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		GridData gd_stepMemoText = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_stepMemoText.widthHint = 202;
		gd_stepMemoText.heightHint = 71;
		stepMemoText.setLayoutData(gd_stepMemoText);
		stepMemoText.setTextLimit(100);
		
		ESepator sep = new ESepator(topContainer, SWT.NONE);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_1 = new Composite(topContainer, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		save_btn = new EButton(composite_1, SWT.NONE);
		save_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		GridData gd_button_1 = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_button_1.widthHint = 90;
		save_btn.setLayoutData(gd_button_1);
		save_btn.setText(Messages.Button_save);
		save_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveNodeAction();
			}
		});
		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_button = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_button.widthHint = 90;
		cancel_btn.setLayoutData(gd_button);
		cancel_btn.setText(Messages.Button_close);

		init();
		return container;
	}

	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		if(null == evaluationStep){
			evaluationStep = new EvaluationStep();
			evaluationStep.setStepName(Messages.Step_signIn);
			evaluationStep.setStepStatus(CommonEnum.NOT_START);
			int maxSortNo = evaluationStepService.getMaxEvaluationStepSortNo(tenderProjectEntity.getId());
			evaluationStep.setSortNo(maxSortNo + 1);
			this.setTitle(MessageFormat.format(Messages.Title_create, Messages.Step_step));
		}else {
			this.setTitle(MessageFormat.format(Messages.Title_edit, Messages.Step_step));
			if(evaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)){
				save_btn.setEnabled(false);
			}
		}
		refreshEvaltionNodeForm();
	}
	
	/**
	 * @Description: 刷新评标步骤表单
	 * @author guoyr
	 */
	private void refreshEvaltionNodeForm(){
		stepNameText.setText(ObjectUtils.getObjString(evaluationStep.getStepName()));
		noteYesBtn.setSelection(CommonEnum.YES.equals(evaluationStep.getEvaluationParamByKey("signatureSelftCommitment")));
		stepMemoText.setText(ObjectUtils.getObjString(evaluationStep.getStepMemo(), Messages.Tips_expertSignStep));
	}
	
	/**
	 * @Description: 确定
	 * @author guoyr
	 */
	private void saveNodeAction(){
		if(!save_btn.getEnabled()){
			return;
		}
		if(!ValidateUtils.checkInput(stepNameText.getParent())){
			return;
		}
		
		// 验证评标步骤名称是否已经存在
		if(evaluationStepService.valEvaluationStepNameExist(tenderProjectEntity.getId(), evaluationStep.getId(), stepNameText.getText().trim())){
			MessageDialog.openWarning(getShell(), Messages.Step_step + "["+stepNameText.getText().trim()+"]" + Messages.Tips_AlreadyExistsPleaseRetype);
			stepNameText.setFocus();
			stepNameText.setSelection(stepNameText.getText().length());
			return;
		}
		
		evaluationStep.setEvaluationParam("signatureSelftCommitment", noteYesBtn.getSelection() ? CommonEnum.YES : CommonEnum.NO);
		
		evaluationStep.setStepName(stepNameText.getText().trim());
		evaluationStep.setStepType(EvaluationStepEnum.SIGIN);//评标步骤类型：01：签到
		evaluationStep.setFilterType(EvaluationStepEnum.FILTER_NO);
		evaluationStep.setStepStatus(CommonEnum.NOT_START);
		evaluationStep.setStepMemo(stepMemoText.getText().trim());
		evaluationStep.setTenderId(tenderProjectEntity.getId());
		evaluationStep = evaluationStepService.saveSignEvaluationStep(evaluationStep);
		if(null != evaluationStep){
			if(null != callBack){
				callBack.callBackObject(evaluationStep);
			}
		}else {
			MessageDialog.openWarning(getShell(), stepNameText.getText()+Messages.Tips_saveFail);
		}
		this.getShell().close();
	}
	
	/**
	 * @Description: 取消
	 * @author guoyr
	 */
	private void cancelAction(){
		this.getShell().close();
	}

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

	public CallBack getCallBack() {
		return callBack;
	}

	public void setCallBack(CallBack callBack) {
		this.callBack = callBack;
	}
	
}
