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

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.BigButton;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
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.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author wangxiang
 */
public class WasteBidProjectStepComposite extends Composite {
	
	private BigButton save_btn;
	private ImageResolver imageResolver = new ImageResolver();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private StyledText reasonText;
	private Label memoTips;
	private Composite composite;
	private CallBack callBack;
	private List<String> stepExpertUserIdList ;
	private Composite voteComposite;
	private TenderProjectService tenderProjectService;
	private Button voteNoBtn;
	private Button voteYesBtn;
	private Composite nameComposite;
	private Label tenderNameLabel;
	private Label tenderNoLabel;

		
	{
		LogUtils.logClass(WasteBidProjectStepComposite.class);
	}


	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public WasteBidProjectStepComposite(Composite parent, int style) {
		super(parent, style);
		this.tenderProjectEntity = (TenderProjectEntity)parent.getData("tenderProject");
		this.evaluationStep = (EvaluationStep)parent.getData("evaluationStep");
		tenderProjectService = new TenderProjectService();
		setBackground(SWTResourceManager.getColor(247, 247, 247));
		setBackgroundMode(SWT.INHERIT_FORCE);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
				if(null != stepExpertUserIdList){
					stepExpertUserIdList.clear();
				}
			}
		});
		GridLayout gl_topContainer = new GridLayout(1, false);
		gl_topContainer.verticalSpacing = 0;
		gl_topContainer.horizontalSpacing = 0;
		gl_topContainer.marginHeight = 0;
		gl_topContainer.marginWidth = 0;
		setLayout(gl_topContainer);
		
		Composite container = new Composite(this, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.marginHeight = 50;
		gl_container.marginWidth = 50;
		container.setLayout(gl_container);
		
		nameComposite = new Composite(container, SWT.NONE);
		nameComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_nameComposite = new GridLayout(1, false);
		gl_nameComposite.marginWidth = 50;
		gl_nameComposite.marginHeight = 10;
		nameComposite.setLayout(gl_nameComposite);
		nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		tenderNameLabel = new Label(nameComposite, SWT.NONE | SWT.WRAP);
		tenderNameLabel.setFont(SWTResourceManager.getFont("微软雅黑", 14, SWT.NORMAL));
		tenderNameLabel.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
		tenderNameLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		tenderNameLabel.setText("项目名称");

		tenderNoLabel = new Label(nameComposite, SWT.NONE);
		tenderNoLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		tenderNoLabel.setText("项目编号：");
		tenderNoLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		tenderNoLabel.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		composite = new Composite(container, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 50;
		gl_composite.verticalSpacing = 5;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite.widthHint = 600;
		gd_composite.minimumHeight = 100;
		composite.setLayoutData(gd_composite);
		
		ESepator sep = new ESepator(composite, SWT.NONE);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		sep.setLineWidth(3);
		sep.setBackground(SWTResourceManager.getColor(240, 240, 240));

		Label wasteReasonTitle = new Label(composite, SWT.NONE);
		wasteReasonTitle.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		wasteReasonTitle.setForeground(ColorConstants.FONT_BLACK_COLOR);
		wasteReasonTitle.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		wasteReasonTitle.setText(Messages.WasteReason + ":");

		
		reasonText = new StyledText(composite, SWT.WRAP | SWT.MULTI);
		reasonText.setLineSpacing(10);
		reasonText.setIndent(30);
		reasonText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		reasonText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		reasonText.setForeground(ColorConstants.FONT_BLACK_COLOR);
		GridData gd_reasonText = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_reasonText.heightHint = 100;
		reasonText.setLayoutData(gd_reasonText);
		
		memoTips = new Label(composite, SWT.RIGHT);
		memoTips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		ESepator voteSep = new ESepator(container, SWT.NONE);
		voteSep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		voteSep.setLineWidth(2);
		voteSep.setBackground(SWTResourceManager.getColor(240, 240, 240));
		voteSep.setLineStyle(SWT.LINE_DOT);
		
		Composite buttomComposite = new Composite(container, SWT.NONE);
		buttomComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		buttomComposite.setLayout(new GridLayout(1, false));
		buttomComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		voteComposite = new Composite(buttomComposite, SWT.NONE);
		voteComposite.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_4 = new GridLayout(3, false);
//		gl_composite_4.marginHeight = 0;
//		gl_composite_4.marginWidth = 0;
		voteComposite.setLayout(gl_composite_4);
		
		
		
		Label label = new Label(voteComposite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label.setText(Messages.NeedVote + ":");
		
		voteYesBtn = new Button(voteComposite, SWT.RADIO);
		voteYesBtn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		voteYesBtn.setSelection(true);
		voteYesBtn.setText(Messages.Vote);
		voteYesBtn.setData("vote","01");
		
		voteNoBtn = new Button(voteComposite, SWT.RADIO);
		voteNoBtn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		voteNoBtn.setText(Messages.NoVote);
		voteNoBtn.setData("vote","00");
		
		
		save_btn = new BigButton(voteComposite, SWT.NONE);
		save_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		save_btn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 3, 1));
		save_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveNodeAction();
			}
		});
		save_btn.setText(Messages.Button_confirm);
		
		init();
	}


	/**
	 * @Description: 初始化
	 * @author wangxiang
	 */
	private void init(){
		tenderNameLabel.setText(tenderProjectEntity.getTenderName());
		tenderNoLabel.setText(Messages.ProjectCode + "：" + tenderProjectEntity.getTenderNo());
		reasonText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				if(reasonText.getText().length()> 500){
					reasonText.setText(reasonText.getText().substring(0, 500));
				}
				memoTips.setText("("+reasonText.getText().length() + "/ "+"500)");
			}
		});
		tenderProjectEntity = tenderProjectService.getById(tenderProjectEntity.getId());
		refreshEvaltionNodeForm();
	}
	
	
	/**
	 * @Description: 刷新评标步骤表单
	 * @author guoyr
	 */
	private void refreshEvaltionNodeForm(){
		if(null == evaluationStep || null == evaluationStep.getId()){
			evaluationStep = new EvaluationStep();
			evaluationStep.setStepType(com.gx.obe.components.core.enumeration.EvaluationStepEnum.WASTE_BID);
			evaluationStep.setStepName(Messages.Step_wasteBidding);
			evaluationStep.setPassType(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PASS_ALL);
			evaluationStep.setRoleType(com.gx.obe.components.core.enumeration.EvaluationStepEnum.EXPERT);
			evaluationStep.setTenderId(tenderProjectEntity.getId());
		}else if(evaluationStepService.isInEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId())){
			save_btn.setVisible(false);
			reasonText.setEditable(false);
			voteComposite.setEnabled(false);
		}
		reasonText.setText(ObjectUtils.getObjString(tenderProjectEntity.getEffectiveMemo()));
		boolean needVote = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_WASTE_NEED_VOTE, Boolean.class, true);
		voteYesBtn.setSelection(needVote);
		voteNoBtn.setSelection(!needVote);
		composite.layout();
	}
	
	/**
	 * @Description: 确定
	 * @author guoyr
	 */
	private void saveNodeAction(){
		if(save_btn.getEnabled()){
			
			if(reasonText.getText().trim().length() < 1){
				MessageDialog.openShortMessage(Messages.Tips_wasteReason);
				return;
			}
			if(!MessageDialog.openConfirm(getShell(), voteYesBtn.getSelection() ? Messages.Tips_wasteStepConfirm : Messages.Tips_wasteStepConfirm1)){
				return;
			}
			save_btn.setEnabled(false);
			evaluationStep.setFilterType(voteYesBtn.getSelection() ? com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_NO : com.gx.obe.components.core.enumeration.EvaluationStepEnum.FILTER_NO);
			tenderProjectEntity.setEffectiveMemo(reasonText.getText().trim());
			evaluationStep.setTenderId(tenderProjectEntity.getId());
			evaluationStep.setEvaluationParam(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_WASTE_NEED_VOTE, voteYesBtn.getSelection());
			evaluationStep = evaluationStepService.saveWasteBidStep(tenderProjectEntity, evaluationStep);
			if (null == evaluationStep) {
				MessageDialog.openError(getShell(), evaluationStep.getStepName()+ Messages.Tips_wasteStepFail);
				save_btn.setEnabled(true);
				return;
			}

//			//如果需要表决
//			if (voteYesBtn.getSelection()) {
//				if(isExpert && isCreateStep){
//					ExpertEvaluationResult expertEvaluationResult = new ExpertEvaluationResult();
//					expertEvaluationResult.setId(UuidUtils.getUUID());
//					expertEvaluationResult.setStepId(evaluationStep.getId());
//					expertEvaluationResult.setTenderId(tenderProjectEntity.getId());
//					expertEvaluationResult.setUserId(Constants.CUR_USER.getId());
//					expertEvaluationResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED);
//					expertEvaluationResultService.insert(expertEvaluationResult);
//					
//					// 修改评标专家的评审记录
//					expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.COMPLETE, tenderProjectEntity.getId(), evaluationStep, expertEvaluationResult.getUserId());
//					
//				}
//			} 
			if (null != callBack) {
				callBack.callBackObject(evaluationStep);
			}
		}
	}
	

	public CallBack getCallBack() {
		return callBack;
	}

	public void setCallBack(CallBack callBack) {
		this.callBack = callBack;
	}

	protected void checkSubclass() {

	}
}
