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

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.RoundBorderLineListener;
import org.eclipse.swt.core.widgets.EButton;
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.Composite;

import com.gx.obe.ColorConstants;
import com.gx.obe.action.bean.EAction;
import com.gx.obe.action.utils.ActionUtil;
import com.gx.obe.common.widget.button.EvalLeftButton;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.evaluation.step.action.ViewBidPriceEvaluationResultAction;
import com.gx.obe.evaluation.step.inter.IEvaluationService;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.CollectionUtils;
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.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 评审界面
 * @author chenxw 
 */
public class SingleEvaluationComposite extends Composite implements IEvaluationService {
	
	private EvalLeftButton bidPriceButton;
	private EvalLeftButton refreshButton;
	protected EButton save_result_btn;
	protected EButton submit_result_btn;
	protected Composite price_evaluation_table;
	private ExpertEvaluationListener expertEvaluationListener;
	protected TenderProjectEntity tenderProjectEntity;
	protected EvaluationStep evaluationStep;
	protected TenderProjectRuleEntity tenderProjectRuleEntity;
	protected Expert curExpert;
	private Composite left_tool_composite;
	
	{
		LogUtils.logClass(SingleEvaluationComposite.class);
	}
	
	/**
	 * @param parent
	 * @param style
	 */
	public SingleEvaluationComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		left_tool_composite = new Composite(this, SWT.NONE);
		left_tool_composite.setBackground(EvalLeftButton.BG);
		left_tool_composite.setBackgroundMode(SWT.INHERIT_FORCE);
		left_tool_composite.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		GridLayout gl_left_tool_composite = new GridLayout(1, false);
		left_tool_composite.setLayout(gl_left_tool_composite);
		
		EvalLeftButton goto_back = new EvalLeftButton(left_tool_composite, SWT.SELECTED);
		goto_back.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				goBackAction();
			}
		});
		goto_back.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		goto_back.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/back.png"));
		goto_back.setToolTipText(Messages.Button_return);
		
		refreshButton = new EvalLeftButton(left_tool_composite, SWT.SELECTED);
		refreshButton.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/refresh.png"));
		refreshButton.setToolTipText(Messages.Button_refresh);
		
		bidPriceButton = new EvalLeftButton(left_tool_composite, SWT.NONE);
		bidPriceButton.setImage(ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/bidPrice.png"));
		bidPriceButton.setToolTipText("查看报价（得分）");
		
		Composite evaluationComposite = new Composite(this, SWT.NONE);
		evaluationComposite.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 = 0;
		gl_container.marginWidth = 0;
		gl_container.horizontalSpacing = 0;
		evaluationComposite.setLayout(gl_container);
		
		price_evaluation_table = new Composite(evaluationComposite, SWT.NONE);
		price_evaluation_table.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		GridLayout gl_price_evaluation_table = new GridLayout(1, false);
		gl_price_evaluation_table.verticalSpacing = 0;
		gl_price_evaluation_table.horizontalSpacing = 0;
		gl_price_evaluation_table.marginHeight = 1;
		gl_price_evaluation_table.marginWidth = 1;
		price_evaluation_table.setLayout(gl_price_evaluation_table);
		price_evaluation_table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		price_evaluation_table.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		Composite composite_5 = new Composite(evaluationComposite, SWT.NONE);
		composite_5.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_5.setLayout(new GridLayout(2, false));
		
		save_result_btn = new EButton(composite_5, SWT.NONE | EButton.CSS_GREEN);
		GridData gd_save_result_btn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_save_result_btn.widthHint = 200;
		gd_save_result_btn.heightHint = 35;
		save_result_btn.setLayoutData(gd_save_result_btn);
		save_result_btn.setText(Messages.Button_saveEvaluationResult);
		save_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		
		submit_result_btn = new EButton(composite_5, SWT.NONE | EButton.CSS_GREEN);
		GridData gd_signBtn = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		gd_signBtn.heightHint = 35;
		gd_signBtn.widthHint = 200;
		submit_result_btn.setLayoutData(gd_signBtn);
		submit_result_btn.setText(Messages.Button_submitEvaluationResult);
		submit_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		
		initListener();
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param tenderProjectEntity
	 * @param evaluationStep
	 * @param tenderProjectRuleEntity
	 * @param isHideLeft 
	 */
	public void refresh(TenderProjectEntity tenderProjectEntity, EvaluationStep evaluationStep, TenderProjectRuleEntity tenderProjectRuleEntity, boolean isHideLeft) {
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		
		left_tool_composite.setVisible(!isHideLeft);
		((GridData)left_tool_composite.getLayoutData()).exclude = isHideLeft;
		
		refreshTenderInfo();
	}
	
	/**
	 * @Description: 刷新项目信息
	 * @author guoyr
	 */
	protected void refreshTenderInfo() {
		curExpert = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), evaluationStep);
		// 修改状态为正在进行中
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProjectEntity.getId(), evaluationStep, curExpert.getUserId());
	}
	
	
	/**
	 * @Description: 刷新监听
	 * @author chenxw
	 */
	private void initListener() {
		refreshButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				refreshTenderInfo();
			}
		});
		bidPriceButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				viewBidPice();
				
			}
		});
		save_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveEvaluationResultAction();
			}
		});
		submit_result_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				submitEvaluationResultAction();
			}
		});
	}
	
	/**
	 * @Description: 返回
	 * @author chenxw 
	 */
	protected void goBackAction() {
		if (null != expertEvaluationListener) expertEvaluationListener.complete();
	}
	
	/**
	 * @Description: 查看投标报价
	 * @author chenxw 
	 */
	public void viewBidPice() {
		EAction action = (EAction) ActionUtil.getEAction(ViewBidPriceEvaluationResultAction.class.getName());
		action.setData("evaluationStep", evaluationStep);
		action.setTenderProject(tenderProjectEntity);
		action.setData(Constants.CUR_EXPERT, curExpert);
		action.setName("查看价格");
		action.setParent(getShell());
		action.run();
	}
	
	/**
	 * @Description: 保存评标结果
	 * @author chenxw 
	 */
	protected void saveEvaluationResultAction() {
		if (!save_result_btn.getEnabled()) return;
	}
	
	/**
	 * @Description: 评审完成
	 * @author chenxw 
	 */
	protected boolean submitEvaluationResultAction() {
		if (!submit_result_btn.getEnabled()) return false;
		
		//个人不控制进度
		if (ObjectUtils.equals(tenderProjectRuleEntity.getOrderReview(), com.gx.obe.components.core.enumeration.EvaluationStepEnum.ORDER_SELF_FREE)) {
			List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateStepList(tenderProjectEntity.getId(), evaluationStep.getSortNo());
			if (null != evaluationStepList && evaluationStepList.size() > 0) {
				MessageDialog.openWarning(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_saveAllComplate, evaluationStepList.get(0).getStepName()));
				return false;
			}
		}
		
		// 未完成的可能会存在废标的评审节点
		List<EvaluationStep> evaluationStepList = evaluationStepService.getNotComplateFilterStepList(tenderProjectEntity.getId(), evaluationStep.getSortNo());
		if (CollectionUtils.isNotNull(evaluationStepList)) {
			if (MessageDialog.openConfirm(getShell(), Messages.Dialog_warmTips, MessageFormat.format(Messages.Tips_preStepNotCompleted, evaluationStepList.get(0).getStepName()), new String[]{Messages.Button_refresh, Messages.Button_cancel})) {
				refreshTenderInfo();
				return false;
			}
		}
		
		String message = MessageFormat.format(Messages.Tips_submitEvaluationResult, evaluationStep.getStepName());
		if (!MessageDialog.openConfirm(getShell(), Messages.Button_submitEvaluationResult, message, new String[] { Messages.Button_confirmComplate, Messages.Button_continueEvaluation})) {
			return false;
		}
		return true;
	}
	
	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}
	
	protected void checkSubclass() {}
}
