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

import java.util.ArrayList;
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.nebula.widgets.grid.listener.GridSelectionListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.listener.RoundBorderLineListener;
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.business.entity.BidPriceRound;
import com.gx.obe.common.widget.listener.SelectSupplierListener;
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.CollectionUtils;
import com.gx.obe.util.utils.ColumnAlignmentUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.price.BidPrice;
import com.gx.obe.web.entity.price.BidPriceHisResult;
import com.gx.obe.web.entity.price.BidPriceResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.BidPriceHisResultService;
import com.gx.obe.web.service.BidPriceService;
import com.gx.obe.web.service.SupplierService;
import com.swtdesigner.SWTResourceManager;

public class BidPriceResutlTableComposite extends Composite {

	private Grid supplierGird;
	private BidPriceService bidPriceService ;
	private BidPriceHisResultService bidPriceHisResultService ;
	private List<BidPrice> bidPriceList;
	private Map<String, Integer> bidPriceTableTotalColumnMap;
	private TenderProjectEntity tenderProject;
	private List<Supplier> supplierList;
	private SelectSupplierListener selectSupplierListener;
	private int round = 0;
	private Supplier selectSupplier;
	private GridColumn supplierNameColumn;
	private SupplierService supplierService = new SupplierService();
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public BidPriceResutlTableComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginWidth = 1;
		gridLayout.marginHeight = 1;
		setLayout(gridLayout);

		supplierGird = new Grid(this, SWT.H_SCROLL | SWT.V_SCROLL);
		supplierGird.setHeaderVisible(true);
		supplierGird.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridColumn numberColumn = new GridColumn(supplierGird, SWT.CENTER);
		numberColumn.setText(Messages.Num);
		numberColumn.setWidth(50);
		numberColumn.setMaxmumWidth(50);// TODO这里设置最大宽度下面的内容就不会加载出来
//		numberColumn.setHeaderFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
//		numberColumn.setWordWrap(true);
		supplierNameColumn = new GridColumn(supplierGird, SWT.NONE);
//		supplierNameColumn.setHeaderFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		supplierNameColumn.setText("投标人名称");
		supplierNameColumn.setWidth(100);
//		supplierNameColumn.setMaxmumWidth(300);
		supplierNameColumn.setWordWrap(true);
		init();
	}
	
	private void init(){
		bidPriceService = new BidPriceService();
		bidPriceHisResultService = new BidPriceHisResultService();
		this.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		supplierGird.setItemHeight(Constants.ROW_HIGHT);
		supplierGird.setAutoHeight(true);
		supplierGird.setAutoWidth(true);
		
		// 添加投标人表格的选中事件和右键菜单事件
		supplierGird.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void rightMouseButtonDown(GridItem item) {
				if(null != selectSupplierListener){
					selectSupplierListener.menuDetectedSupplier((Supplier)item.getData("supplier"));
				}
			}
			@Override
			public void leftMouseButtonDown(GridItem item) {
				selectSupplierAction();
			}
		});
	}

	
	public void refreshBidPriceResultTable(TenderProjectEntity tenderProject, List<Supplier> supplierList, int round, BidPriceRound curbidPriceRound){
		this.tenderProject= tenderProject;
		this.supplierList= supplierList;
		this.round= round;
		supplierNameColumn.setText(com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType()));
		if(null != curbidPriceRound && curbidPriceRound.getRoundNo() !=1 && curbidPriceRound.getRoundNo() !=0 ) {
			List<Supplier> roundSupplierList = supplierService.getPrivceRoundSupplierList(curbidPriceRound.getId());
			if(null != roundSupplierList && roundSupplierList.size() > 0) {
				this.supplierList = roundSupplierList;
			}
  		}
		refreshBidPriceTableTitle();
		refreshPriceTableGrid();
		supplierGird.resizeColumnWidth(1);
	}
	
	private void selectSupplierAction(){
		if(null != selectSupplierListener){
			if(supplierGird.getSelectionCount() > 0){
				selectSupplier = (Supplier)supplierGird.getSelection()[0].getData("supplier");
			}
			selectSupplierListener.selectSupplier(selectSupplier);
		}
	}
	
	public void refreshBidPriceTableTitle(){

		for(int i = 2; i < supplierGird.getColumnCount(); i++){
			GridColumn column = supplierGird.getColumn(i);
			column.dispose();
			i--;
		}
		if(null == tenderProject){
			return;
		}
		if(null != bidPriceList){
			bidPriceList.clear();
		}
		bidPriceList = bidPriceService.getTenderBidPriceTableList(tenderProject.getId());
		if(null == bidPriceList){
			bidPriceList = new ArrayList<BidPrice>();
		}
		if(null == bidPriceTableTotalColumnMap){
			bidPriceTableTotalColumnMap = new HashMap<String, Integer>();
		}
		bidPriceTableTotalColumnMap.clear();
		
		for(BidPrice bidPrice : bidPriceList){
			if(StringUtils.isEmpty(bidPrice.getBidPriceName())){
				continue;
			}
			GridColumn gridColumn = null;
			if(BidPriceEnum.IS_PRICE_ITEM.equals(bidPrice.getBidPriceType())){
				gridColumn = new GridColumn(supplierGird, SWT.RIGHT);;
			}else {
				gridColumn = new GridColumn(supplierGird, ColumnAlignmentUtils.getAlignment(bidPrice.getBidPriceName()));;
			}
			
			gridColumn.setWordWrap(true);
			gridColumn.setHeaderWordWrap(true);
//			gridColumn.setHeaderFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
			gridColumn.setText(bidPrice.getBidPriceName());
//			gridColumn.setMinimumWidth(100);
			gridColumn.setWidth(100);
//			gridColumn.setMaxmumWidth(250);
			bidPriceTableTotalColumnMap.put(bidPrice.getId(), supplierGird.indexOf(gridColumn));
		}
		// 如果显示标书密封情况
	}
	
	/**
	 * @Description: 刷新开标一览表总表
	 * @author guoyr 
	 */
	private void refreshPriceTableGrid(){
		supplierGird.removeAll();
		if(null == tenderProject){
			return;
		}
		
		
		if(null != supplierList){
			//获取报价结果
			List<BidPriceHisResult> allBidPriceResultList= bidPriceHisResultService.getSupplierBidPriceHisResultListByRound(tenderProject.getId(), null, round) ;
			
			int resultFontSize = 10;
			for(int i = 0; i < supplierList.size(); i++){
				Supplier supplier = supplierList.get(i);
				GridItem gridItem = new GridItem(supplierGird, SWT.NONE);
				gridItem.setFont(SWTResourceManager.getFont("微软雅黑", resultFontSize, SWT.NONE));
				gridItem.setText(0, "" +(i+1));
				gridItem.setText(1, supplier.getSupplierName());
				gridItem.setData("supplier", supplier);
				if(null != selectSupplier && supplier.getId().equals(selectSupplier.getId())){
					supplierGird.select(i);
				}
				
				List<BidPriceHisResult> bidPriceResultList = CollectionUtils.getFilterList(allBidPriceResultList, t -> {
					return supplier.getId().equals(t.getSupplierId());
				});
				refreshSupplierOpenBidResult(supplier, bidPriceResultList, gridItem);
			}
		}
		if(null != selectSupplierListener){
			if(supplierGird.getItemCount() > 0 && supplierGird.getSelectionCount() < 1){
				supplierGird.select(0);
			}
			selectSupplierAction();
		}
	}
	private void refreshSupplierOpenBidResult(Supplier supplier, List<BidPriceHisResult> bidPriceResultList, GridItem gridItem) {
		
//		List<BidPriceResult> bidPriceResultList = bidPriceResultService.getBidPriceResultTableList(tenderProject.getId(), supplier.getId());
		if(null == bidPriceResultList){
			bidPriceResultList = new ArrayList<BidPriceHisResult>();
		}
		// 如果投标报价多于一个，则显示总投标报价
		for (BidPriceResult bidPriceResult : bidPriceResultList) {
			int columnCount = 1;
			if(null != bidPriceTableTotalColumnMap.get(bidPriceResult.getBidPriceId())){
				columnCount = bidPriceTableTotalColumnMap.get(bidPriceResult.getBidPriceId());
				gridItem.setText(columnCount, bidPriceResult.getContentValue(null));
			}
		}
	}
	
	public void addSelectSupplierListener(SelectSupplierListener selectSupplierListener){
		this.selectSupplierListener = selectSupplierListener;
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
