package com.gx.obe.management.dialog;

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.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ETabFolder;
import org.eclipse.swt.core.widgets.ETabItem;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
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 org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.formula.PriceParam;
import com.gx.obe.formula.composite.BasePriceComposite;
import com.gx.obe.formula.composite.ScoreCalculationComposite;
import com.gx.obe.formula.composite.SpecimenRangeComposite;
import com.gx.obe.message.Messages;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class PriceFormulaConfigDialog extends EDialog {
	
//	private TenderProjectRuleEntity tenderProjectRuleEntity;
//	private TenderProjectEntity tenderProjectEntity;
	private EvaluationFactor evaluationFactor;
	private PriceParam priceParam;
	private SpecimenRangeComposite specimenRangeComposite;
	private BasePriceComposite basePriceComposite;
	private ScoreCalculationComposite scoreCalculationComposite;
	private ScrolledComposite scrolledComposite;
	private Composite contentCcomposite;
	private ETabFolder eTabFolder;
	private Composite loadComposite;
	private StackLayout stackLayout =  new StackLayout();
	private boolean isSave = false;
	
	private TenderProjectEntity tenderProject;
	private TenderProjectRuleEntity	projectRule;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public PriceFormulaConfigDialog(Shell parentShell, TenderProjectEntity tenderProject, TenderProjectRuleEntity projectRule, EvaluationFactor evaluationFactor) {
		super(parentShell);
		setTitle(Messages.FormulaConfig);
		this.projectRule = projectRule;
		this.tenderProject = tenderProject;
		this.evaluationFactor = evaluationFactor;
		this.setOperator(SWT.MAX | SWT.CLOSE);
	}
	
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 10;
		gridLayout.marginWidth = 10;
		gridLayout.verticalSpacing = 0;
		container.setLayout(gridLayout);
		
		eTabFolder = new ETabFolder(container, SWT.NONE);
		eTabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		loadComposite = new Composite(container, SWT.NONE);
		loadComposite.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		loadComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		FillLayout fl_loadComposite = new FillLayout(SWT.HORIZONTAL);
		fl_loadComposite.marginWidth = 10;
		fl_loadComposite.marginHeight = 5;
		loadComposite.setLayout(fl_loadComposite);
		loadComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		loadComposite.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR, -1));
		
		scrolledComposite = new ScrolledComposite(loadComposite, SWT.V_SCROLL);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		contentCcomposite = new Composite(scrolledComposite, SWT.NONE);
		contentCcomposite.setLayout(stackLayout);
		
		specimenRangeComposite = new SpecimenRangeComposite(contentCcomposite, SWT.NONE);
		
		basePriceComposite = new BasePriceComposite(contentCcomposite, SWT.NONE);
		
		scoreCalculationComposite = new ScoreCalculationComposite(contentCcomposite, SWT.NONE);
		scrolledComposite.setContent(contentCcomposite);
		scrolledComposite.setMinSize(contentCcomposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		Composite operateComposite = new Composite(container, SWT.NONE);
		GridLayout gl_operateComposite = new GridLayout(3, false);
		gl_operateComposite.marginWidth = 0;
		gl_operateComposite.marginHeight = 0;
		gl_operateComposite.marginTop = 5;
		operateComposite.setLayout(gl_operateComposite);
		operateComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		operateComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		Label label = new Label(operateComposite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		EButton save_btn = new EButton(operateComposite, SWT.NONE);
		save_btn.setBackground(ColorConstants.SKIN_BG);
		save_btn.setForeground(ColorConstants.SKIN_FOREGROUND);
		save_btn.setActiveBorderColor(ColorConstants.SKIN_ACTIVE_BG);
		save_btn.setActiveBgColor(ColorConstants.SKIN_ACTIVE_BG);
		save_btn.setActiveForeground(ColorConstants.SKIN_ACTIVE_FOREGROUND);
		save_btn.setArcWidth(6);
		save_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		save_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				savePriceParamAction();
			}
		});
		GridData gd_savePriceParamBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_savePriceParamBtn.heightHint = 30;
		gd_savePriceParamBtn.widthHint = 90;
		save_btn.setLayoutData(gd_savePriceParamBtn);
		save_btn.setText(Messages.Button_ok);
		
		EButton cancel_btn = new EButton(operateComposite, SWT.NONE);
		cancel_btn.setBackground(ColorConstants.SKIN_BG);
		cancel_btn.setForeground(ColorConstants.SKIN_FOREGROUND);
		cancel_btn.setActiveBorderColor(ColorConstants.SKIN_ACTIVE_BG);
		cancel_btn.setActiveBgColor(ColorConstants.SKIN_ACTIVE_BG);
		cancel_btn.setActiveForeground(ColorConstants.SKIN_ACTIVE_FOREGROUND);
		cancel_btn.setArcWidth(6);
		cancel_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		cancel_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		GridData gd_cancelBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_cancelBtn.heightHint = 30;
		gd_cancelBtn.widthHint = 90;
		cancel_btn.setLayoutData(gd_cancelBtn);
		cancel_btn.setText(Messages.Button_cancel);
		
		init();
		return container;
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		if(null == projectRule){
			projectRule = new TenderProjectRuleEntity();
			projectRule.setTenderId(tenderProject.getId());
		}
		eTabFolder.setTabHeight(40);
		eTabFolder.setTopOffset(6);
		eTabFolder.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
		eTabFolder.setDefaultForeground(ColorConstants.NOTE_TIPS_COLOR);
		eTabFolder.setSelectionForeground(ResourceManager.getColor(SWT.COLOR_WHITE));
		eTabFolder.setDefaultBackgroundColor(SWTResourceManager.getColor(247, 247, 247));
		eTabFolder.setDefaultBorderColor(SWTResourceManager.getColor(224, 224, 224));
		eTabFolder.setSelectionBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tab_selected.png"));
		eTabFolder.setSelectionBorder(new int[]{7,7});
		
		{
			ETabItem baseInfoItem = new ETabItem(eTabFolder, SWT.NONE);
			baseInfoItem.setLeftMargin(10);
			baseInfoItem.setRightMargin(5);
//			baseInfoItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/number_1.png"));
			baseInfoItem.setText(Messages.SelectionBasePriceCalculationRange + " > ");
			baseInfoItem.addSelectionListener(new ETabFolder.SelectionListener() {
				public void selected(MouseEvent e) {
					stackLayout.topControl = specimenRangeComposite;
					contentCcomposite.layout();
				}
			});
		}
		{
			ETabItem baseInfoItem = new ETabItem(eTabFolder, SWT.NONE);
			baseInfoItem.setLeftMargin(15);
			baseInfoItem.setRightMargin(5);
//			baseInfoItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/number_2.png"));
			baseInfoItem.setText(Messages.SetBasePriceCalculationFormula + " > ");
			baseInfoItem.addSelectionListener(new ETabFolder.SelectionListener() {
				public void selected(MouseEvent e) {
					stackLayout.topControl = basePriceComposite;
					contentCcomposite.layout();
				}
			});
		}
		{
			ETabItem baseInfoItem = new ETabItem(eTabFolder, SWT.NONE);
			baseInfoItem.setLeftMargin(15);
			baseInfoItem.setRightMargin(15);
//			baseInfoItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/number_3.png"));
			baseInfoItem.setText(Messages.SetScoreCalculationFormula);
			baseInfoItem.addSelectionListener(new ETabFolder.SelectionListener() {
				public void selected(MouseEvent e) {
					stackLayout.topControl = scoreCalculationComposite;
					contentCcomposite.layout();
				}
			});
			baseInfoItem.notifyListeners(SWT.MouseUp, null);
		}
		
		// 反序列化对象
		priceParam = PriceParam.deserializePriceParam(evaluationFactor.getComputerParams());
		
		priceParam.setPriceFactorScore(evaluationFactor.getFactorScore());
		priceParam.setPriceFactorWeight(evaluationFactor.getFactorWeight());
		
		// 为价格公式设置拦标价
		if(null == priceParam.getSpecimenParam().getPriceCeiling()){
			priceParam.getSpecimenParam().setPriceCeiling(tenderProject.getBidPriceLimit());
		}
		
		// 样本范围设置
		specimenRangeComposite.init(priceParam, projectRule);
		// 基准价设置
		basePriceComposite.init(priceParam, projectRule);
		// 得分计算设置
		scoreCalculationComposite.init(priceParam, projectRule);
	}
	
	/**
	 * @Description: 保存
	 * @author guoyr
	 */
	private void savePriceParamAction(){
		priceParam = specimenRangeComposite.getPriceParam(priceParam);
		if(specimenRangeComposite.hasError()){
			return;
		}
		priceParam = basePriceComposite.getPriceParam(priceParam);
		if(basePriceComposite.hasError()){
			return;
		}
		priceParam = scoreCalculationComposite.getPriceParam(priceParam);
		if(scoreCalculationComposite.hasError()){
			return;
		}
		TenderProjectService tenderProjectService = new TenderProjectService();
		boolean isInEvaluationProgress = tenderProjectService.isInProgress(tenderProject.getId(), EvaluationStepEnum.PRICE);
		if(isInEvaluationProgress){
			MessageDialog.openInformation(getShell(), Messages.Dialog_warmTips, Messages.Tips_enteredPriceEvaluation);
			return;
		}
		isSave = true;
//		priceParam.refreshDesc();
		evaluationFactor.setComputerParams(priceParam.getPriceParamSerializable());
		getShell().close();
	}
	
	public TenderProjectRuleEntity getProjectRule() {
		return projectRule;
	}

	public boolean isSave() {
		return isSave;
	}

	public PriceParam getPriceParam() {
		return priceParam;
	}

	/**
	 * @Description: 取消
	 * @author guoyr
	 */
	private void cancelAction(){
		getShell().close();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(900, 600);
	}
}
