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

import java.text.MessageFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.evaluation.step.dialog.EvaluationOpinionDialog;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.EvaluationFactorResult;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.TableTitle;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.editors.KTableCellEditorGRadio;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellBackgroundRender;
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.CellRender;
import de.kupzog.ktable.renderers.FixedWrapCellRenderer;
import de.kupzog.ktable.renderers.TextWrapCellRenderer;

public class AiEvaluationComplianceComposite extends EvaluationSupplierComposite {
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AiEvaluationComplianceComposite(Composite parent, int style) {
		super(parent, style);
	}
	
	@Override
	public void initTableTitleList() {}
	
	@Override
	public void drawTable() {
		super.drawTable();
		
		// 设置单元格内容
		tableCModel.setCellContentRender(new CellContentRender() {
			public Object receiveContent(int row, int col) {
				if (row < tableCModel.getFixedRowCount()) {
					if (col == supplierNameCol) return MessageFormat.format(Messages.NameFormat, listener.getSupplierName());
					if (col == evaluationResultCol) return "评审结果";
					TableTitle tableTitle = tableTitleList.get(col - tableTitleStactCol);
					if (null != tableTitle) return tableTitle.getName();
				} else {
					Supplier supplier = supplierList.get(row - tableCModel.getFixedRowCount());
					if (col == supplierNameCol) return supplier.getSupplierName();
					if (col == evaluationResultCol) return factorResultMap.get(supplier.getId()).getEvalPassTypeCN(curFactor.getEvalContent());
					TableTitle tableTitle = tableTitleList.get(col - tableTitleStactCol);
					if (null != tableTitle) return tableTitle.getValue(structListMap.get(supplier.getId()));
				}
				return "";
			}
		});
		
		// 设置单元格渲染器
		final FixedWrapCellRenderer fixedWrapCellRenderer = new FixedWrapCellRenderer();
		final TextWrapCellRenderer textWrapCellRenderer = new TextWrapCellRenderer();
		tableCModel.setCellRender(new CellRender() {
			public Object receiveCellRender(int row, int col) {
				if (tableCModel.isFixedCell(col, row)) return fixedWrapCellRenderer;
				return textWrapCellRenderer;
			}
		});
		
		// 设置单元格编辑器
		final KTableCellEditorGRadio radio = new KTableCellEditorGRadio();
		radio.setSingleClickEdit(true);
		radio.addItem(EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.CONFORM), EvaluationFactorEnum.CONFORM);
		radio.addItem(EvaluationFactorEnum.getEvalPassTypeCN(EvaluationFactorEnum.UNCONFORM), EvaluationFactorEnum.UNCONFORM);
		String evalContent = curFactor.getEvalContent();
		if (StringUtils.isNotEmpty(evalContent)) {
			String[] evalContents = evalContent.split(",");
			if (evalContents.length == 2) {
				radio.setItem(0, evalContents[0], EvaluationFactorEnum.CONFORM);
				radio.setItem(1, evalContents[1], EvaluationFactorEnum.UNCONFORM);
			}
		}
		tableCModel.setCellEditorRender(new CellEditorRender() {
			public Object receiveCellEditor(int row, int col) {
				if (row < tableCModel.getFixedRowCount()) return null;
				if (col != evaluationResultCol) return null;
				if (CollectionUtils.isNull(structListMap.get(supplierList.get(row - tableCModel.getFixedRowCount()).getId()))) return radio;
				if (dataParams.isEditEvaluationResult(curFactor.getDataType())) return radio;
				return null;
			}
		});
		
		// 编辑单元格内容
		tableCModel.setCellEditContentRender(new CellEditContentRender() {
			
			public void receiveContent(int row, int col, Object content) {
				if (null == content) return;
				if (row < tableCModel.getFixedRowCount()) return;
				if (col != evaluationResultCol) return;
				Supplier supplier = supplierList.get(row - tableCModel.getFixedRowCount());
				EvaluationFactorResult evaluationFactorResult = factorResultMap.get(supplier.getId());
				evaluationFactorResult.setEvaluationResult(content.toString());
				
				ExpertEvaluationResult expertEvaluationResult = listener.getResultSupplierMap().get(supplier.getId());
				if (EvaluationFactorEnum.UNCONFORM.equals(evaluationFactorResult.getEvaluationResult())) {
					EvaluationOpinionDialog dialog = new EvaluationOpinionDialog(getShell(), listener.getCurTender(), supplier, Constants.USER, expertEvaluationResult);
					dialog.open();
				}
				listener.refreshFactorGridImage();
			}
		});
		
		// 编辑单元格对齐方式
		tableCModel.setCellAlignmentRender(new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (row < tableCModel.getFixedRowCount()) return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
				if (col == supplierNameCol) return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
				return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
			}
		});
		
		// 设置单元格前景颜色
		tableCModel.setCellForegroundRender(new CellForegroundRender() {
			public Color receiveCellForeground(int row, int col) {
				if (row < tableCModel.getFixedRowCount()) return null;
				if (col != evaluationResultCol) return null;
				Supplier supplier = supplierList.get(row - tableCModel.getFixedRowCount());
				EvaluationFactorResult evaluationFactorResult = factorResultMap.get(supplier.getId());
				if (EvaluationFactorEnum.CONFORM.equals(evaluationFactorResult.getEvaluationResult())) return SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN);
				if (EvaluationFactorEnum.UNCONFORM.equals(evaluationFactorResult.getEvaluationResult())) return SWTResourceManager.getColor(SWT.COLOR_RED);
				return null;
			}
		});
		
		// 设置单元格背景颜色
		tableCModel.setCellBackgroundRender(new CellBackgroundRender() {
			public Color receiveCellBackground(int row, int col) {
				return SWTResourceManager.getColor(SWT.COLOR_WHITE);
			}
		});
		
		tablcComposite.layout();
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
}
