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

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import com.gx.obe.components.core.AIDataParams;
import com.gx.obe.components.core.Constants;
import com.gx.obe.evaluation.step.listener.AiEvaluationListener;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.web.entity.EvaluationFactorResult;
import com.gx.obe.web.entity.Struct;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.TableTitle;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.KTableCellSelectionAdapter;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.RowHeightRender;

public class EvaluationSupplierComposite extends Composite {
	
	protected Composite tablcComposite;
	protected KTable table;
	protected List<Supplier> supplierList;
	protected EvaluationFactor curFactor;
	protected AIDataParams dataParams;
	protected int curRow = 1;
	protected int tableTitleStactCol = 1;
	protected boolean isEditEvaluationResult;
	protected List<TableTitle> tableTitleList;
	protected CommonKtableCModel tableCModel;
	protected int supplierNameCol;
	protected int evaluationResultCol;
	protected Map<String, EvaluationFactorResult> factorResultMap;
	protected AiEvaluationListener listener;
	protected Map<String, List<Struct>> structListMap;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public EvaluationSupplierComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				
			}
		});
		
		tablcComposite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		tablcComposite.setLayout(gl_composite);
		tablcComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
	
	}
	
	public void refresh(EvaluationFactor curFactor) {
		this.curFactor = curFactor;
		this.supplierList = listener.getSupplierList();
		this.factorResultMap = listener.getFactorResultMap();
		
		this.dataParams = curFactor.getAIDataParams();
		if (null == this.dataParams) {
			this.dataParams = new AIDataParams();
		}
		this.isEditEvaluationResult = dataParams.isEditEvaluationResult(curFactor.getDataType());
		this.tableTitleList = dataParams.getTableTitleList();
		
		initTableTitleList();
		drawTable();
	}
	
	protected void initTableTitleList() {}
	
	protected void drawTable() {
		for (Control control : tablcComposite.getChildren())
			control.dispose();
		
		table = new KTable(tablcComposite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.FILL_WITH_LASTCOL | SWTX.EDIT_ON_KEY);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		tableCModel = new CommonKtableCModel(table, 1, tableTitleStactCol, supplierList.size(), tableTitleList.size() + 1);
		table.setModel(tableCModel);
		table.setSelection(1, curRow, true);
		table.setAutoComputerSurplusWidth(true);
		
		//投标人名称
		supplierNameCol = 0;
		//评审结果分值
		evaluationResultCol = tableCModel.getColCount() - 1;
		
		//设置行高
		tableCModel.setRowHeightRender(new RowHeightRender() {
			public int receiveRowHeight(int row) {
				return Constants.ROW_HIGHT;
			}
		});
		
		//设置列宽
		tableCModel.setColumnWidth(supplierNameCol, 250);
		tableCModel.setColumnMaxWidth(supplierNameCol, 250);
		for (int i = 0; i < tableTitleList.size(); i++) {
			TableTitle tableTitle = tableTitleList.get(i);
			tableCModel.setColumnWidth(i + tableTitleStactCol, tableTitle.getWidth());
			tableCModel.setColumnMaxWidth(i + tableTitleStactCol, tableTitle.getWidth());
		}
		tableCModel.setColumnWidth(evaluationResultCol, 100);
		tableCModel.setColumnMaxWidth(evaluationResultCol, 100);
		
		//添加表格点击监听
		table.addCellSelectionListener(new KTableCellSelectionAdapter() {
			public void cellSelected(int col, int row, int statemask) {
				if (row < tableCModel.getFixedRowCount()) return;
				curRow = row;
				listener.selectingSupplierAction(supplierList.get(row - tableCModel.getFixedRowCount()));
			}
		});
		
		tablcComposite.layout();
	}

	/**
	 * @Description: 刷新投标人结构化数据
	 * @author chenxw
	 * @param supplierId 
	 */
	public void refreshSupplierStruct(String supplierId) {
		for (int i = 0; i < supplierList.size(); i++)
			if (supplierList.get(i).getId().equals(supplierId)) table.redraw(tableTitleStactCol, i + 1, tableTitleList.size(), 1);
	}
	
	/**
	 * @Description: 刷新投标人结果
	 * @author chenxw
	 * @param supplierId 
	 */
	public void refreshSupplierResult(String supplierId) {}
	
	/**
	 * @Description: 刷新表格数据
	 * @author chenxw 
	 */
	public void redrawTable() {
		table.redraw();
		tablcComposite.layout();
		if (CollectionUtils.isNotNull(supplierList)) {
			listener.selectingSupplierAction(supplierList.get(curRow - tableCModel.getFixedRowCount()));
		}
	}
	
	/**
	 * @Description: 调整列宽
	 * @author chenxw 
	 */
	public void resizeColumnOptimal() {
		table.resizeColumnOptimal();
	}
	
	public void addAiEvaluationListener(AiEvaluationListener listener) {
		this.listener = listener;
	}
	
	public void setStructListMap(Map<String, List<Struct>> structListMap) {
		this.structListMap = structListMap;
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
