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

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.business.entity.ScoreItem;
import com.gx.obe.business.enumeration.StructEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.SerializeUtils;
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.TableTitle.TitleNo;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.editors.KTableCellEditorGCombo;
import de.kupzog.ktable.editors.KTableCellEditorText;
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.CellImageRender;
import de.kupzog.ktable.renderers.CellRender;
import de.kupzog.ktable.renderers.FixedWrapCellRenderer;
import de.kupzog.ktable.renderers.TextWrapCellRenderer;

/** 
 * @Description: 智能评审得分
 * @author chenxw 
 */
public class AiEvaluationScoreComposite extends EvaluationSupplierComposite {
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AiEvaluationScoreComposite(Composite parent, int style) {
		super(parent, style);
	}
	
	@Override
	protected void initTableTitleList() {
		super.initTableTitleList();
		
		String dataType = curFactor.getDataType();
		if (null == dataType) return;
		
		if (dataType.equals(StructEnum.EQ_ENTERPRISE_INFO)) {

		} else if (dataType.equals(StructEnum.EQ_BUSINESS_LICENSE)) {
			initBusinessLicenseTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_FINANCIAL_REPORT)) {
			initFinancialReportTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_ACHIEVEMENTS)) {
			initAchievementsTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION)) {
			initQualificationTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_ZXDJ)) {
			initQualificationZXDJTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_ZLJX)) {
			initQualificationZLJXTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_KJYF)) {
			initQualificationKJYFTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_HYPM)) {
			initQualificationHYPMTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_KJYF_SJ)) {
			initZZQZLJTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_ZLJX_SJ)) {
			initJZQKJJTableTitleList();
		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_XJS)) {

		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_SCXK)) {

		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_CPRZ)) {

		} else if (dataType.equals(StructEnum.EQ_QUALIFICATION_HBTX)) {

		}
	}
	
	@Override
	protected 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 NumberFormatUtils.format(factorResultMap.get(supplier.getId()).getEvaluationScore(), listener.getScoreRound());
					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 KTableCellEditorText text = new KTableCellEditorText();
		text.setSingleClickEdit(true);
		tableCModel.setCellEditorRender(new CellEditorRender() {
			public Object receiveCellEditor(int row, int col) {
				if (row < tableCModel.getFixedRowCount()) return null;
				if (col != evaluationResultCol) return null;
				if (isEditEvaluationResult) return text;
				if (CollectionUtils.isNotNull(structListMap.get(supplierList.get(row - tableCModel.getFixedRowCount()).getId()))) return null;
				List<ScoreItem> itemList = SerializeUtils.deserializeObject(curFactor.getObjectiveItem());
				if (CollectionUtils.isNull(itemList)) return text;
				return getKTableCellEditorGCombo(itemList);
			}
		});
		
		// 设置单元格图片
		final Image editImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/edit.png");
		tableCModel.setCellImageRender(new CellImageRender() {
			public Image receiveCellImage(int row, int col) {
				if (row < tableCModel.getFixedRowCount()) return null;
				if (col != evaluationResultCol) return null;
				if (isEditEvaluationResult) return editImage;
				if (CollectionUtils.isNotNull(structListMap.get(supplierList.get(row - tableCModel.getFixedRowCount()).getId()))) return null;
				List<ScoreItem> itemList = SerializeUtils.deserializeObject(curFactor.getObjectiveItem());
				if (CollectionUtils.isNotNull(itemList)) return null;
				return editImage;
			}
		});
		
		// 编辑单元格内容
		tableCModel.setCellEditContentRender(new CellEditContentRender() {
			private boolean isShowError = true;
			
			public void receiveContent(int row, int col, Object content) {
				if (row < tableCModel.getFixedRowCount()) return;
				if (col != evaluationResultCol) return;
				BigDecimal evaluationScore = ObjectUtils.getObjBigDecimal(content);
				if (null == evaluationScore) return;
				if (evaluationScore.compareTo(curFactor.getFactorMinScore()) < 0 || evaluationScore.compareTo(curFactor.getFactorMaxScore()) > 0) {
					if (isShowError) {
						isShowError = false;
						MessageDialog.openError(getShell(), MessageFormat.format(Messages.Tips_entryScroeRange, curFactor.getFactorMinScore(), curFactor.getFactorMaxScore()));
						isShowError = true;
					}
					return;
				}
				EvaluationFactorResult evaluationFactorResult = factorResultMap.get(supplierList.get(row - tableCModel.getFixedRowCount()).getId());
				evaluationFactorResult.setEvaluationScore(evaluationScore);
				evaluationFactorResult.setEvaluationFinalScore(getEvaluationFinalScore(curFactor, evaluationScore));
				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) {
				return null;
			}
		});
		
		// 设置单元格背景颜色
		tableCModel.setCellBackgroundRender(new CellBackgroundRender() {
			public Color receiveCellBackground(int row, int col) {
				return SWTResourceManager.getColor(SWT.COLOR_WHITE);
			}
		});
		
		tablcComposite.layout();
	}
	
	@Override
	public void refreshSupplierResult(String supplierId) {
		super.refreshSupplierResult(supplierId);
		if (dataParams.isEditEvaluationResult(curFactor.getDataType())) return;
		BigDecimal evaluationScore = dataParams.getScore(curFactor.getDataType(), structListMap.get(supplierId));
		EvaluationFactorResult evalFactorResult = factorResultMap.get(supplierId);
		evalFactorResult.setEvaluationScore(evaluationScore);
		evalFactorResult.setEvaluationFinalScore(getEvaluationFinalScore(curFactor, evaluationScore));
		
		for (int i = 0; i < supplierList.size(); i++)
			if (supplierList.get(i).getId().equals(supplierId)) table.redraw(evaluationResultCol, i + 1, 1, 1);
	}
	
	/**
	 * @Description: 获取表格编辑控件
	 * @author chenxw
	 * @param itemList
	 * @return
	 */
	private KTableCellEditorGCombo getKTableCellEditorGCombo(List<ScoreItem> itemList) {
		KTableCellEditorGCombo combo = new KTableCellEditorGCombo();
		combo.setSingleClickEdit(true);
		for (ScoreItem item : itemList) {
			BigDecimal score = item.getScore();
			String name = item.getName();
			combo.addItem(score + "分" + (StringUtils.isNotEmpty(name) ? " " + name : ""), score);
		}
		return combo;
	}
	
	/**
	 * @Description: 得到最终得分
	 * @author guoyr
	 * @param evaluationFactor
	 * @param evaluationScore
	 * @return
	 */
	private BigDecimal getEvaluationFinalScore(EvaluationFactor evaluationFactor, BigDecimal evaluationScore) {
		BigDecimal evaluationFinalScore = evaluationScore;
		if (null != evaluationScore && null != evaluationFactor.getFactorFinalWeight()) {
			evaluationFinalScore = BigDecimalUtils.div(BigDecimalUtils.mul(evaluationFactor.getFactorFinalWeight(), evaluationScore), new BigDecimal(100));
		}
		return BigDecimalUtils.round(evaluationFinalScore, listener.getScoreRound());
	}
	
	/**
	 * @Description: 营业执照
	 * @author chenxw
	 */
	private void initBusinessLicenseTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("是否区内企业");
			tableTitle1.setTitleNo(TitleNo.isInAreaEnterprises);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 财务报表
	 * @author chenxw
	 */
	private void initFinancialReportTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("实收资本超额率");
			tableTitle1.setTitleNo(TitleNo.capitalStock);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("主营业务利润率");
			tableTitle1.setTitleNo(TitleNo.profitRate);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("资产负债率");
			tableTitle1.setTitleNo(TitleNo.debtRatio);
			tableTitle1.setWidth(150);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 同业业绩
	 * @author guoyr
	 */
	private void initAchievementsTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("业绩数量");
			tableTitle1.setTitleNo(TitleNo.goodsQuantity);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("超基础业绩百分比");
			tableTitle1.setTitleNo(TitleNo.ratio);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 其他资质
	 * @author guoyr
	 */
	private void initQualificationTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("证书名称");
			tableTitle1.setTitleNo(TitleNo.name);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 资信等级
	 * @author chenxw
	 */
	private void initQualificationZXDJTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("信用等级");
			tableTitle1.setTitleNo(TitleNo.creditRating);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 质量奖项
	 * @author chenxw
	 */
	private void initQualificationZLJXTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("质量奖项");
			tableTitle1.setTitleNo(TitleNo.name);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 科技研发
	 * @author chenxw
	 */
	private void initQualificationKJYFTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("科技研发奖项");
			tableTitle1.setTitleNo(TitleNo.name);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 自治区质量奖
	 * @author chenxw
	 */
	private void initZZQZLJTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("自治区质量奖");
			tableTitle1.setTitleNo(TitleNo.name);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 自治区科技奖
	 * @author chenxw
	 */
	private void initJZQKJJTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("自治区科技奖");
			tableTitle1.setTitleNo(TitleNo.name);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	/**
	 * @Description: 行业排名
	 * @author chenxw
	 */
	private void initQualificationHYPMTableTitleList() {
		{
			TableTitle tableTitle1 = new TableTitle();
			tableTitle1.setName("行业排名");
			tableTitle1.setTitleNo(TitleNo.name);
			tableTitle1.setWidth(100);
			dataParams.addTableTitle(tableTitle1);
		}
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
}
