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

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
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.listener.RoundBorderLineListener;
import org.eclipse.swt.core.widgets.ELink;
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.common.icons.IconsActivator;
import com.gx.obe.common.widget.composite.TitleComposite;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.BidPriceEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.ColumnAlignmentUtils;
import com.gx.obe.util.utils.ListSortUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierOpeningResult;
import com.gx.obe.web.entity.price.BidPrice;
import com.gx.obe.web.entity.price.BidPriceResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.BidPriceResultService;
import com.gx.obe.web.service.BidPriceService;
import com.gx.obe.web.service.ProjectRuleService;
import com.gx.obe.web.service.SupplierService;
import com.gx.tools.utils.BidPriceUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 查看唱标结果
 * @author wangx
 */
public class ViewBidPriceSingComposite extends Composite{

	private SupplierService supplierService = new SupplierService();
	private BidPriceService bidPriceService = new BidPriceService();
	private BidPriceResultService bidPriceResultService = new BidPriceResultService();
	private ProjectRuleService projectRuleService = new ProjectRuleService();
	private TenderProjectEntity tenderProject;
	private TenderProjectRuleEntity projectRule;
	private List<Supplier> supplierList;
	private Composite singBidPriceContentComposite;
	private Grid bidPriceTableTotalGrid;
	private List<BidPrice> bidPriceList;
	private Map<String, Integer> bidPriceTableTotalColumnMap;
	private String SUPPLIER_NAME;
	private TitleComposite titleComposite;
	private ELink priceSortElink;
	private ELink priceSortDescElink;
	
	{
		LogUtils.logClass(ViewBidPriceSingComposite.class);
	}
	
	/**
	 * @param parentShell
	 * @param tenderProject
	 * @param projectRule
	 * @param supplier
	 */
	public ViewBidPriceSingComposite(Composite composite, int style) {
		super(composite, style);
		this.tenderProject = (TenderProjectEntity) composite.getData("tenderProject");
		
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 1;
		gridLayout.horizontalSpacing = 1;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		titleComposite = new TitleComposite(this, SWT.NONE);
		titleComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		priceSortElink = titleComposite.addELink("SIGN");
		priceSortElink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				sortBybidPrice(false);
				MessageDialog.openShortMessage(Messages.Tips_operateSuccess);
			}
		});
		priceSortElink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/up.png"));
		priceSortElink.setText(MessageFormat.format(Messages.PriorityLowToHightFormat, Messages.BidPrice));
		
		priceSortDescElink = titleComposite.addELink("SIGNDESC");
		priceSortDescElink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				sortBybidPrice(true);
				MessageDialog.openShortMessage(Messages.Tips_operateSuccess);
			}
		});
		priceSortDescElink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/down.png"));
		priceSortDescElink.setText(MessageFormat.format(Messages.PriorityHighToLowFormat, Messages.BidPrice));
		
		singBidPriceContentComposite = new Composite(this, SWT.NONE);
		GridLayout gl_composite_10 = new GridLayout(1, false);
		gl_composite_10.marginWidth = 1;
		gl_composite_10.marginHeight = 1;
		gl_composite_10.verticalSpacing = 0;
		singBidPriceContentComposite.setLayout(gl_composite_10);
		singBidPriceContentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		singBidPriceContentComposite.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		
		bidPriceTableTotalGrid = new Grid(singBidPriceContentComposite, SWT.H_SCROLL | SWT.V_SCROLL);
		bidPriceTableTotalGrid.setHeaderVisible(true);
		bidPriceTableTotalGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		init();
	}

	/**
	 * @Description: 初始化界面数据
	 * @author guoyr
	 */
	private void init(){
		SUPPLIER_NAME = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		projectRule = projectRuleService.getProjectRule(tenderProject.getId());
		supplierList = supplierService.getOpenBidSuccessSupplier(tenderProject.getId());
		bidPriceTableTotalGrid.setItemHeight(Constants.ROW_HIGHT);
		titleComposite.layout();
		refreshBidPriceTable();
		refreshPriceTableGrid();
	}
	
	/**
	 * @Description: 刷新开标一览表总表
	 * @author wangx 
	 */
	private void refreshPriceTableGrid(){
		bidPriceTableTotalGrid.removeAll();
		if(null != supplierList && supplierList.size() > 0){
			for(int i = 0; i < supplierList.size(); i++){
				Supplier supplier = supplierList.get(i);
				GridItem gridItem = new GridItem(bidPriceTableTotalGrid, SWT.NONE);
				gridItem.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NONE));
				gridItem.setText(0, supplier.getSupplierName());
				refreshSupplierOpenBidResult(supplier, gridItem);
				
			}
		}
	}
	
	private void refreshSupplierOpenBidResult(Supplier supplier, GridItem gridItem) {
		SupplierOpeningResult supplierOpeningResult = supplier.getSupplierOpeningResult();
		//获取报价结果
		List<BidPriceResult> bidPriceResultList = bidPriceResultService.getBidPriceResultTableList(tenderProject.getId(), supplier.getId());
		if(null == bidPriceResultList || bidPriceResultList.size() == 0){
			bidPriceResultList = new ArrayList<BidPriceResult>();
			if(null != supplierOpeningResult){
				bidPriceResultList.add(supplierOpeningResult.getBidPriceResult(projectRule.getPriceUnitUseDefault(), projectRule.getCurrencyCode(), projectRule.getCurrencyName()));
			}
		}
		for (BidPriceResult bidPriceResult : bidPriceResultList) {
			if(StringUtils.isEmpty(bidPriceResult.getBidPriceName())){
				continue;
			}
			int columnCount = 2;
			if(null != bidPriceTableTotalColumnMap.get(bidPriceResult.getBidPriceId())){
				columnCount = bidPriceTableTotalColumnMap.get(bidPriceResult.getBidPriceId());
				gridItem.setText(columnCount, bidPriceResult.getContentValue(projectRule.getPriceUnitUseDefault()));
			}
		}
	}

	/**
	 * @Description: 获得报价
	 * @author wangx 
	 */
	private void refreshBidPriceTable(){
		if(null == bidPriceTableTotalColumnMap){
			bidPriceTableTotalColumnMap = new HashMap<String, Integer>();
		}else {
			bidPriceTableTotalColumnMap.clear();
		}
		bidPriceList = bidPriceService.getBidPriceFactorList(tenderProject.getId());
		if(null == bidPriceList || bidPriceList.size() == 0){
			bidPriceList = new ArrayList<BidPrice>();
			bidPriceList.add(BidPriceUtils.getTotalBidprice());
		}
		for(GridColumn column : bidPriceTableTotalGrid.getColumns()){
			column.dispose();
		}
		GridColumn supplierNameColumn = new GridColumn(bidPriceTableTotalGrid, SWT.NONE);
		supplierNameColumn.setText(SUPPLIER_NAME);
		supplierNameColumn.setWidth(200);
		supplierNameColumn.setWordWrap(true);
		
		for(BidPrice bidPrice : bidPriceList){
			if(StringUtils.isEmpty(bidPrice.getBidPriceName())){
				continue;
			}
			GridColumn gridColumn = null;
			if(BidPriceEnum.IS_PRICE_ITEM.equals(bidPrice.getBidPriceType())){
				gridColumn = new GridColumn(bidPriceTableTotalGrid, SWT.RIGHT);;
			}else {
				gridColumn = new GridColumn(bidPriceTableTotalGrid, ColumnAlignmentUtils.getAlignment(bidPrice.getBidPriceName()));;
			}
			
			gridColumn.setWordWrap(true);
			gridColumn.setHeaderWordWrap(true);
			gridColumn.setHeaderFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
			gridColumn.setText(bidPrice.getBidPriceName());
			gridColumn.setMinimumWidth(100);
			gridColumn.setWidth(100);
			gridColumn.setMaxmumWidth(250);
			bidPriceTableTotalColumnMap.put(bidPrice.getId(), bidPriceTableTotalGrid.indexOf(gridColumn));
		}
	}
	
	/**
	 * @Description: 按报价排序
	 * @author wangxiang 
	 */
	private void sortBybidPrice(final boolean desc) {
		Comparator<Supplier> comparator = new Comparator<Supplier>() {
			public int compare(Supplier supplier1, Supplier supplier2) {
				SupplierOpeningResult supplierOpeningResult1 = supplier1.getSupplierOpeningResult();
				BigDecimal bidPrice1 = null != supplierOpeningResult1 ? supplierOpeningResult1.getBidPrice() : null;
				SupplierOpeningResult supplierOpeningResult2 = supplier2.getSupplierOpeningResult();
				BigDecimal bidPrice2 =  null != supplierOpeningResult2 ? supplierOpeningResult2.getBidPrice() : null;
				if(desc){
					return bidPrice2.compareTo(bidPrice1);
				}else{
					return bidPrice1.compareTo(bidPrice2);
				}
			}
		};
		
		ListSortUtils<Supplier> listSortUtils = new ListSortUtils<Supplier>(supplierList, comparator);
		for (Supplier supplier : supplierList) {
			supplier.setSortNo(listSortUtils.getSortNo(supplier));
		}
		Collections.sort(supplierList, comparator);
		refreshBidPriceTable();
		refreshPriceTableGrid();
	}
}
