package com.gx.obe.management.composite;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import com.gx.obe.action.bean.Action;
import com.gx.obe.common.widget.thread.SyncRunnable;
import com.gx.obe.common.widget.thread.SyncThread;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.evaluation.step.composite.WasteBidProjectStepComposite;
import com.gx.obe.evaluation.step.review.composite.WasteBidProjectEvaluationComposite;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.evaluation.EvaluationStepService;

public class WasteBidProjectComposite extends Composite {

	 private Action projectAction;
	 private TenderProjectEntity tenderProjectEntity;
	 private EvaluationStep evaluationStep;
	 private EvaluationStepService evaluationStepService;
	private Composite loadComposite;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public WasteBidProjectComposite(Composite parent, Action projectAction, int style) {
		super(parent, style);
		this.projectAction = projectAction;
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		loadComposite = new Composite(this, SWT.NONE);
		loadComposite.setLayout(new FillLayout(SWT.HORIZONTAL));

		init();
	}
	
	private void init() {
		tenderProjectEntity = projectAction.getTenderProject();
		evaluationStepService = new EvaluationStepService();
		
		loadWasteComposite();
	}
	
	private void loadWasteComposite(){
		new SyncThread(new SyncRunnable(loadComposite){
			@Override
			public void threadRun() {
				tenderProjectEntity = projectAction.refreshTenderProject();
				List<EvaluationStep> evaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), EvaluationStepEnum.WASTE_BID);
				if(null != evaluationStepList && evaluationStepList.size() > 0){
					evaluationStep = evaluationStepList.get(evaluationStepList.size() -1);
				}
			}
			@Override
			public void asyncExec() {
				for(Control control : loadComposite.getChildren()) {
					control.dispose();
				}
				loadComposite.setData("tenderProject", tenderProjectEntity);
				loadComposite.setData("evaluationStep", evaluationStep);
				if(null != evaluationStep) {
					loadComposite.setData("result","result");
					new WasteBidProjectEvaluationComposite(loadComposite, SWT.NONE);
				}else {
					WasteBidProjectStepComposite wasteBidProjectStepComposite = new WasteBidProjectStepComposite(loadComposite,  SWT.NONE);
					wasteBidProjectStepComposite.setCallBack(new CallBack() {

						@Override
						public void callBackObject(Object obj) {
							if(null != obj) {
								loadWasteComposite();
							}
						}
						
					});
				}
				loadComposite.layout();
			}
		}).start();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
