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

import java.math.BigDecimal;
import java.text.NumberFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.listener.NumberModifyListener;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
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.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.vo.BillFilter;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.ObjectUtils;

public class BillFilterDialog extends EDialog {
	private Text minDeviationPercentageText;
	private Text minWeightText;
	private Text maxDeviationPercentageText;
	private Text maxWeightText;
	private EButton button;
	private Composite roleTypeComposite;

	private BillFilter billFilter;
	private Button amongWeightButton;

	public BillFilter getBillFilter() {
		return billFilter;
	}

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param billFilter 
	 */
	public BillFilterDialog(Shell parentShell, BillFilter billFilter) {
		super(parentShell);
		setTitle("筛选清单项");
		this.billFilter = billFilter;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		container.setLayout(gl_container);

		Composite composite = new Composite(container, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		roleTypeComposite = new Composite(composite, SWT.NONE);
		GridLayout gl_roleTypeComposite = new GridLayout(3, false);
		gl_roleTypeComposite.verticalSpacing = 0;
		gl_roleTypeComposite.horizontalSpacing = 0;
		gl_roleTypeComposite.marginHeight = 1;
		gl_roleTypeComposite.marginWidth = 1;
		roleTypeComposite.setLayout(gl_roleTypeComposite);
		roleTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Label fill = new Label(roleTypeComposite, SWT.NONE);
		GridData gd_fill = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_fill.widthHint = 10;
		fill.setLayoutData(gd_fill);

		Button allButton = new Button(roleTypeComposite, SWT.RADIO);
		GridData gd_button_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
		gd_button_1.heightHint = 33;
		allButton.setLayoutData(gd_button_1);
		allButton.setText("列出全部清单");
		allButton.setData(BillFilter.ALL);
		allButton.setSelection(true);

		addESepator(roleTypeComposite);
		new Label(roleTypeComposite, SWT.NONE);

		Button underControlLineButton = new Button(roleTypeComposite, SWT.RADIO);
		GridData gd_button_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
		gd_button_2.heightHint = 33;
		underControlLineButton.setLayoutData(gd_button_2);
		underControlLineButton.setText("列出低于评标控制线的清单");
		underControlLineButton.setData(BillFilter.UNDER_CONTROL_LINE);

//		addESepator(roleTypeComposite);
//		new Label(roleTypeComposite, SWT.NONE);

//		Button exceedDoubleControlLineButton = new Button(roleTypeComposite, SWT.RADIO);
//		GridData gd_button_3 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
//		gd_button_3.heightHint = 33;
//		exceedDoubleControlLineButton.setLayoutData(gd_button_3);
//		exceedDoubleControlLineButton.setText("列出超过评标控制线200%的清单");
//		exceedDoubleControlLineButton.setData(BillFilter.EXCEED_DOUBLE_CONTROL_LINE);

		addESepator(roleTypeComposite);
		new Label(roleTypeComposite, SWT.NONE);

		Button amongDeviationPercentageButton = new Button(roleTypeComposite, SWT.RADIO);
		amongDeviationPercentageButton.setText("按偏差百分比过滤清单");
		amongDeviationPercentageButton.setData(BillFilter.AMONG_DEVIATION_PERCENTAGE);

		Composite composite_2 = new Composite(roleTypeComposite, SWT.NONE);
		composite_2.setLayout(new GridLayout(4, false));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		minDeviationPercentageText = new Text(composite_2, SWT.BORDER | SWT.RIGHT);
		GridData gd_minDeviationPercentageText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_minDeviationPercentageText.widthHint = 77;
		minDeviationPercentageText.addModifyListener(new NumberModifyListener(BigDecimal.class, "-100", "-100"));
		minDeviationPercentageText.setLayoutData(gd_minDeviationPercentageText);

		Label label = new Label(composite_2, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("% ~ ");

		maxDeviationPercentageText = new Text(composite_2, SWT.BORDER | SWT.RIGHT);
		maxDeviationPercentageText.addModifyListener(new NumberModifyListener(BigDecimal.class, "0"));
		GridData gd_maxDeviationPercentageText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_maxDeviationPercentageText.widthHint = 77;
		maxDeviationPercentageText.setLayoutData(gd_maxDeviationPercentageText);

		Label label_2 = new Label(composite_2, SWT.NONE);
		label_2.setText("%");

		addESepator(roleTypeComposite);
		new Label(roleTypeComposite, SWT.NONE);

		Composite composite_4 = new Composite(roleTypeComposite, SWT.NONE);
		GridLayout gl_composite_4 = new GridLayout(2, false);
		gl_composite_4.verticalSpacing = 0;
		gl_composite_4.horizontalSpacing = 0;
		gl_composite_4.marginHeight = 0;
		gl_composite_4.marginWidth = 0;
		composite_4.setLayout(gl_composite_4);
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		amongWeightButton = new Button(composite_4, SWT.CHECK);
		amongWeightButton.setText("按权重百分比过滤清单");

		Composite composite_3 = new Composite(composite_4, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_3.setLayout(new GridLayout(4, false));

		minWeightText = new Text(composite_3, SWT.BORDER | SWT.RIGHT);
		GridData gd_minWeightText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_minWeightText.widthHint = 77;
		minWeightText.setLayoutData(gd_minWeightText);
		minWeightText.addModifyListener(new NumberModifyListener(BigDecimal.class, "0", "0", "100"));

		Label label_1 = new Label(composite_3, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("% ~ ");

		maxWeightText = new Text(composite_3, SWT.BORDER | SWT.RIGHT);
		minWeightText.addModifyListener(new NumberModifyListener(BigDecimal.class, "0", "0"));
		GridData gd_maxWeightText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_maxWeightText.widthHint = 77;
		maxWeightText.setLayoutData(gd_maxWeightText);
		maxWeightText.addModifyListener(new NumberModifyListener(BigDecimal.class, "100", "0", "100"));

		Label label_3 = new Label(composite_3, SWT.NONE);
		label_3.setText("%");

		addESepator(roleTypeComposite);

		addESepator(container);

		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		button = new EButton(composite_1, SWT.NONE);
		GridData gd_button = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_button.widthHint = 50;
		button.setLayoutData(gd_button);
		button.setText(Messages.Button_confirm);

		init();
		return container;
	}

	private void init() {
		if (null == billFilter) {
			billFilter = new BillFilter();
		}
		
		WidgetUtils.setRadioChecked(roleTypeComposite, billFilter.getFilterType());
		amongWeightButton.setSelection(billFilter.isWeightAmong());
		
		minDeviationPercentageText.setText(null != billFilter.getMinDeviationPercentage() ? NumberFormat.getInstance().format(billFilter.getMinDeviationPercentage().multiply(BigDecimal.valueOf(100))):"-100");
		maxDeviationPercentageText.setText(null != billFilter.getMaxDeviationPercentage() ? NumberFormat.getInstance().format(billFilter.getMaxDeviationPercentage().multiply(BigDecimal.valueOf(100))) : "0");
		minWeightText.setText(null != billFilter.getMinWeight() ? NumberFormat.getInstance().format(billFilter.getMinWeight().multiply(BigDecimal.valueOf(100))) : "0");
		maxWeightText.setText(null != billFilter.getMaxWeight() ? NumberFormat.getInstance().format(billFilter.getMaxWeight().multiply(BigDecimal.valueOf(100))) : "100");
		
		initListener();

	}

	private void initListener() {
//		minDeviationPercentageText.addModifyListener(new ModifyListener() {
//			public void modifyText(ModifyEvent arg0) {
//				billFilter.setMinDeviationPercentage(getObjBigDecimal(minDeviationPercentageText, this));
//			}
//		});
//		maxDeviationPercentageText.addModifyListener(new ModifyListener() {
//			public void modifyText(ModifyEvent arg0) {
//				billFilter.setMaxDeviationPercentage(getObjBigDecimal(maxDeviationPercentageText, this));
//			}
//		});
//		minWeightText.addModifyListener(new ModifyListener() {
//			public void modifyText(ModifyEvent arg0) {
//				billFilter.setMinWeight(getObjBigDecimal(minWeightText, this));
//			}
//		});
//		maxWeightText.addModifyListener(new ModifyListener() {
//			public void modifyText(ModifyEvent arg0) {
//				billFilter.setMaxWeight(getObjBigDecimal(maxWeightText, this));
//			}
//		});
		button.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				billFilter.setFilterType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(roleTypeComposite)));
				billFilter.setWeightAmong(amongWeightButton.getSelection());
				billFilter.setMinDeviationPercentage(getObjBigDecimal(minDeviationPercentageText));
				billFilter.setMaxDeviationPercentage(getObjBigDecimal(maxDeviationPercentageText));
				billFilter.setMinWeight(getObjBigDecimal(minWeightText));
				billFilter.setMaxWeight(getObjBigDecimal(maxWeightText));
				BillFilter _billFilter = billFilter;
				close();
				billFilter = _billFilter;
			}
		});
	}

	public boolean close() {
		billFilter = null;
		return super.close();
	}
	
	private BigDecimal getObjBigDecimal(Text text) {
		BigDecimal textValue = ObjectUtils.getObjBigDecimal(text.getText().trim());
		if (null == textValue) {
			return null;
		}
		return textValue.divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_UP);
	}

	private void addESepator(Composite composite) {
		ESepator topSep = new ESepator(composite, SWT.NONE);
		topSep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		topSep.setLineStyle(SWT.LINE_DOT);
		topSep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
	}

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