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

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
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.dialog.EvaluationOpinionDialog;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
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.editors.KTableCellEditorGRadio;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.CellEditContentRender;
import de.kupzog.ktable.renderers.CellEditorRender;
import de.kupzog.ktable.renderers.CellForegroundRender;
import de.kupzog.ktable.renderers.RowHeightRender;

/** 
 * @Description: 商务评审结果界面
 * @author chenxw 
 */
public class BusinessVetoEvalResultComposite extends Composite {
	
	private final String EVAL_QUALIFIED = "合格";
	private final String EVAL_UNQUALIFIED = "不合格";
	
	private KTable resultTable;
	
	private TenderProjectEntity tenderProject;

	private List<Supplier> supplierList;

	private Map<String, ExpertEvaluationResult> evalResultMap;
	
	private boolean isEdit;
	
	/** 
	 * @author chenxw
	 * @param parent
	 * @param style 
	 */
	public BusinessVetoEvalResultComposite(Composite parent, int style, boolean isEdit) {
		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));
		
		this.isEdit = isEdit;
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param tenderProject
	 * @param evalResultMap 
	 * @param projectRule
	 * @param basePrice
	 * @param businessEvalResultList 
	 */
	public void refresh(TenderProjectEntity tenderProject, List<Supplier> supplierList, Map<String, ExpertEvaluationResult> evalResultMap) {
		this.tenderProject = tenderProject;
		this.supplierList = supplierList;
		this.evalResultMap = evalResultMap;
		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;
		final int bidNameCol = 0;
		final int bidResultCol = 1;
		
		CommonKtableCModel tableModel = new CommonKtableCModel(resultTable, headRow, 1, supplierList.size(), 2);
		
		tableModel.setColumnWidth(bidNameCol, 500);
		tableModel.setColumnWidth(bidResultCol, 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) {
					switch (col) {
						case bidNameCol: return supplierName.concat("名称");
						case bidResultCol: return "是否合格";
						default: return "";
					}
				} else {
					Supplier supplier = supplierList.get(row - headRow);
					switch (col) {
						case bidNameCol: return ObjectUtils.getObjString(supplier.getSupplierName());
						case bidResultCol:
							ExpertEvaluationResult evalResult = evalResultMap.get(supplier.getId());
							if (null == evalResult) {
								return "";
							}
							if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(evalResult.getAccessment())) {
								return EVAL_UNQUALIFIED;
							} else if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED.equals(evalResult.getAccessment())) {
								return EVAL_QUALIFIED;
							} else {
								return "";
							}
						default: 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 {
					switch (col) {
						case bidNameCol: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
						case bidResultCol: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
						default: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					}
				}
			}
		});
		
		if (isEdit) {
			// 设置单元格编辑器
			final KTableCellEditorGRadio radio = new KTableCellEditorGRadio();
			radio.setSingleClickEdit(true);
			radio.addItem(EVAL_QUALIFIED, com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED);
			radio.addItem(EVAL_UNQUALIFIED, com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED);
			tableModel.setCellEditorRender(new CellEditorRender() {
				public Object receiveCellEditor(int row, int col) {
					if (row >= headRow) {
						if (col == bidResultCol) {
							return radio;
						}
					}
					return null;
				}
			});
			
			// 编辑单元格内容
			tableModel.setCellEditContentRender(new CellEditContentRender() {
				public void receiveContent(int row, int col, Object content) {
					if (null == content) {
						return;
					}
					if (row < headRow) {
						return;
					}
					if (col == bidResultCol) {
						Supplier supplier = supplierList.get(row - headRow);
						ExpertEvaluationResult evalResult = evalResultMap.get(supplier.getId());
						if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(content)) {
//							EvaluationOpinionDialog dialog = new EvaluationOpinionDialog(getShell(), tenderProject, supplier, Constants.USER, evalResult,new CallBack() {
//								public void callBackBoolean(final boolean isEmpyt) {
//									if(isEmpyt) {
//										refreshTableComposite();
//									}else {
//										evalResult.setAccessment(content.toString());
//									}
//								}
//							});
							EvaluationOpinionDialog dialog = new EvaluationOpinionDialog(getShell(), tenderProject, supplier, Constants.USER, evalResult);
							dialog.open();
						}
						//evalResult.setAccessment(content.toString());
					}
				}
			});
		}
		
		// 设置单元格前景颜色
		tableModel.setCellForegroundRender(new CellForegroundRender() {
			public Color receiveCellForeground(int row, int col) {
				if (row >= headRow) {
					if (col == bidResultCol) {
						Supplier supplier = supplierList.get(row - headRow);
						ExpertEvaluationResult evalResult = evalResultMap.get(supplier.getId());
						if (null != evalResult) {
							if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(evalResult.getAccessment())) {
								return SWTResourceManager.getColor(SWT.COLOR_RED);
							} else if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED.equals(evalResult.getAccessment())) {
								return SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
							}
						}
					}
				}
				return null;
			}
		});
		
		resultTable.setModel(tableModel);
		resultTable.refresh();
		
		layout();
	}
	
	protected void checkSubclass() {}
	
}
