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

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.listener.TextHintListener;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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 org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.dialog.RecommendRankSupplierSelectedDialog;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.evaluation.step.inter.ISupplierRecommoned;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.NumericChineseUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class RecommendRankEvaluationTableTdComposite extends Composite implements ISupplierRecommoned{

	private CLabel titleLabel;
	private CLabel contentLabel;
	private int sortNo = 0;
	private Supplier curSupplier;
	private TenderProjectEntity tenderProjectEntity;
	private List<Supplier> selectSupplierList;
	private String supplierName = "";
	private ELink selectLink;
	private ELink deleteLink;
	private Text supplierOpinionText;
	private Composite valueComposite;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public RecommendRankEvaluationTableTdComposite(Composite parent, int style) {
		super(parent, style);
		setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing = 1;
		gridLayout.horizontalSpacing = 1;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite titleComposite = new Composite(this, SWT.NONE);
		titleComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		titleComposite.setLayout(new GridLayout(1, false));
		GridData gd_titleComposite = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_titleComposite.widthHint = 200;
		titleComposite.setLayoutData(gd_titleComposite);
		
		titleLabel = new CLabel(titleComposite, SWT.NONE);
		titleLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		titleLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		titleLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		titleLabel.setText("第一名");
		
		valueComposite = new Composite(this, SWT.NONE);
		valueComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		valueComposite.setLayout(new GridLayout(3, false));
		GridData gd_valueComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_valueComposite.widthHint = 500;
		valueComposite.setLayoutData(gd_valueComposite);
		
		contentLabel = new CLabel(valueComposite, SWT.NONE);
		contentLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		contentLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		contentLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		contentLabel.setText("                                 ");
		
		selectLink = new ELink(valueComposite, SWT.NONE);
		selectLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/query.png"));
		selectLink.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		selectLink.setText("选择");
		selectLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				selectAction();
			}
		});
		
		deleteLink = new ELink(valueComposite, SWT.NONE);
		deleteLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		deleteLink.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		deleteLink.setText(Messages.Button_delete);
		
		supplierOpinionText = new Text(valueComposite, SWT.BORDER | SWT.MULTI | SWT.WRAP);
		supplierOpinionText.addPaintListener(new TextHintListener("请输入评审意见（500字以内）"));
		GridData gd_text = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
		gd_text.heightHint = 40;
		supplierOpinionText.setTextLimit(500);
		supplierOpinionText.setLayoutData(gd_text);
		deleteLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				deleteAction();
			}
		});
	}
	
	/**
	 * @Description: 生成表头
	 * @author chenxw
	 * @param tenderProjectEntity 
	 */
	public void addTitle(String title, String valueTitle) {
		titleLabel.setText(null != title ? title : Messages.SortNo);
		for(Control control : valueComposite.getChildren()){
			control.dispose();
		}
		CLabel valueLabel = new CLabel(valueComposite, SWT.NONE);
		valueLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		valueLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		valueLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		valueLabel.setText(null != valueTitle ? valueTitle : "供应商");
		layout();
	}
	
	/**
	 * @Description: 生成表体
	 * @author chenxw
	 * @param tenderProjectEntity
	 * @param selectSupplierList
	 * @param sortNo
	 * @param supplier 
	 */
	public void addParameter(TenderProjectEntity tenderProjectEntity, List<Supplier> selectSupplierList, int sortNo, Supplier supplier, String supplierOpinion) {
		this.tenderProjectEntity = tenderProjectEntity;
		this.selectSupplierList = selectSupplierList;
		this.sortNo = sortNo;
		this.curSupplier = supplier;
		supplierOpinionText.setText(null != supplierOpinion ? supplierOpinion : "");
		init();
	}
	
	/**
	 * @Description: 表体初始化
	 * @author chenxw 
	 */
	private void init() {
		if (null != tenderProjectEntity) {
			supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		}
		if (sortNo != 0) {
			titleLabel.setText("第" + NumericChineseUtils.getChinese(sortNo) + "名");
		}
		if (null == curSupplier) {
			contentLabel.setText(Messages.Tips_select + supplierName);
			contentLabel.setForeground(SWTResourceManager.getColor(220, 220, 220));
			supplierOpinionText.setText("");
			supplierOpinionText.setEnabled(false);
		} else {
			contentLabel.setText(curSupplier.getSupplierName());
			contentLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
			selectSupplierList.remove(curSupplier);
			supplierOpinionText.setEnabled(true);
		}
		valueComposite.layout();
	}

	/**
	 * @Description: 选择供应商事件
	 * @author chenxw 
	 */
	protected void selectAction() {
		RecommendRankSupplierSelectedDialog dialog = new RecommendRankSupplierSelectedDialog(getShell(), selectSupplierList, supplierName);
		dialog.open();
		Supplier supplier = dialog.getSupplier();
		if (null == supplier) {
			return;
		}
		if (null != curSupplier) {
			selectSupplierList.add(curSupplier);
		}
		curSupplier = supplier;
		selectSupplierList.remove(curSupplier);
		contentLabel.setText(curSupplier.getSupplierName());
		contentLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		supplierOpinionText.setEnabled(true);
		valueComposite.layout();
	}

	/**
	 * @Description: 删除供应商事件
	 * @author chenxw 
	 */
	protected void deleteAction() {
		contentLabel.setText(Messages.Tips_select + supplierName);
		contentLabel.setForeground(SWTResourceManager.getColor(220, 220, 220));
		supplierOpinionText.setText("");
		supplierOpinionText.setEnabled(false);
		valueComposite.layout();
		if (null != curSupplier) {
			selectSupplierList.add(curSupplier);
		}
		curSupplier = null;
	}

	/** 
	 * @Description: 获得排名
	 * @author chenxw
	 * @return 
	 */
	public int getSupplierSortNo() {
		return sortNo;
	}
	
	/** 
	 * @Description: 获得供应商
	 * @author chenxw
	 * @return 
	 */
	public Supplier getSupplier() {
		return curSupplier;
	}
	public String getSupplierOpinion(){
		if(null != curSupplier){
			return supplierOpinionText.getText().trim();
		}
		return null;
	}
	@Override
	protected void checkSubclass() {
	}

}
