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

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.RectangleRadioButton;
import org.eclipse.swt.custom.CLabel;
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.action.bean.Action;
import com.gx.obe.action.listener.ActionListener;
import com.gx.obe.action.utils.ActionUtil;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.evaluation.step.action.LoadBidPriceSingAction;
import com.gx.obe.evaluation.step.resutl.action.RecommonedEvaluationResultAction;
import com.gx.obe.message.Messages;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 开评标结果界面
 * @author wangx
 */
public class BidOpenAndEvaluationResultComposite extends Composite implements ActionListener{

	private Composite loadComposite;
	private TenderProjectEntity tenderProject;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private Composite eTabFolder;
	private EvaluationStep recommonedStep;
	private TenderProjectRuleEntity projectRule;
	private String expertSelfUserId;
	private String VIEW_OPEN_RESULT;
	private String VIEW_EVAL_RESULT;
	private RectangleRadioButton openingResultBtn;
	private RectangleRadioButton evaluationResultBtn;
	private RectangleRadioButton curBtn = null;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public BidOpenAndEvaluationResultComposite(Composite parent, int style) {
		super(parent, style);
		tenderProject = (TenderProjectEntity)parent.getData("tenderProject");
		expertSelfUserId = (String)parent.getData("expertSelfUserId");
		VIEW_OPEN_RESULT = (String)parent.getData(ConfigConstants.VIEW_OPEN_RESULT);
		VIEW_EVAL_RESULT = (String)parent.getData(ConfigConstants.VIEW_EVAL_RESULT);
		
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		eTabFolder = new Composite(this, SWT.NONE);
		GridLayout gl_eTabFolder = new GridLayout(2, false);
		gl_eTabFolder.verticalSpacing = 0;
		eTabFolder.setLayout(gl_eTabFolder);
		eTabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		openingResultBtn = new RectangleRadioButton(eTabFolder, SWT.NONE);
		openingResultBtn.setText(Messages.BidOpeningResult);
		
		evaluationResultBtn = new RectangleRadioButton(eTabFolder, SWT.NONE);
		evaluationResultBtn.setText(Messages.BidEvaluationResult);
		
		ESepator expert_tab = new ESepator(eTabFolder, SWT.NONE);
		expert_tab.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		expert_tab.setBackground(ColorConstants.SKIN_BG);
		expert_tab.setLineWidth(2);
		
		Composite topComposite = new Composite(this, SWT.NONE);
		topComposite.setBackground(SWTResourceManager.getColor(240, 240, 240));
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_topComposite = new GridLayout(1, false);
		gl_topComposite.marginBottom = 1;
		gl_topComposite.marginRight = 1;
		gl_topComposite.marginLeft = 1;
		gl_topComposite.horizontalSpacing = 0;
		gl_topComposite.marginHeight = 0;
		gl_topComposite.marginWidth = 0;
		gl_topComposite.verticalSpacing = 0;
		topComposite.setLayout(gl_topComposite);
		
		loadComposite = new Composite(topComposite, SWT.NONE);
		loadComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		loadComposite.setBackground(SWTResourceManager.getColor(255, 255, 255));
		FillLayout fl_loadComposite = new FillLayout(SWT.HORIZONTAL);
		fl_loadComposite.marginWidth = 5;
		fl_loadComposite.marginHeight = 5;
		loadComposite.setLayout(fl_loadComposite);
		loadComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author wangxiang
	 */
	private void init(){
//		tenderProject = tenderProjectService.getById(tenderProject.getId(), TenderProject.class);
		loadComposite.setData("tenderProject", tenderProject);
		
		if (CommonEnum.YES.equals(VIEW_OPEN_RESULT)) {
			final Action action = ActionUtil.getAction(LoadBidPriceSingAction.class.getName());
			action.setData("tenderProject", tenderProject);
			action.addActionListener(this);
			openingResultBtn.addMouseListener(new MouseAdapter() {
				public void mouseUp(MouseEvent e) {
					if(null != curBtn){
						curBtn.setSelection(false);
					}
					curBtn = openingResultBtn;
					curBtn.setSelection(true);
					action.run();
				}
			});
			curBtn = openingResultBtn;
		}else {
			WidgetUtils.setControlVisible(openingResultBtn, false);
		}
		
		if(CommonEnum.YES.equals(VIEW_EVAL_RESULT)){
			List<EvaluationStep> recommonedStepList = evaluationStepService.getEvaluationStepListByType(tenderProject.getId(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.RECOMMONED);
			if(null != recommonedStepList && recommonedStepList.size() > 0){
				recommonedStep = recommonedStepList.get(0);
//				if(null == curExpertId || "".equals(curExpertId)){
//					curExpert = expertService.getEvaluationStepExpert(tenderProject.getId(), recommonedStep);
//				}else{
//					curExpert = expertService.getById(curExpertId, Expert.class);
//				}
				final Action action = ActionUtil.getAction(RecommonedEvaluationResultAction.class.getName());
				action.addActionListener(this);
				action.setData("evaluationStep", recommonedStep);
				action.setTenderProjectRule(projectRule);
				action.setTenderProject(tenderProject);
				evaluationResultBtn.addMouseListener(new MouseAdapter() {
					public void mouseUp(MouseEvent e) {
						if(null != curBtn){
							curBtn.setSelection(false);
						}
						curBtn = evaluationResultBtn;
						curBtn.setSelection(true);
						action.run();
					}
				});
				curBtn = evaluationResultBtn;
			}else {
				WidgetUtils.setControlVisible(evaluationResultBtn, false);
			}
		}else {
			WidgetUtils.setControlVisible(evaluationResultBtn, false);
		}
		if(null != curBtn){
			curBtn.notifyListeners(SWT.MouseUp, null);
		}else {
			eTabFolder.setVisible(false);
			Composite composite = new Composite(loadComposite, SWT.NONE);
			composite.setLayout(new GridLayout(1, false));
			
			CLabel error = new CLabel(composite, SWT.NONE);
			error.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
			error.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tips_error.png"));
			error.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.BOLD));
			error.setForeground(ColorConstants.FONT_BLUE_COLOR);
			error.setText("对不起，您当前没有可查看的权限！");
		}
	}

	public void onLoad(Action action) {
		for(Control control : loadComposite.getChildren()){
			control.dispose();
		}
		loadComposite.setData("expertSelfUserId", expertSelfUserId);
	}

	public void loadComposite(Action action, Composite composite) {
		loadComposite.layout();
	}

	public Composite getParentComposite() {
		return loadComposite;
	}
	@Override
	protected void checkSubclass() {
	}

}
