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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.thread.MyRunnable;
import com.gx.obe.components.core.vo.Qualification;
import com.gx.obe.evaluation.step.inter.IStruct;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.Struct;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.listener.WebBoolearListener;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.award.AwardCertificateService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 行业排名
 * @author chenxw 
 */
public class QualificationHYPMComposite extends Composite implements IStruct {
	private Combo certificateNameCombo;
	private Label companyNameLabel;
	private Label issueAuthorityLabel;
	private Label issueDateLabel;
	private Qualification qualification;
	private CLabel isRosterAmongLabel;
	private Struct struct;
	private Supplier curSupplier;
	
	private SupplierService supplierService = new SupplierService();
	
	private AwardCertificateService awardCertificateService ;
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public QualificationHYPMComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(2, false));
		
		Label label = new Label(this, SWT.NONE);
		label.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("排名标题：");
		
		certificateNameCombo = new Combo(this, SWT.READ_ONLY);
		certificateNameCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				certificateNameSelectionAction();
			}
		});
		GridData gd_certificateNameCombo = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_certificateNameCombo.widthHint = 200;
		certificateNameCombo.setLayoutData(gd_certificateNameCombo);
		
		Label label_5 = new Label(this, SWT.NONE);
		label_5.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_5.setText("企业名称：");
		
		companyNameLabel = new Label(this, SWT.NONE);
		companyNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(this, SWT.NONE);
		label_2.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_2.setText("评选机构：");
		
		issueAuthorityLabel = new Label(this, SWT.NONE);
		issueAuthorityLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_3 = new Label(this, SWT.NONE);
		label_3.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_3.setText("评选年度：");
		
		issueDateLabel = new Label(this, SWT.NONE);
		issueDateLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		isRosterAmongLabel = new CLabel(this, SWT.NONE);
		isRosterAmongLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/tips_small.png"));
		isRosterAmongLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.BOLD));
		isRosterAmongLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		isRosterAmongLabel.setToolTipText("名单是指内蒙电力物资部收录的各奖项及区内企业名单；" +
				"\r\n系统根据名单进行对比及给出提示，" +
				"\r\n但最终仍需由专家根据投标文件的响应情况进行判断并作出结论。");
	}
	
	/** 
	 * @Description: 刷新
	 * @author chenxw
	 * @param struct 
	 */
	public void refresh(Struct struct) {
		this.struct = struct;
		qualification = struct.getQualification();
		
		String[] comboContent = new String[] { "《财富》世界企业500强", "中国企业500强", "中国制造业企业500强" };
		for (int i = 0; i < comboContent.length; i++) {
			certificateNameCombo.add(comboContent[i], i);
			certificateNameCombo.setData(i + "", comboContent[i]);
		}
		WidgetUtils.setComboSelection(certificateNameCombo, qualification.getCertificateName());
		
		companyNameLabel.setText(ObjectUtils.getObjString(qualification.getCompanyName()));
		issueAuthorityLabel.setText(ObjectUtils.getObjString(qualification.getIssueAuthority()));
		issueDateLabel.setText(qualification.getIssueDate());
		
		curSupplier = supplierService.getById(struct.getSupplierId());
		certificateNameSelectionAction();
	}
	
	private void certificateNameSelectionAction() {
		if( null == awardCertificateService){
			awardCertificateService = new AwardCertificateService();
		}
		final String strSupplierName = curSupplier.getSupplierName();
		final String strCertificateName = certificateNameCombo.getText();
		AwardCertificateListThread  awardCertificateListThread = new AwardCertificateListThread(strCertificateName,new WebBoolearListener() {
			@Override
			public void run(boolean ishasAwardCertificateList) {
				if (ishasAwardCertificateList) {
					final boolean hasSupplier = awardCertificateService.isHasSupplier(strSupplierName, strCertificateName);
					Display.getDefault().syncExec(new Runnable(){
						public void run(){
							isRosterAmongLabel.setText("该企业" + (!hasSupplier ? "未" : "") + "在内蒙电力物资部收录的名单中");
							if (hasSupplier) {
								isRosterAmongLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
							} else {
								isRosterAmongLabel.setForeground(SWTResourceManager.getColor(250, 207, 0));
							}
						} 
					});
				} else {
					Display.getDefault().syncExec(new Runnable(){
						public void run(){
							((GridData)isRosterAmongLabel.getLayoutData()).exclude = true;							
						} 
					});
				}
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						isRosterAmongLabel.getParent().layout();
					} 
				});
			}
		});
		awardCertificateListThread.start();
	}

	class AwardCertificateListThread extends MyRunnable{
		private WebBoolearListener listener;
		private String strCertificateName;
		public AwardCertificateListThread(String strCertificateName ,WebBoolearListener listener){
			this.listener = listener;
			this.strCertificateName = strCertificateName;
		}
		@Override
		public void run() {
			boolean ishasAwardCertificateList = awardCertificateService.getAwardCertificateList(strCertificateName).size() > 0;
 			listener.run(ishasAwardCertificateList);
		}
	}
	/** 
	 * @Description: 设置是否编辑
	 * @author chenxw
	 * @param isEdit 
	 */
	public void setEdit(boolean isEdit) {
		certificateNameCombo.setEnabled(isEdit);
	}
	
	/** 
	 * @Description: 刷新编辑数据
	 * @author chenxw 
	 */
	public void refreshEditData() {
		qualification.setCertificateName(certificateNameCombo.getText());
		struct.setQualification(qualification);
	}
	
	/** 
	 * @Description: 获取标题名称
	 * @author chenxw
	 * @return 
	 */
	public String getTopName() {
		return ObjectUtils.getObjString(qualification.getCertificateName());
	}
	
	protected void checkSubclass() {}
	
}
