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.FloatInputListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.ValidateUtils;
import org.eclipse.swt.core.utils.WidgetUtils;
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.custom.CLabel;
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.I18NEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
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 MultiRoundsPriceEvaluationStepDialog extends EDialog {
	
	private Text stepNameText;
	private Composite roleTypeComposite;
	private EButton save_btn;
	private ImageResolver imageResolver = new ImageResolver();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private Text stepMemoText;
	private Composite composite;
	private CallBack callBack;
	private Tips tips;
	private Composite bidPriceSourceComposite;
	private Button localBtn;
	private Button remoteBtn;
	private Text maxRroundText;
//	private Text evalMinScoreText;
//	private Label minScoreTips;
	
	{
		LogUtils.logClass(MultiRoundsPriceEvaluationStepDialog.class);
	}

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public MultiRoundsPriceEvaluationStepDialog(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);
		this.setOperator(SWT.MAX|SWT.CLOSE);
		
		tips = new Tips(container, SWT.WRAP);
		tips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		tips.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(EvaluationStepEnum.MULTI_ROUNDS_PRICE)));
		tips.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
		
		composite = new Composite(container, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite.widthHint = 414;
		composite.setLayoutData(gd_composite);
		
		CLabel label = new CLabel(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText(Messages.StepName + ": ");
		
		stepNameText = new Text(composite, SWT.BORDER);
		
		GridData gd_stepNameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_stepNameText.widthHint = 150;
		stepNameText.setLayoutData(gd_stepNameText);
		stepNameText.addVerifyListener(new VerifyRequiredListener(stepNameText, MessageFormat.format(Messages.Tips_required, Messages.StepName)));
		stepNameText.setTextLimit(20);
		
		CLabel label_1 = new CLabel(composite, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText(Messages.EvaluationMembers + "：");
		
		Composite composite_6 = new Composite(composite, SWT.NONE);
		composite_6.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_6 = new GridLayout(1, false);
		gl_composite_6.marginHeight = 0;
		gl_composite_6.verticalSpacing = 1;
		gl_composite_6.marginWidth = 0;
		composite_6.setLayout(gl_composite_6);
		
		roleTypeComposite = new Composite(composite_6, SWT.NONE);
		roleTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_roleTypeComposite = new GridLayout(2, false);
		gl_roleTypeComposite.marginHeight = 0;
		gl_roleTypeComposite.marginWidth = 0;
		roleTypeComposite.setLayout(gl_roleTypeComposite);
		
		Button button = new Button(roleTypeComposite, SWT.RADIO);
		button.setText(Messages.ExpertLeader);
		button.setData("roleType", EvaluationStepEnum.EXPERT_LEADER);
		button.setSelection(true);
		
		Button button_1 = new Button(roleTypeComposite, SWT.RADIO);
		button_1.setText(Messages.ProjectManager);
		button_1.setData("roleType", EvaluationStepEnum.OPERATOR);
		
		CLabel label_2 = new CLabel(composite, SWT.NONE);
		label_2.setText(Messages.BidPriceSource + "：");
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.marginHeight = 0;
		gl_composite_2.marginWidth = 0;
		composite_2.setLayout(gl_composite_2);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		bidPriceSourceComposite = new Composite(composite_2, SWT.NONE);
		bidPriceSourceComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_sourseComposite = new GridLayout(2, false);
		gl_sourseComposite.marginHeight = 0;
		gl_sourseComposite.marginWidth = 0;
		bidPriceSourceComposite.setLayout(gl_sourseComposite);
		
		localBtn = new Button(bidPriceSourceComposite, SWT.RADIO);
		localBtn.setText(Messages.LocalEntry);
		localBtn.setData("bidPriceSource", EvaluationStepEnum.LACOL);
		localBtn.setSelection(true);
		localBtn.setVisible(false);
		
		remoteBtn = new Button(bidPriceSourceComposite, SWT.RADIO);
		remoteBtn.setText(Messages.RemoteBidPrice);
		remoteBtn.setData("bidPriceSource", EvaluationStepEnum.REMOTE);
		remoteBtn.setVisible(false);
		
		CLabel label_3 = new CLabel(composite, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText(Messages.MaxRound + "：");
		
		Composite composite_3 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(3, false);
		gl_composite_3.verticalSpacing = 0;
		gl_composite_3.marginHeight = 1;
		gl_composite_3.marginWidth = 1;
		composite_3.setLayout(gl_composite_3);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		maxRroundText = new Text(composite_3, SWT.CENTER);
		maxRroundText.setForeground(SWTResourceManager.getColor(0, 0, 255));
		maxRroundText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		GridData gd_maxRroundText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_maxRroundText.widthHint = 30;
		maxRroundText.setLayoutData(gd_maxRroundText);
		
		Label label_4 = new Label(composite_3, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
		label_4.setText(Messages.Round);
		
		Label label_6 = new Label(composite_3, SWT.WRAP);
		label_6.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 2));
		label_6.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_6.setText(Messages.Tips_maxBidPriceRound);
		
		ESepator maxRroundLabel = new ESepator(composite_3, SWT.NONE);
		GridData gd_maxRroundLabelLabel = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_maxRroundLabelLabel.widthHint = 50;
		maxRroundLabel.setLayoutData(gd_maxRroundLabelLabel);
		maxRroundLabel.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
//		Label label_11 = new Label(composite, SWT.NONE);
//		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
//		label_11.setText(Messages.MinScore + "：");
		
//		Composite composite_5 = new Composite(composite, SWT.NONE);
//		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
//		GridLayout gl_composite_5 = new GridLayout(3, false);
//		gl_composite_5.verticalSpacing = 0;
//		gl_composite_5.marginHeight = 0;
//		gl_composite_5.marginWidth = 0;
//		composite_5.setLayout(gl_composite_5);
		
//		evalMinScoreText = new Text(composite_5, SWT.CENTER);
//		evalMinScoreText.setForeground(SWTResourceManager.getColor(0, 0, 255));
//		evalMinScoreText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
//		GridData gd_evalMinScoreText = new GridData(SWT.LEFT, SWT.BOTTOM, false, true, 1, 1);
//		gd_evalMinScoreText.widthHint = 50;
//		evalMinScoreText.setLayoutData(gd_evalMinScoreText);
//		evalMinScoreText.setTextLimit(2);
		
//		Label label_12 = new Label(composite_5, SWT.NONE);
//		label_12.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 2));
//		label_12.setText(Messages.TextScore);
		
//		minScoreTips = new Label(composite_5, SWT.WRAP);
//		minScoreTips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 2));
//		minScoreTips.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
//		ESepator evalMinScoreLabel = new ESepator(composite_5, SWT.NONE);
//		GridData gd_evalMinScoreLabel = new GridData(SWT.FILL, SWT.TOP, false, true, 1, 1);
//		gd_evalMinScoreLabel.widthHint = 30;
//		evalMinScoreLabel.setLayoutData(gd_evalMinScoreLabel);
//		evalMinScoreLabel.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		CLabel label_5 = new CLabel(composite, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, true, 1, 1));
		label_5.setText(Messages.NodeNote + "：");
		
		stepMemoText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		stepMemoText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		stepMemoText.setTextLimit(100);
		
		ESepator but_sep = new ESepator(topContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
		but_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		but_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"));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);
		
		save_btn = new EButton(composite_1, SWT.NONE);
		save_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		GridData gd_confirmBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_confirmBtn.widthHint = 90;
		save_btn.setLayoutData(gd_confirmBtn);
		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(){
		String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		tips.setText(MessageFormat.format(Messages.Tips_multiRoundBidPrice, supplierName));
//		minScoreTips.setText(MessageFormat.format(Messages.Tips_allowedRoundBidPrice, supplierName));
		
		String multiRoundPriceType = PropertiesUtils.getProperty(ConfigConstants.MULITI_ROUND_PRICE, "");
		if(StringUtils.isEmpty(multiRoundPriceType)){
			multiRoundPriceType = EvaluationStepEnum.LACOL;
		}
		String[] types = multiRoundPriceType.split(",");
		for (String string : types) {
			if(EvaluationStepEnum.LACOL.equals(string)){
				localBtn.setVisible(true);
			}else if(EvaluationStepEnum.REMOTE.equals(string)){
				remoteBtn.setVisible(true);
			}
		}
		
		
		if(null == evaluationStep){
			evaluationStep = new EvaluationStep();
			evaluationStep.setStepName("多轮报价");
			evaluationStep.setFilterType(EvaluationStepEnum.FILTER_NO);
			evaluationStep.setPassType(EvaluationStepEnum.PASS_ALL);
			evaluationStep.setRoleType(EvaluationStepEnum.EXPERT_LEADER);
			evaluationStep.setStepStatus(CommonEnum.NOT_START);
			evaluationStep.setEvaluationParam("BidPriceSource", ObjectUtils.getObjString(WidgetUtils.getRadioVal(bidPriceSourceComposite,"bidPriceSource")));
			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));
//			boolean isInEvaluation = false;
			if(ObjectUtils.equals(evaluationStep.getStepStatus(), CommonEnum.COMPLETE)){
				composite.setEnabled(false);
				save_btn.setEnabled(false);
//				isInEvaluation = true;
//			}else {
//				isInEvaluation = evaluationStepService.isInEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId());
			}
//			if(isInEvaluation){
//				roleTypeComposite.setEnabled(false);
//				bidPriceSourceComposite.setEnabled(false);
//			}
		}
		FloatInputListener factorWeightListener = new FloatInputListener();
		factorWeightListener.addMinCheck(1, Messages.Tips_MakeAtLeastOneRound);
		maxRroundText.addKeyListener(factorWeightListener);
		
		refreshEvaltionNodeForm();
	}
	
	/**
	 * @Description: 刷新评标步骤表单
	 * @author guoyr
	 */
	private void refreshEvaltionNodeForm(){
		stepNameText.setText(ObjectUtils.getObjString(evaluationStep.getStepName()));
		stepMemoText.setText(ObjectUtils.getObjString(evaluationStep.getStepMemo()));
		WidgetUtils.setRadioChecked(roleTypeComposite, ObjectUtils.getObjString(evaluationStep.getRoleType()), "roleType");
		WidgetUtils.setRadioChecked(bidPriceSourceComposite, ObjectUtils.getObjString(evaluationStep.getEvaluationParamByKey("BidPriceSource")), "bidPriceSource");
//		evalMinScoreText.setText(ObjectUtils.getObjString(evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.EVALUATION_MIN_SCORE, BigDecimal.class)));
		
		int maxRound = evaluationStep.getEvaluationParamByKey(ConfigConstants.MAX_ROUND, Integer.class, 3);
		if(maxRound > 0){
			maxRroundText.setText(maxRound +"");
		}else{
			maxRroundText.setText(PropertiesUtils.getProperty(ConfigConstants.MAX_ROUND, "3"));
		}
	}
	
	/**
	 * @Description: 确定
	 * @author guoyr
	 */
	private void saveNodeAction(){
		if(save_btn.getEnabled()){
			if(!ValidateUtils.checkInput(stepNameText.getParent())){
				return;
			}
			if(null == maxRroundText.getText().trim() || "".equals(maxRroundText.getText().trim())){
				MessageDialog.openInformation(getShell(), Messages.Tips_requriedMaxRound);
				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.setStepName(stepNameText.getText().trim());
			evaluationStep.setStepType(EvaluationStepEnum.MULTI_ROUNDS_PRICE);
			evaluationStep.setStepMemo(stepMemoText.getText().trim());
			evaluationStep.setTenderId(tenderProjectEntity.getId());
			evaluationStep.setRoleType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(roleTypeComposite,"roleType")));
			evaluationStep.setEvaluationParam("BidPriceSource",ObjectUtils.getObjString(WidgetUtils.getRadioVal(bidPriceSourceComposite,"bidPriceSource")));
			evaluationStep.setEvaluationParam(ConfigConstants.MAX_ROUND,ObjectUtils.getObjString(maxRroundText.getText().trim()));
//			evaluationStep.setEvaluationParam(EvaluationStepEnum.EVALUATION_MIN_SCORE, ObjectUtils.getObjBigDecimal(evalMinScoreText.getText().trim()));
			evaluationStep = (EvaluationStep) evaluationStepService.saveEvaluationStep(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();
	}

	public CallBack getCallBack() {
		return callBack;
	}

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

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