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

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
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.custom.CLabel;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
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.entity.EditPriceItem;
import com.gx.obe.business.entity.EditPriceItemResult;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.BidPriceEnum;
import com.gx.obe.components.core.util.MoneyUtils;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import org.eclipse.swt.core.utils.WidgetUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 修正报价结果界面
 * @author chenxw
 */
public class EditPriceResultDialog extends EDialog {
	
	private final String[] addSubSymbol = { "增加", "减少" };
	private final String[] floatSymbol = { "上浮", "下调" };
	
	private Composite mainComposite;
	private CLabel supplierNameLabel;
	private Grid editPriceItemGrid;
	private Label itemNameLabel;
	private Composite editPriceComposite;
	private Combo editPriceCombo;
	private Text editPriceText;
	private Label editPriceUnitLabel;
	private Label priceNameLabel;
	private Label priceLabel;
	private Label priceUnitlabel;
	private Text editReasonText;
	
	private final List<EditPriceItem> editPriceItemList;
	private final Map<String, EditPriceItemResult> editPriceItemResultMap;
	private final TenderProjectRuleEntity tenderProjectRuleEntity;
	private final BigDecimal priceOriginal;
	
	private EditPriceItem curEditPriceItem;
	private String supplierName;
	
	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param priceOriginal
	 * @param editPriceItemResultMap
	 * @param editPriceItemList
	 */
	public EditPriceResultDialog(Shell parentShell, List<EditPriceItem> editPriceItemList, Map<String, EditPriceItemResult> editPriceItemResultMap, TenderProjectRuleEntity tenderProjectRuleEntity, BigDecimal priceOriginal) {
		super(parentShell);
		
		setTitle(Messages.Button_correctionBidPrice);
		setTitleImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/edit.png"));
		
		this.editPriceItemList = editPriceItemList;
		this.editPriceItemResultMap = editPriceItemResultMap;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		this.priceOriginal = priceOriginal;
	}
	
	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false));
		
		mainComposite = new Composite(container, SWT.NONE);
		mainComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		mainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_mainComposite = new GridLayout(2, false);
		gl_mainComposite.horizontalSpacing = 1;
		gl_mainComposite.verticalSpacing = 1;
		gl_mainComposite.marginHeight = 1;
		gl_mainComposite.marginWidth = 1;
		mainComposite.setLayout(gl_mainComposite);
		
		Composite topComposite = new Composite(mainComposite, SWT.NONE);
		topComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		topComposite.setLayout(new GridLayout(1, false));
		
		supplierNameLabel = new CLabel(topComposite, SWT.NONE);
		supplierNameLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		supplierNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_3 = new Composite(mainComposite, SWT.NONE);
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.marginHeight = 0;
		gl_composite_3.marginWidth = 0;
		composite_3.setLayout(gl_composite_3);
		GridData gd_composite_3 = new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 3);
		gd_composite_3.widthHint = 200;
		composite_3.setLayoutData(gd_composite_3);
		
		editPriceItemGrid = new Grid(composite_3, SWT.H_SCROLL | SWT.V_SCROLL);
		editPriceItemGrid.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				selectionEditPriceItemAction();
			}
		});
		editPriceItemGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		editPriceItemGrid.setHeaderVisible(true);
		editPriceItemGrid.setItemHeight(Constants.ROW_HIGHT);
		
		GridColumn gridColumn = new GridColumn(editPriceItemGrid, SWT.NONE);
		gridColumn.setText("修正项");
		gridColumn.setWidth(200);
		
		Composite temNameComposite = new Composite(mainComposite, SWT.NONE);
		temNameComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		temNameComposite.setLayout(new GridLayout(2, false));
		temNameComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 2));
		
		Label label = new Label(temNameComposite, SWT.NONE);
		label.setText("修正项：");
		
		itemNameLabel = new Label(temNameComposite, SWT.NONE);
		itemNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
//		Composite composite_4 = new Composite(mainComposite, SWT.NONE);
//		composite_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
//		composite_4.setLayout(new GridLayout(1, false));
//		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		editPriceComposite = new Composite(mainComposite, SWT.NONE);
		editPriceComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		editPriceComposite.setLayout(new GridLayout(3, false));
		editPriceComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		editPriceCombo = new Combo(editPriceComposite, SWT.READ_ONLY);
		editPriceCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				editPriceSelectoinAction();
			}
		});
		editPriceCombo.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		editPriceCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		editPriceText = new Text(editPriceComposite, SWT.BORDER | SWT.CENTER);
		editPriceText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				editPriceModifyAction(this);
			}
		});
		editPriceText.setBackground(SWTResourceManager.getColor(255, 255, 255));
		editPriceText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		editPriceText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		editPriceUnitLabel = new Label(editPriceComposite, SWT.NONE);
		editPriceUnitLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		editPriceUnitLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		
		priceNameLabel = new Label(editPriceComposite, SWT.NONE);
		priceNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		priceNameLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		
		Composite priceComposite = new Composite(editPriceComposite, SWT.NONE);
		GridLayout gl_priceComposite = new GridLayout(1, false);
		gl_priceComposite.verticalSpacing = 0;
		gl_priceComposite.horizontalSpacing = 0;
		gl_priceComposite.marginHeight = 0;
		gl_priceComposite.marginWidth = 0;
		priceComposite.setLayout(gl_priceComposite);
		GridData gd_priceComposite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_priceComposite.heightHint = 40;
		priceComposite.setLayoutData(gd_priceComposite);
		
		priceLabel = new Label(priceComposite, SWT.CENTER);
		GridData gd_priceLabel = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
		gd_priceLabel.widthHint = 200;
		priceLabel.setLayoutData(gd_priceLabel);
		priceLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		
		ESepator sep = new ESepator(priceComposite, SWT.NONE);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		priceUnitlabel = new Label(editPriceComposite, SWT.NONE);
		priceUnitlabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		
		Label editReasonLabel = new Label(editPriceComposite, SWT.NONE);
		editReasonLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		editReasonLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		editReasonLabel.setText("修正说明：");
		
		editReasonText = new Text(editPriceComposite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		GridData gd_editReasonText = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
		gd_editReasonText.heightHint = 100;
		editReasonText.setLayoutData(gd_editReasonText);
		editReasonText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		editReasonText.setTextLimit(500);
		
//		Composite composite_2 = new Composite(mainComposite, SWT.NONE);
//		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
//		composite_2.setLayout(new GridLayout(2, false));
//		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		EButton button = new EButton(editPriceComposite, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				confirmAction();
			}
		});
		GridData gd_button = new GridData(SWT.CENTER, SWT.CENTER, true, false, 3, 1);
		gd_button.widthHint = 70;
		button.setLayoutData(gd_button);
		button.setText("确   认");
		
//		EButton button_1 = new EButton(composite_2, SWT.NONE);
//		button_1.addMouseListener(new MouseAdapter() {
//			public void mouseUp(MouseEvent e) {
//				close();
//			}
//		});
//		GridData gd_button_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
//		gd_button_1.widthHint = 70;
//		button_1.setLayoutData(gd_button_1);
//		button_1.setText("取   消");
		
		init();
		
		return container;
	}
	
	/**
	 * @Description: 初始化
	 * @author chenxw
	 */
	private void init() {
		supplierNameLabel.setText(supplierName);
		initEditPriceItemGrid();
	}
	
	/**
	 * @Description: 初始化修正报价项表格
	 * @author chenxw
	 */
	private void initEditPriceItemGrid() {
		if (CollectionUtils.isNotNull(editPriceItemList)) for (EditPriceItem editPriceItem : editPriceItemList) {
			GridItem gridItem = new GridItem(editPriceItemGrid, SWT.NONE);
			gridItem.setText(editPriceItem.getItemName());
		}
		
		if (editPriceItemGrid.getItemCount() > 0) {
			if (editPriceItemGrid.getItemCount() == 1) {
				WidgetUtils.setControlVisible(editPriceItemGrid.getParent(), false);
				((GridLayout) mainComposite.getLayout()).numColumns = 1;
			}
			editPriceItemGrid.setSelection(0);
			selectionEditPriceItemAction();
		} else {
			editPriceComposite.dispose();
		}
	}
	
	/**
	 * @Description: 选择修正报价项事件
	 * @author chenxw
	 */
	private void selectionEditPriceItemAction() {
		int selectionIndex = editPriceItemGrid.getSelectionIndex();
		
		curEditPriceItem = editPriceItemList.get(selectionIndex);
		refreshEditPriceComposite();
	}
	
	/**
	 * @Description: 刷新修正报价界面
	 * @author chenxw
	 */
	private void refreshEditPriceComposite() {
		refreshEditPriceControl();
		EditPriceItemResult editPriceItemResult = editPriceItemResultMap.get(curEditPriceItem.getId());
		BigDecimal editPrice = editPriceItemResult.getEditPrice();
		if (null == editPrice) {
			editPriceCombo.select(0);
		} else {
			if (editPrice.compareTo(BigDecimal.ZERO) < 0) {
				editPrice = editPrice.multiply(BigDecimal.valueOf(-1));
				editPriceCombo.select(1);
			} else {
				editPriceCombo.select(0);
			}
			if (BidPriceEnum.EDIT_ADD_SUB.equals(curEditPriceItem.getEditType())) {
				editPriceText.setText(MoneyUtils.getMoneyFormatByUnit(editPrice, tenderProjectRuleEntity.getPriceUnitUseDefault()));
			} else if (BidPriceEnum.EDIT_FLOAT.equals(curEditPriceItem.getEditType())) {
				editPriceText.setText(moneyToFloat(editPrice));
			}
			editPriceModifyAction(null);
		}
		editPriceSelectoinAction();
		editReasonText.setText(ObjectUtils.getObjString(editPriceItemResult.getEditReason()));
		
		boolean enabled = !BidPriceEnum.DEVIATE_CALUSE.equals(curEditPriceItem.getItemCode());
		
		editPriceCombo.setEnabled(enabled);
		editPriceText.setEnabled(enabled);
		editReasonText.setEnabled(enabled);
	}
	
	/**
	 * @Description: 刷新修正报价控件
	 * @author chenxw
	 */
	private void refreshEditPriceControl() {
		itemNameLabel.setText(ObjectUtils.getObjString(curEditPriceItem.getItemName()));
		editPriceCombo.removeAll();
		editPriceText.setText("");
		editPriceUnitLabel.setText("");
		priceUnitlabel.setText("");
		if (BidPriceEnum.EDIT_ADD_SUB.equals(curEditPriceItem.getEditType())) {
			editPriceCombo.add(addSubSymbol[0], 0);
			editPriceCombo.add(addSubSymbol[1], 1);
			editPriceUnitLabel.setText(tenderProjectRuleEntity.getPriceUnitUseDefault());
			priceUnitlabel.setText("%");
		} else if (BidPriceEnum.EDIT_FLOAT.equals(curEditPriceItem.getEditType())) {
			editPriceCombo.add(floatSymbol[0], 0);
			editPriceCombo.add(floatSymbol[1], 1);
			editPriceUnitLabel.setText("%");
			priceUnitlabel.setText(tenderProjectRuleEntity.getPriceUnitUseDefault());
		}
	}
	
	/**
	 * @Description: 修正价格监听
	 * @author chenxw
	 * @param modifyListener
	 */
	private void editPriceModifyAction(ModifyListener modifyListener) {
		BigDecimal editPrice = ObjectUtils.getObjBigDecimal(editPriceText.getText().trim());
		if (null == editPrice) {
			editPriceText.removeModifyListener(modifyListener);
			editPriceText.setText("");
			editPriceText.addModifyListener(modifyListener);
			priceLabel.setText("");
		} else {
			if (BidPriceEnum.EDIT_ADD_SUB.equals(curEditPriceItem.getEditType())) {
				priceLabel.setText(moneyToFloat(MoneyUtils.getMoneyFromText(editPriceText.getText().trim(), tenderProjectRuleEntity.getPriceUnitUseDefault())));
			} else if (BidPriceEnum.EDIT_FLOAT.equals(curEditPriceItem.getEditType())) {
				priceLabel.setText(floatToMoney(editPrice));
			}
		}
	}
	
	/**
	 * @Description: 金额转浮动
	 * @author chenxw
	 * @param editPrice
	 * @return
	 */
	private String moneyToFloat(BigDecimal editPrice) {
		return editPrice.multiply(BigDecimal.valueOf(100)).divide(priceOriginal, 2, BigDecimal.ROUND_HALF_UP).toPlainString();
	}
	
	/**
	 * @Description: 浮动转金额
	 * @author chenxw
	 * @param editPrice
	 * @param priceOriginal
	 * @return
	 */
	private String floatToMoney(BigDecimal editPrice) {
		return MoneyUtils.getMoneyFormatByUnit(editPrice.divide(BigDecimal.valueOf(100)).multiply(priceOriginal), tenderProjectRuleEntity.getPriceUnitUseDefault());
	}
	
	/**
	 * @Description: 修正报价下拉监听
	 * @author chenxw
	 */
	private void editPriceSelectoinAction() {
		int selectionIndex = editPriceCombo.getSelectionIndex();
		if (BidPriceEnum.EDIT_ADD_SUB.equals(curEditPriceItem.getEditType())) {
			priceNameLabel.setText(floatSymbol[selectionIndex]);
		} else if (BidPriceEnum.EDIT_FLOAT.equals(curEditPriceItem.getEditType())) {
			priceNameLabel.setText(addSubSymbol[selectionIndex]);
		}
	}
	
	/**
	 * @Description: 确认事件
	 * @author chenxw
	 */
	private void confirmAction() {
		String editPriceString = editPriceText.getText().trim();
		if (editPriceString.isEmpty()) {
			MessageDialog.openWarning(getShell(), "修正报价不能为空！");
			editPriceText.setFocus();
			return;
		}
		if (editPriceCombo.getSelectionIndex() == 1) {
			editPriceString = "-" + editPriceString;
		}
		
		EditPriceItemResult editPriceItemResult = editPriceItemResultMap.get(curEditPriceItem.getId());
		if (BidPriceEnum.EDIT_ADD_SUB.equals(curEditPriceItem.getEditType())) {
			editPriceItemResult.setEditPrice(MoneyUtils.getMoneyFromText(editPriceString, tenderProjectRuleEntity.getPriceUnitUseDefault()));
		} else if (BidPriceEnum.EDIT_FLOAT.equals(curEditPriceItem.getEditType())) {
			editPriceItemResult.setEditPrice(ObjectUtils.getObjBigDecimal(editPriceString).divide(BigDecimal.valueOf(100)).multiply(priceOriginal));
		}
		editPriceItemResult.setEditReason(editReasonText.getText().trim());
		MessageDialog.openShortMessage("已确认！");
	}
	
	public void setSupplierName(String supplierName) {
		this.supplierName = supplierName;
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	protected Point getInitialSize() {
		return new Point(560, 380);
	}
	
}
