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

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.core.widgets.RectangleRadioButton;
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.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.BidPriceRound;
import com.gx.obe.business.entity.ExpertEvaluationStep;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.listener.SelectSupplierListener;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.composite.BidPriceResutlTableComposite;
import com.gx.obe.evaluation.step.composite.MultiBidPriceResultDetailComposite;
import com.gx.obe.evaluation.step.dialog.ApplyReEvaluationDialog;
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.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.ReBidPriceRoundService;
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;

public class MultiRoundsPriceResultComposite extends Composite {
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private SupplierService supplierService = new SupplierService();
	private ReBidPriceRoundService reBidPriceRoundService = new ReBidPriceRoundService();
	private ExpertService expertService;
	private Label messageLabe;
	private ELink applyReEvaluation_link;
	private Expert CURRENT_EXPERT;
	private Composite backComposite;
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	private Composite roundComposite;
	private Composite roundListComposite;
	private int maxRound = 1;
	private ImageButton curRoundButton;
	private BidPriceResutlTableComposite bidPriceResutlTableComposite;
	private MultiBidPriceResultDetailComposite detail;
	private List<Supplier> supplierList;
	private BidPriceRound curbidPriceRound;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public MultiRoundsPriceResultComposite(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setBackgroundMode(SWT.INHERIT_FORCE);
		
		backComposite =  new Composite(this, SWT.NONE);
		backComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		backComposite.setLayout(new GridLayout(2, false));
		

		messageLabe = new Label(backComposite, 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(backComposite, 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() {
			public void mouseUp(MouseEvent e) {
				applyReEvaluationAction();
			}
		});


		Composite resultComposite = new Composite(this, SWT.NONE);
		resultComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_resultComposite = new GridLayout(1, false);
		gl_resultComposite.marginHeight = 0;
		gl_resultComposite.marginWidth = 0;
		resultComposite.setLayout(gl_resultComposite);
		roundComposite = new Composite(resultComposite, SWT.NONE);
		roundComposite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		GridLayout gl_roundComposite = new GridLayout(1, false);
		gl_roundComposite.verticalSpacing = 0;
		gl_roundComposite.marginHeight = 0;
		gl_roundComposite.marginWidth = 0;
		roundComposite.setLayout(gl_roundComposite);
		roundComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		roundListComposite = new Composite(roundComposite, SWT.NONE);
		roundListComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		RowLayout rl_roundListComposite = new RowLayout(SWT.HORIZONTAL);
		rl_roundListComposite.marginBottom = 0;
		roundListComposite.setLayout(rl_roundListComposite);
		
		ESepator subLine = new ESepator(roundComposite, SWT.NONE);
		subLine.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		subLine.setBackground(ColorConstants.SKIN_BG);
		subLine.setLineWidth(2);
		
		bidPriceResutlTableComposite  = new BidPriceResutlTableComposite(resultComposite, SWT.NONE);
		GridData gd_bidPriceResutlTableComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_bidPriceResutlTableComposite.heightHint = 50;
		bidPriceResutlTableComposite.setLayoutData(gd_bidPriceResutlTableComposite);
		
		detail = new MultiBidPriceResultDetailComposite(resultComposite, SWT.NONE);
		GridData gd_detail = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_detail.heightHint = 50;
		detail.setLayoutData(gd_detail);
		
		init();
	}

	/**
	 * @Description: 初始化
	 * @author wangxiang
	 */
	private void init() {
		expertService = new ExpertService();
		bidPriceResutlTableComposite.addSelectSupplierListener(new SelectSupplierListener(){
			public void selectSupplier(Supplier supplier) {
				if(null != supplier && null != curbidPriceRound){
					detail.refresh(tenderProjectEntity, supplier, maxRound, curbidPriceRound, true);
				}
			}
			
		});
	}
	
	/**
	 * @Description: 刷新
	 * @author guoyr
	 */
	public void refresh(TenderProjectEntity tenderProjectEntity, TenderProjectRuleEntity tenderProjectRuleEntity, EvaluationStep evaluationStep, String OPERATOR_USER_ID) {
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
		this.CURRENT_EXPERT = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), evaluationStep);
		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)) {
			msg.append(tenderProjectEntity.getTenderName());
			msg.append(" 已完成[");
			msg.append(evaluationStep.getStepName());
			msg.append("]！   ");
			if(null != OPERATOR_USER_ID && OPERATOR_USER_ID.equals(Constants.USER.getId())){
				applyReEvaluation_link.setVisible(true);
			}
		} else if (null != evaluationStep.getStepStatus() && evaluationStep.getStepStatus().equals(CommonEnum.IN_PROGRESS)) {
			msg.append(tenderProjectEntity.getTenderName());
			msg.append(" [");
			msg.append(evaluationStep.getStepName());
			msg.append("]进行中...   ");
		} else {
			msg.append(tenderProjectEntity.getTenderName());
			msg.append(" 未开始[");
			msg.append(evaluationStep.getStepName());
			msg.append("]！");
		}
		messageLabe.setText(msg.toString());
		backComposite.layout();
		supplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(), evaluationStep);
		
		refreshBidPriceRound();
	}

	
	private ImageButton createRoundButton(final BidPriceRound bidPriceRound){
		final ImageButton roundButton = new RectangleRadioButton(roundListComposite, SWT.NONE);
		if(bidPriceRound.getRoundNo() == 1) {
			roundButton.setText(Messages.OriginalBidPrice);
		}else {
			roundButton.setText(ObjectUtils.getObjString(bidPriceRound.getRoundName()));
		}
		roundButton.setData("bidPriceRound", bidPriceRound);
		roundButton.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		roundButton.addMouseListener(new MouseAdapter() {

			public void mouseUp(MouseEvent e) {
				if(null != curRoundButton && !curRoundButton.isDisposed()){
					curRoundButton.setSelection(false);
				}
				curRoundButton = roundButton;
				curRoundButton.setSelection(true);
				loadRoundBidPriceResult();
			}
		});
		return roundButton;
	}
	
	private void refreshBidPriceRound() {
		
		for(Control control : roundListComposite.getChildren()){
			control.dispose();
		}
		List<BidPriceRound> bidPriceRoundList = reBidPriceRoundService.getBidPriceRoundList(tenderProjectEntity.getId());
		if(null == bidPriceRoundList || bidPriceRoundList.size() == 0){
			BidPriceRound lastBidPriceRound = new BidPriceRound();
			lastBidPriceRound.setRoundName(Messages.OriginalBidPrice);
			lastBidPriceRound.setRoundNo(0);
			bidPriceRoundList.add(lastBidPriceRound);
		}
		if(null != bidPriceRoundList && bidPriceRoundList.size() > 0){
			maxRound = bidPriceRoundList.size();
//			maxRound =  reBidPriceRoundService.getMaxRound(tenderProjectEntity.getId());
			for(BidPriceRound bidPriceRound : bidPriceRoundList){
				curRoundButton = createRoundButton(bidPriceRound);
			}
		}
		curRoundButton.notifyListeners(SWT.MouseUp, null);
		roundListComposite.layout();
		roundComposite.layout();
		this.layout();
	}
	
	/**
	 * 加载轮次报价
	 * @param roundBidPriceButton 
	 */
	private void loadRoundBidPriceResult() {
		if(null == curRoundButton || curRoundButton.isDisposed())return;
		curbidPriceRound = (BidPriceRound)curRoundButton.getData("bidPriceRound");
		int round = curbidPriceRound.getRoundNo();
		if(curbidPriceRound.getRoundNo() == maxRound){
			round = 0;
		}
		bidPriceResutlTableComposite.refreshBidPriceResultTable(tenderProjectEntity, supplierList, round,curbidPriceRound);	
	}
	

	/**
	 * @Description: 申请回退
	 * @author guoyr
	 */
	private void applyReEvaluationAction() {
		if (applyReEvaluation_link.getEnabled()) {
			ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId(), CURRENT_EXPERT.getUserId());
			if(null == 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
	}
}
