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

import java.math.BigDecimal;
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.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.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

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.common.widget.composite.TotalEvaluationPriceTableComposite;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.listener.PriceEvaluationListener;
import com.gx.obe.message.Messages;
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.SupplierOpeningResultService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class CorrectPriceComposite extends Composite {
	private Composite ktableComposite;
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private EvaluationStep evaluationStep;
	private Expert CURRENT_EXPERT;
	private SupplierService supplierService = new SupplierService();
	private SupplierOpeningResultService supplierOpeningResultService ;
	private CLabel evalNodeNameLabel;
	private ImageResolver imageResolver = new ImageResolver();
	private PriceEvaluationListener listener;
	private List<Supplier> supplierList;
	List<Expert> expertList;
	private CLabel correctPriceLink;
	private Composite composite;
	
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();

	/**
	 * @param parent
	 * @param style
	 */
	public CorrectPriceComposite(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				imageResolver.disposite();
			}
		});
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginBottom = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		tenderProjectEntity = (TenderProjectEntity)parent.getData("tenderProjectEntity");
		tenderProjectRuleEntity = (TenderProjectRuleEntity)parent.getData("tenderProjectRuleEntity");
		evaluationStep = (EvaluationStep)parent.getData("evaluationStep");
		CURRENT_EXPERT = (Expert) parent.getData(Constants.CUR_EXPERT);
		
		Composite priceComposite = new Composite(this, SWT.NONE);
		priceComposite.setLayout(new GridLayout(7, false));
		priceComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		correctPriceLink = new CLabel(priceComposite, SWT.NONE);
		correctPriceLink.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		correctPriceLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		correctPriceLink.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		correctPriceLink.setText("修正"+Messages.EvaluationPrice);
		
		CLabel label_2 = new CLabel(priceComposite, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("宋体", 18, SWT.NORMAL));
		label_2.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_2.setText(">>");
		label_2.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		
		evalNodeNameLabel = new CLabel(priceComposite, SWT.NONE);
		evalNodeNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		evalNodeNameLabel.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.NORMAL));
		evalNodeNameLabel.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		evalNodeNameLabel.setText(evaluationStep.getStepName());
		
	
		ELink goEvaluationButton = new ELink(priceComposite, SWT.NONE);
		goEvaluationButton.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/back.png"));
		goEvaluationButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		goEvaluationButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				listener.priceEvaluation();
			}
		});
		goEvaluationButton.setText("返回评审界面");
		
		composite = new Composite(this, SWT.NONE);
		composite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		
		ktableComposite = new Composite(composite, SWT.NONE);
		ktableComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		ktableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		FillLayout fl_ktableComposite = new FillLayout(SWT.HORIZONTAL);
		ktableComposite.setLayout(fl_ktableComposite);
		init();
	}
	
	/**
	 * @Description: 初始化界面数据
	 * @author guoyr
	 */
	private void init(){
		supplierOpeningResultService = new SupplierOpeningResultService();
		
		boolean canEdit = true;
		boolean isSaved = false;
		ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId(), CURRENT_EXPERT.getUserId());
		if(null != expertEvaluationStep && ObjectUtils.equals(expertEvaluationStep.getPriceConfirmStatus(), CommonEnum.YES)){
			isSaved = true;
		}
		if(ObjectUtils.equals(tenderProjectRuleEntity.getConfirmPrice(), CommonEnum.YES)){
			if(isSaved){
				canEdit = false;
			}
		}
		
		drawEvaluationTable(canEdit);
	}

	/**
	 * @Description: 添加监听
	 * @author guoyr
	 * @param listener
	 */
	public void addCorrectEvalPriceListener(PriceEvaluationListener listener){
		this.listener= listener;
	}
	
	/**
	 * @Description: 加载评审表格数据
	 * @author guoyr
	 * @param canEdit
	 */
	private void drawEvaluationTable(boolean canEdit){
		// 获取投标人评标结果
		getSupplierList();
		for(Control control : ktableComposite.getChildren()){
			control.dispose();
		}
		TotalEvaluationPriceTableComposite composite = new TotalEvaluationPriceTableComposite(ktableComposite, SWT.NONE);
		composite.refresh(tenderProjectEntity, tenderProjectRuleEntity, supplierList, canEdit);
		ktableComposite.layout();
		
	}

	/**
	 * @Description: 获得所有投标人价格得分
	 * @author guoyr
	 * @return
	 */
	private List<Supplier> getSupplierList(){
		
		if(null != supplierList){
			supplierList.clear();
		}else {
			supplierList = new ArrayList<Supplier>();
		}
		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())){
							supplierOpeningResult = result;
							break;
						}
					}
				}
				if(null != supplierOpeningResult){
					if(null == supplierOpeningResult.getBidPrice()){
						supplierOpeningResult.setBidPrice(BigDecimal.ZERO);
					}
					if(null == supplierOpeningResult.getEvaluationPrice()){
						supplierOpeningResult.setEvaluationPrice(supplierOpeningResult.getBidPrice());
					}
					supplier.setSupplierOpeningResult(supplierOpeningResult);
					supplierList.add(supplier);
				}
			}
		}
		return supplierList;
	}
	
	@Override
	protected void checkSubclass() {
	}
}
