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

import java.util.List;

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.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.file.enumeration.FileConstants;
import com.gx.obe.common.file.utils.ParsingXmlUtils;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.vo.BidPriceTable;
import com.gx.obe.components.core.vo.TableXML;
import com.gx.obe.components.core.vo.TableXML.Tr;
import com.gx.obe.components.core.vo.TableXML.Tr.Td;
import com.gx.obe.evaluation.step.utils.ParsingBidPriceUtils;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 投标报价表明细界面
 * @author chenxw
 */
public class BidPriceTableDetailComposite extends Composite {
	
	private Composite bidPriceTableComposite, subBidPriceTableComposite;
	
	private Grid bidPriceGrid, subBidPriceGrid;
	
	private TenderProjectEntity tenderProject;
	
	private Supplier supplier;
	
	/**
	 * @author chenxw
	 * @param parent
	 * @param style
	 */
	public BidPriceTableDetailComposite(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		Composite composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		SashForm sashForm = new SashForm(composite, SWT.VERTICAL);
		sashForm.setBackground(ColorConstants.SKIN_BG);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		bidPriceTableComposite = new Composite(sashForm, SWT.NONE);
		bidPriceTableComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_bidPriceTableComposite = new GridLayout(1, false);
		gl_bidPriceTableComposite.marginHeight = 0;
		gl_bidPriceTableComposite.marginWidth = 0;
		bidPriceTableComposite.setLayout(gl_bidPriceTableComposite);
		
		subBidPriceTableComposite = new Composite(sashForm, SWT.NONE);
		subBidPriceTableComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_subBidPriceTableComposite = new GridLayout(1, false);
		gl_subBidPriceTableComposite.marginHeight = 0;
		gl_subBidPriceTableComposite.marginWidth = 0;
		subBidPriceTableComposite.setLayout(gl_subBidPriceTableComposite);
		
		sashForm.setWeights(new int[] { 1, 1 });
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param tenderProject
	 * @param supplier
	 */
	public void refresh(TenderProjectEntity tenderProject, Supplier supplier) {
		this.tenderProject = tenderProject;
		this.supplier = supplier;
		refreshBidPriceGrid();
	}
	
	/**
	 * @Description: 刷新投标报价表格
	 * @author chenxw
	 */
	private void refreshBidPriceGrid() {
		if (null != bidPriceGrid) {
			bidPriceGrid.dispose();
		}
		bidPriceTableComposite.layout();
		
		final BidPriceTable bidPriceTable = ParsingBidPriceUtils.parsintBidPriceTable(tenderProject.getId(), supplier.getId());
		if (null == bidPriceTable) {
			return;
		}
		bidPriceGrid = refreshBidPriceTableInfoList(bidPriceTable, bidPriceTableComposite);
		if (null == bidPriceGrid) {
			return;
		}
		bidPriceTableComposite.layout();
		
		bidPriceGrid.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				refreshSubBidPriceGrid(bidPriceTable);
			}
		});
	}
	
	/**
	 * @Description: 刷新分项投标报价表格
	 * @author chenxw
	 * @param parentBidPriceGrid
	 * @param bidPriceTable
	 */
	private void refreshSubBidPriceGrid(BidPriceTable bidPriceTable) {
		if (null != subBidPriceGrid) {
			subBidPriceGrid.dispose();
		}
		subBidPriceTableComposite.layout();
		
		if (bidPriceGrid.getSelectionCount() == 0) {
			return;
		}
		String subjectTableID = (String) bidPriceGrid.getItem(bidPriceGrid.getSelectionIndex()).getData("subjectTableID");
		if (null == subjectTableID) {
			return;
		}
		subBidPriceGrid = refreshBidPriceTableInfoList(CollectionUtils.get(bidPriceTable.getBidPriceTableList(), t -> subjectTableID.equals(t.getSubjectTableID())), subBidPriceTableComposite);
		if (null == subBidPriceGrid) {
			return;
		}
		subBidPriceTableComposite.layout();
	}
	
	/**
	 * @Description: 刷新投标价列表
	 * @author chenxw
	 * @param bidPriceTable
	 * @param composite
	 */
	private Grid refreshBidPriceTableInfoList(final BidPriceTable bidPriceTable, Composite composite) {
		Grid bidPriceGrid = new Grid(composite, SWT.V_SCROLL | SWT.H_SCROLL);
		bidPriceGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		bidPriceGrid.setShowEvenBackground(false);
		bidPriceGrid.setLinesVisible(true);
		bidPriceGrid.setAutoHeight(true);
		bidPriceGrid.setHeaderVisible(true);
		bidPriceGrid.setItemHeight(Constants.ROW_HIGHT);
		
		TableXML detailTable = ParsingXmlUtils.parsingTableXML(FileConstants.getBidFilePath(tenderProject.getId(), supplier.getId()).concat(bidPriceTable.getDetailFileName()));
		if (null != detailTable) {
			refreshBidPriceTableCol(bidPriceGrid, detailTable.getHeadList());
			refreshBidPriceTableRow(bidPriceGrid, detailTable.getBodyList());
		}
		
		TableXML totalTable = ParsingXmlUtils.parsingTableXML(FileConstants.getBidFilePath(tenderProject.getId(), supplier.getId()).concat(bidPriceTable.getTotalFileName()));
		if (null != totalTable) {
			refreshTotalTable(bidPriceGrid, totalTable.getHeadList());
			refreshTotalTable(bidPriceGrid, totalTable.getBodyList());
		}
		
		return bidPriceGrid;
	}
	
	/**
	 * @Description: 刷新投标报价表格列
	 * @author chenxw
	 * @param bidPriceGrid
	 * @param trList
	 */
	private void refreshBidPriceTableCol(Grid bidPriceGrid, List<Tr> trList) {
		if (null == trList || trList.size() != 1) {
			return;
		}
		Td[] tds = trList.get(0).getTds();
		if (null == tds || tds.length == 0) {
			return;
		}
		for (int i = 0; i < tds.length; i++) {
			GridColumn gridColumn = new GridColumn(bidPriceGrid, SWT.NONE);
			gridColumn.setText(tds[i].getContent());
			gridColumn.setWidth(tds[i].getWidth());
			gridColumn.setData("colType", tds[i].getColType());
		}
	}
	
	/**
	 * @Description: 刷新投标报价表格行
	 * @author chenxw
	 * @param bidPriceGrid
	 * @param totalTable
	 * @param trList
	 */
	private void refreshBidPriceTableRow(Grid bidPriceGrid, List<Tr> trList) {
		if (null == trList || trList.isEmpty()) {
			return;
		}
		for (Tr tr : trList) {
			GridItem gridItem = new GridItem(bidPriceGrid, SWT.NONE);
			Td[] tds = tr.getTds();
			if (null == tds || tds.length == 0) {
				continue;
			}
			for (int i = 0; i < tds.length; i++) {
				gridItem.setText(i, tds[i].getContent());
			}
			gridItem.setData("subjectTableID", tr.getSubjectTableID());
		}
	}
	
	/**
	 * @Description: 刷新汇总表
	 * @author chenxw
	 * @param bidPriceGrid
	 * @param trList
	 */
	private void refreshTotalTable(Grid bidPriceGrid, List<Tr> trList) {
		if (null == trList || trList.isEmpty()) {
			return;
		}
		int columnCount = bidPriceGrid.getColumnCount();
		for (Tr tr : trList) {
			GridItem gridItem = new GridItem(bidPriceGrid, SWT.NONE);
			Td[] tds = tr.getTds();
			if (null == tds || tds.length == 0) {
				continue;
			}
			if (tds.length > columnCount) {
				continue;
			}
			int span = columnCount / tds.length;
			int remainder = columnCount % tds.length;
			for (int i = 0; i < tds.length - 1; i++) {
				gridItem.setColumnSpan(i * span, span - 1);
				gridItem.setText(i * span, tds[i].getContent());
			}
			gridItem.setColumnSpan((tds.length - 1) * span, span - 1 + remainder);
			gridItem.setText((tds.length - 1) * span, tds[tds.length - 1].getContent());
		}
	}
	
	@Override
	protected void checkSubclass() {}
	
}
