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

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.evaluation.step.entity.BusinessSummariseResult;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.CellForegroundRender;
import de.kupzog.ktable.renderers.RowHeightRender;

/**
 * @Description: 商务评审结果界面
 * @author chenxw
 */
public class BusinessSummariseResultComposite extends Composite {
	
	private KTable resultTable;
	
	private TenderProjectEntity tenderProject;
	private List<Expert> expertList;
	private List<BusinessSummariseResult> summariseResultList;
	
	/**
	 * @author chenxw
	 * @param parent
	 * @param style
	 */
	public BusinessSummariseResultComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gl_evaluationTableComposite = new GridLayout(1, false);
		gl_evaluationTableComposite.marginHeight = 0;
		gl_evaluationTableComposite.marginWidth = 0;
		setLayout(gl_evaluationTableComposite);
		GridData gd_evaluationTableComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_evaluationTableComposite.minimumHeight = 200;
		setLayoutData(gd_evaluationTableComposite);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 */
	public void refresh(TenderProjectEntity tenderProject, List<Expert> expertList, List<BusinessSummariseResult> summariseResultList) {
		this.tenderProject = tenderProject;
		this.expertList = expertList;
		this.summariseResultList = summariseResultList;
		refreshTableComposite();
	}
	
	/**
	 * @Description: 刷新表格界面
	 * @author chenxw
	 */
	private void refreshTableComposite() {
		if (null != resultTable) {
			resultTable.dispose();
		}
		
		resultTable = new KTable(this, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		resultTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		final String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		
		final int headRow = 1;
		
		CommonKtableCModel tableModel = new CommonKtableCModel(resultTable, headRow, 1, summariseResultList.size(), expertList.size() + 2);
		
		final int supplierNameCol = 0;
		final int statisticResultCol = tableModel.getColCount() - 2;
		final int summariseResultCol = tableModel.getColCount() - 1;
		
		tableModel.setColumnWidth(supplierNameCol, 500);
		for (int i = 1; i <= expertList.size(); i++) {
			tableModel.setColumnWidth(supplierNameCol + i, 50);
		}
		tableModel.setColumnWidth(statisticResultCol, 100);
		tableModel.setColumnWidth(summariseResultCol, 50);
		
		tableModel.setRowHeightRender(new RowHeightRender() {
			public int receiveRowHeight(int row) {
				return Constants.ROW_HIGHT;
			}
		});
		
		tableModel.setCellContentRender(new CellContentRender() {
			public Object receiveContent(int row, int col) {
				if (row < headRow) {
					if (col == supplierNameCol) {
						return supplierName;
					} else if (col > 0 && col < expertList.size() + 1) {
						return expertList.get(col - 1).getUserName();
					} else if (col == statisticResultCol) {
						return "统计";
					} else if (col == summariseResultCol) {
						return "结论";
					}
				} else {
					BusinessSummariseResult summariseResult = summariseResultList.get(row - headRow);
					if (col == supplierNameCol) {
						return summariseResult.getSupplierName();
					} else if (col > 0 && col < expertList.size() + 1) {
						return summariseResult.getExpertResultStr(expertList.get(col - 1).getId());
					} else if (col == statisticResultCol) {
						return summariseResult.getStatisticResult();
					} else if (col == summariseResultCol) {
						return summariseResult.getSummariseResultStr();
					}
				}
				return "";
			}
		});
		
		tableModel.setCellAlignmentRender(new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (row < headRow) {
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
				} else {
					if (col == supplierNameCol) {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					} else if (col > 0 && col < expertList.size() + 1) {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					} else if (col == statisticResultCol) {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					} else if (col == summariseResultCol) {
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					}
				}
				return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
			}
		});
		
		// 设置单元格前景颜色
		tableModel.setCellForegroundRender(new CellForegroundRender() {
			public Color receiveCellForeground(int row, int col) {
				if (row >= headRow) {
					BusinessSummariseResult summariseResult = summariseResultList.get(row - headRow);
					if (col > 0 && col < expertList.size() + 1) {
						String expertResult = summariseResult.getExpertResultMap().get(expertList.get(col - 1).getId());
						if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(expertResult)) {
							return SWTResourceManager.getColor(SWT.COLOR_RED);
						} else if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED.equals(expertResult)) {
							return SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
						}
					} else if (col == summariseResultCol) {
						String result = summariseResult.getSummariseResult();
						if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(result)) {
							return SWTResourceManager.getColor(SWT.COLOR_RED);
						} else if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED.equals(result)) {
							return SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
						}
					}
				}
				return null;
			}
		});
		
		resultTable.setModel(tableModel);
		resultTable.refresh();
		
		layout();
	}
	
	protected void checkSubclass() {}
	
}
