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

import java.text.MessageFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.ExpertEvaluationStep;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.dialog.ApplyReEvaluationDialog;
import com.gx.obe.evaluation.step.inter.IEvaluationResultService;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 评审结果界面
 * @author chenxw
 */
public class EvaluationResultComposite extends Composite implements IEvaluationResultService {
	
	private Label messageLabe;
	private ELink applyReEvaluation_link;
	protected Composite tableComposite;
	
	protected TenderProjectEntity tenderProjectEntity;
	protected EvaluationStep evaluationStep;
	protected TenderProjectRuleEntity tenderProjectRuleEntity;
	protected Expert curExpert;
	
	protected ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	
	{
		LogUtils.logClass(EvaluationResultComposite.class);
	}
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public EvaluationResultComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		
		Composite composite_1 = new Composite(this, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		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);
		
		messageLabe = new Label(composite_1, SWT.WRAP);
		messageLabe.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		messageLabe.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		messageLabe.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		applyReEvaluation_link = new ELink(composite_1, SWT.NONE);
		applyReEvaluation_link.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				applyReEvaluationAction();
			}
		});
		applyReEvaluation_link.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		applyReEvaluation_link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/hand.png"));
		
		tableComposite = new Composite(this, SWT.NONE);
		tableComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 1;
		gl_composite.marginWidth = 1;
		tableComposite.setLayout(gl_composite);
		tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param tenderProjectEntity
	 * @param evaluationStep
	 * @param tenderProjectRuleEntity
	 */
	public void refresh(TenderProjectEntity tenderProjectEntity, EvaluationStep evaluationStep, TenderProjectRuleEntity tenderProjectRuleEntity) {
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		this.curExpert = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), evaluationStep);
		
		refreshTenderInfo();
	}
	
	/**
	 * @Description: 刷新界面
	 * @author chenxw 
	 */
	private void refreshTenderInfo() {
		applyReEvaluation_link.setText(MessageFormat.format(Messages.Button_appllyForBackFormat, evaluationStep.getStepName()));
		applyReEvaluation_link.setVisible(false);
		
		StringBuilder msg = new StringBuilder();
		if (null != evaluationStep.getStepStatus() && evaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)) {
			if (Constants.USER.getId().equals(curExpert.getUserId())) applyReEvaluation_link.setVisible(true);
			msg.append(MessageFormat.format(Messages.Tips_progressComplate, evaluationStep.getStepName()));
		} else if (null != evaluationStep.getStepStatus() && evaluationStep.getStepStatus().equals(CommonEnum.IN_PROGRESS)) {
			msg.append(MessageFormat.format(Messages.Tips_progressIn, evaluationStep.getStepName()));
		} else {
			msg.append(MessageFormat.format(Messages.Tips_progressNotStart, evaluationStep.getStepName()));
		}
		messageLabe.setText(msg.toString());
	}
	
	/**
	 * @Description: 申请回退
	 * @author chenxw
	 */
	private void applyReEvaluationAction() {
		ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId(), curExpert.getUserId());
		if (null == expertEvaluationStep) return;
		ApplyReEvaluationDialog applyReEvaluationDialog = new ApplyReEvaluationDialog(getShell(), evaluationStep, expertEvaluationStep);
		applyReEvaluationDialog.open();
	}
	
	protected void checkSubclass() {}
	
}
