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

import java.text.NumberFormat;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.listener.VerifyFloatListener;
import org.eclipse.swt.core.listener.VerifyMaxEqListener;
import org.eclipse.swt.core.listener.VerifyMinEqListener;
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.ELabel;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
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.components.core.I18NEnum;
import com.gx.obe.web.entity.EvaluationFactorResult;
import com.gx.obe.business.entity.ScoreItem;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.SerializeUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 详细评审对话框
 * @author guoyr
 */
public class ScoreEvaluationDialog extends EDialog {

	private StyledText factorMemoText;
	private EvaluationFactor evaluationFactor;
	private Supplier supplier;
	private ExpertEvaluationResult expertEvaluationResult;
	private EvaluationFactorResult  evalFactorResult;
	private ELabel objectiveLabel;
	private Group evaluateGroup;
	private Text evaluationOpinionText;
	private Text supplierEvalOpinionText;
	private EButton confirmBtn;
	private CallBack callBack;
	private Combo scoreTypeCombo;
	private Text evaluationScoreText;
	private TenderProjectEntity tenderProjectEntity;
	
	{
		LogUtils.logClass(ScoreEvaluationDialog.class);
	}
	
	/**
	 * @param parentShell
	 * @param tenderProjectEntity
	 * @param supplier
	 * @param evaluationFactor
	 * @param expertEvaluationResult
	 * @param evalFactorResult
	 * @param callBack
	 */
	public ScoreEvaluationDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, Supplier supplier, EvaluationFactor evaluationFactor, ExpertEvaluationResult expertEvaluationResult, EvaluationFactorResult  evalFactorResult, CallBack callBack) {
		super(parentShell);
		setTitle(supplier.getSupplierName());
		this.tenderProjectEntity = tenderProjectEntity;
		this.supplier = supplier;
		this.evaluationFactor = evaluationFactor;
		this.expertEvaluationResult = expertEvaluationResult;
		this.evalFactorResult = evalFactorResult;
		this.callBack = callBack;
	}

	/** 
	 * @Description: 
	 * @param parent
	 * @return
	 */
	@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, true, 1, 1));
		GridLayout gl_container = new GridLayout(1, false);
		container.setLayout(gl_container);
		
		Group composite_1 = new Group(container, SWT.NONE);
		composite_1.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		composite_1.setText("指标信息");
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);
		
		factorMemoText = new StyledText(composite_1, SWT.READ_ONLY | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_factorMemoText = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_factorMemoText.heightHint = 100;
		factorMemoText.setLayoutData(gd_factorMemoText);
		
		objectiveLabel = new ELabel(composite_1, SWT.WRAP);
		objectiveLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		objectiveLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		objectiveLabel.setText("当前指标为“客观项指标”，所有专家对此项的评分结果须一致！");
		
		evaluateGroup = new Group(container, SWT.NONE);
		evaluateGroup.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		GridLayout gl_evaluateGroup = new GridLayout(2, false);
		evaluateGroup.setLayout(gl_evaluateGroup);
		evaluateGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		evaluateGroup.setText(supplier.getSupplierName()+"评标结果");
		
		Label label_4 = new Label(evaluateGroup, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setText("评分：");
		
		evaluationScoreText = new Text(evaluateGroup, SWT.BORDER);
		evaluationScoreText.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				evaluationScoreText.selectAll();
			}
		});
		GridData gd_evaluationScoreText = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		gd_evaluationScoreText.widthHint = 150;
		evaluationScoreText.setLayoutData(gd_evaluationScoreText);
		evaluationScoreText.setTextLimit(8);
		if(StringUtils.equals(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE, evaluationFactor.getScoreType())){
			evaluationScoreText.dispose();
			scoreTypeCombo = new Combo(evaluateGroup, SWT.READ_ONLY);
			GridData gd_combo = new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1);
			gd_combo.widthHint = 150;
			scoreTypeCombo.setLayoutData(gd_combo);
		}
		
		Label label_5 = new Label(evaluateGroup, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_5.setText("您对指标的\r\n评审意见：");
		
		evaluationOpinionText = new Text(evaluateGroup, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_text_1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_text_1.heightHint = 50;
		evaluationOpinionText.setLayoutData(gd_text_1);
		evaluationOpinionText.setTextLimit(500);
		
		Label label_6 = new Label(evaluateGroup, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1));
		label_6.setText("您对"+com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType())+" \r\n的评审意见：");
		
		supplierEvalOpinionText = new Text(evaluateGroup, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_text_2 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_text_2.heightHint = 50;
		supplierEvalOpinionText.setLayoutData(gd_text_2);
		supplierEvalOpinionText.setTextLimit(500);
		
		ESepator label_7 = new ESepator(top, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_7.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite = new Composite(top, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite = new GridLayout(2, false);
		composite.setLayout(gl_composite);
		
		confirmBtn = new EButton(composite, SWT.NONE);
		confirmBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/yes.png"));
		confirmBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				confirmAction();
			}
		});
		GridData gd_confirmBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		confirmBtn.setLayoutData(gd_confirmBtn);
		confirmBtn.setText(Messages.Button_ok);
		EButton cancel_btn = new EButton(composite, 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) {
				getShell().close();
			}
		});
		GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		cancel_btn.setLayoutData(gd_button);
		cancel_btn.setText(Messages.Button_close);

		init();
		return container;
	}

	/**
	 * @Description: 初始化界面数据
	 * @author guoyr
	 */
	@SuppressWarnings("unchecked")
	private void init(){
		if(null == evaluationFactor){
			evaluationFactor = new EvaluationFactor();
		}

		StringBuilder evaluationFactorMemo = new StringBuilder();
		evaluationFactorMemo.append("分值范围：[").append(NumberFormat.getInstance().format(evaluationFactor.getFactorMinScore())).append("-").append(NumberFormat.getInstance().format(evaluationFactor.getFactorMaxScore())).append("分]");
		if(null != evaluationFactor.getFactorWeight()){
			evaluationFactorMemo.append("(权重").append(BigDecimalUtils.round(evaluationFactor.getFactorWeight(), 2).toString()).append("%)");
		}
		StyleRange styleScoreRange = new StyleRange(); 
		styleScoreRange.start = 5; 
		styleScoreRange.foreground = SWTResourceManager.getColor(77, 163, 19); 
		styleScoreRange.fontStyle = SWT.BOLD; 
		styleScoreRange.length = evaluationFactorMemo.length()-5; 
		evaluationFactorMemo.append("\r\n");
		int startIndex = evaluationFactorMemo.length();
		
		StyleRange factorNameTitleStyleRange = getTitleStyleRange(startIndex, 5);
		
		StyleRange styleFactorNameRange = new StyleRange(); 
		styleFactorNameRange.start = startIndex + 5; 
		evaluationFactorMemo.append("评审指标：").append(evaluationFactor.getFactorName());
		styleFactorNameRange.length = evaluationFactor.getFactorName().length(); 
		styleFactorNameRange.foreground = SWTResourceManager.getColor(20, 20, 20); 
		styleFactorNameRange.fontStyle = SWT.NORMAL; 
		
		StyleRange factorMemoTitleStyleRange = null;
		StyleRange styleFactorMemoRange = null;
		if(null != evaluationFactor.getMemo() && evaluationFactor.getMemo().length() > 0){
			evaluationFactorMemo.append("\r\n");
			startIndex = evaluationFactorMemo.length();
			factorMemoTitleStyleRange = getTitleStyleRange(startIndex, 5);
			evaluationFactorMemo.append("评分细则：").append(evaluationFactor.getMemo());
			styleFactorMemoRange = new StyleRange(); 
			styleFactorMemoRange.start = startIndex + 5; 
			styleFactorMemoRange.length = evaluationFactor.getMemo().length(); 
			styleFactorMemoRange.foreground = SWTResourceManager.getColor(100, 100, 100); 
			styleFactorMemoRange.fontStyle = SWT.NORMAL; 
		}
		factorMemoText.setText(evaluationFactorMemo.toString());
		factorMemoText.setStyleRange(getTitleStyleRange(0, 5));
		factorMemoText.setStyleRange(styleScoreRange);
		factorMemoText.setStyleRange(factorNameTitleStyleRange);
		factorMemoText.setStyleRange(styleFactorNameRange);
		if(null != factorMemoTitleStyleRange){
			factorMemoText.setStyleRange(factorMemoTitleStyleRange);
			factorMemoText.setStyleRange(styleFactorMemoRange);
		}
	
		if(!ObjectUtils.equals(evaluationFactor.getObjective(), EvaluationFactorEnum.OBJECTIVE_YES)){
			objectiveLabel.dispose();
		}

		if(StringUtils.equals(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE, evaluationFactor.getScoreType())){
			scoreTypeCombo.removeAll();
			List<ScoreItem> itemList = (List<ScoreItem>)SerializeUtils.deserializeObject(evaluationFactor.getObjectiveItem());
			if(null != itemList && itemList.size() > 0){
				int index = 0;
				int selectIndex = 0;
				for(ScoreItem scoreItem : itemList){
					scoreTypeCombo.add(ObjectUtils.getObjString(scoreItem.getName())+"("+ObjectUtils.getObjString(scoreItem.getScore())+")", index);
					scoreTypeCombo.setData(ObjectUtils.getObjString(index), ObjectUtils.getObjString(scoreItem.getScore()));
					if(null != evalFactorResult.getEvaluationScore() && scoreItem.getScore().compareTo(evalFactorResult.getEvaluationScore()) == 0){
						selectIndex = index;
					}
					index++;
				}
				scoreTypeCombo.select(selectIndex);
			}
		}else{
			evaluationScoreText.addVerifyListener(new VerifyFloatListener("请输入正确的分值！"));
			evaluationScoreText.addVerifyListener(new VerifyMinEqListener(null != evaluationFactor.getFactorMinScore()?evaluationFactor.getFactorMinScore():0));
			evaluationScoreText.addVerifyListener(new VerifyMaxEqListener(null != evaluationFactor.getFactorMaxScore()?evaluationFactor.getFactorMaxScore():0));
			;
			evaluationScoreText.setText(NumberFormatUtils.format(evalFactorResult.getEvaluationScore(), 3));
			evaluationScoreText.setFocus();
		}
		evaluationOpinionText.setText(ObjectUtils.getObjString(evalFactorResult.getEvaluationOpinion()));
		supplierEvalOpinionText.setText(ObjectUtils.getObjString(expertEvaluationResult.getSupplierOpinion()));
	}
	
	private StyleRange getTitleStyleRange(int start, int length){
		StyleRange styleRange = new StyleRange(); 
		styleRange.start = start; 
		styleRange.length = length; 
//		styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_WHITE); 
		styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_BLUE);
		styleRange.fontStyle = SWT.BOLD; 
		return styleRange;
	}
	
	/**
	 * @Description: 确认评审结果
	 * @author guoyr
	 */
	private void confirmAction(){
		if(!ValidateUtils.checkInput(evaluateGroup)){
			return;
		}
		if(StringUtils.equals(EvaluationFactorEnum.SCORE_TYPE_OBJECTIVE, evaluationFactor.getScoreType())){
			evalFactorResult.setEvaluationScore(ObjectUtils.getObjBigDecimal(WidgetUtils.getComboSelection(scoreTypeCombo), Constants.SCORE_SCALE));
		}else{
			evalFactorResult.setEvaluationScore(ObjectUtils.getObjBigDecimal(evaluationScoreText.getText().trim(), Constants.SCORE_SCALE));
		}
		evalFactorResult.setEvaluationOpinion(evaluationOpinionText.getText());
		expertEvaluationResult.setSupplierOpinion(supplierEvalOpinionText.getText());
		getShell().close();
		callBack.callBack();
	}
	@Override
	protected Point getInitialSize() {
		return new Point(648, 449);
	}
}
