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

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.widgets.ELink;
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.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.business.enumeration.SupplierEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.composite.PriceScoreResultTableComposite;
import com.gx.obe.evaluation.step.dialog.ApplyReEvaluationDialog;
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.Supplier;
import com.gx.obe.web.entity.SupplierOpeningResult;
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.ProjectRuleService;
import com.gx.obe.web.service.SupplierOpeningResultService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class PriceResultComposite extends Composite {

	private Composite tableComposite;
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private Expert CURRENT_EXPERT;
	private ProjectRuleService projectRuleService;
	private SupplierOpeningResultService supplierOpeningResultService;
//	private ExpertEvaluationStepService expertEvaluationStepService ;
	private SupplierService supplierService = new SupplierService();
	private ExpertService expertService ;
	private ImageResolver imageResolver = new ImageResolver();
	private Composite composite_1;
	private Label messageLabe;
	private ELink applyReEvaluation_link;
//	private Expert curEvaluationExpert;
	
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	
	{
		LogUtils.logClass(PriceResultComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public PriceResultComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		
		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);
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);
		
		messageLabe = new Label(composite_1, SWT.WRAP);
		messageLabe.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, 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.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		applyReEvaluation_link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/hand.png"));
		applyReEvaluation_link.setText(Messages.Button_appllyForBack);
		applyReEvaluation_link.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				applyReEvaluationAction();
			}
		});
		
		tableComposite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		tableComposite.setLayout(gl_composite);
		tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		tableComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		expertService = new ExpertService();
		supplierOpeningResultService = new SupplierOpeningResultService();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	public void refresh(TenderProjectEntity tenderProjectEntity, TenderProjectRuleEntity tenderProjectRuleEntity, EvaluationStep evaluationStep, String OPERATOR_USER_ID){
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
		if(null == projectRuleService){
			projectRuleService = new ProjectRuleService();
		}
		tenderProjectRuleEntity = projectRuleService.getProjectRule(tenderProjectEntity.getId());
		
		CURRENT_EXPERT = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), evaluationStep);
		
		applyReEvaluation_link.setText(MessageFormat.format(Messages.Button_appllyForBackFormat, evaluationStep.getStepName()));
		applyReEvaluation_link.setVisible(false);
		if(null == tenderProjectRuleEntity)tenderProjectRuleEntity = new TenderProjectRuleEntity();
		String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		StringBuilder msg = new StringBuilder();
		if(null != evaluationStep.getStepStatus() &&evaluationStep.getStepStatus().equals(CommonEnum.COMPLETE)){
			msg.append(MessageFormat.format(Messages.Tips_progressComplate, evaluationStep.getStepName()));
			if(null != OPERATOR_USER_ID && OPERATOR_USER_ID.equals(Constants.USER.getId())){
				applyReEvaluation_link.setVisible(true);
			}else{
				((GridData)applyReEvaluation_link.getLayoutData()).exclude = true;
				applyReEvaluation_link.setVisible(false);
			}
		}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()));
		}
		msg.append("  ").append(Messages.Title_bidEvaluationRules).append(":");
		if(evaluationStep.getFinalistNumber() > 0){
			msg.append(MessageFormat.format(Messages.Tips_evaluationRule_sortNoFilter, evaluationStep.getFinalistNumber(), supplierName));
		}else {
			msg.append(MessageFormat.format(Messages.Tips_evaluationRule_noFilter, supplierName));
		}
		messageLabe.setText(msg.toString());
		
		PriceScoreResultTableComposite priceScoreResultTableComposite = new PriceScoreResultTableComposite(tableComposite, SWT.NONE);
		priceScoreResultTableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		priceScoreResultTableComposite.refresh(tenderProjectEntity, tenderProjectRuleEntity, CURRENT_EXPERT, evaluationStep, getSupplierList());
		layout();
	}

	/**
	 * @Description: 刷新投标人的价格分
	 * @author guoyr
	 * @return
	 */
	private List<Supplier> getSupplierList(){
		
		List<Supplier> supplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(), evaluationStep);
		if(null != supplierList && supplierList.size() > 0){
			// 则获取开标成功的投标人
			List<SupplierOpeningResult> supplierOpeningResultList = supplierOpeningResultService.getSupplierOpeningResultListByTenderIdAndBidOpeningStatus(tenderProjectEntity.getId(), SupplierEnum.OPENBID_SUCCESS);
			
			for(Supplier supplier : supplierList){
				SupplierOpeningResult supplierOpeningResult = null;
				if(null != supplierOpeningResultList && supplierOpeningResultList.size() > 0){
					for(SupplierOpeningResult result : supplierOpeningResultList){
						if(ObjectUtils.equals(result.getSupplierId(), supplier.getId())){
							if(null == result.getEvaluationPrice()){
								result.setEvaluationPrice(result.getBidPrice());
							}
							supplierOpeningResult = result;
							break;
						}
					}
				}
				if(null == supplierOpeningResult){
					supplierOpeningResult = new SupplierOpeningResult();
					supplierOpeningResult.setTenderId(tenderProjectEntity.getId());
					supplierOpeningResult.setSupplierId(supplier.getId());
				}
				supplier.setSupplierOpeningResult(supplierOpeningResult);
			}
		}else {
		}
		if(null == supplierList)supplierList = new ArrayList<Supplier>();
		return supplierList;
	}
	
	/**
	 * @Description: 申请回退
	 * @author guoyr 
	 */
	private void applyReEvaluationAction(){
		if(applyReEvaluation_link.getEnabled()){
			ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId(), CURRENT_EXPERT.getUserId());
			if(null != expertEvaluationStep){
				ApplyReEvaluationDialog applyReEvaluationDialog = new ApplyReEvaluationDialog(getShell(), evaluationStep, expertEvaluationStep);
				applyReEvaluationDialog.open();
			}
		}
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
