package com.gx.obe.management.composite;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.utils.FileDialogUtils;
import org.eclipse.swt.core.widgets.ELabel;
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.Text;

import com.gx.obe.common.file.utils.ParsingXmlUtils;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 选中本地指标
 * @author guoyr
 */
public class SelectLocalFactorFileComposite extends Composite {
	
	private Text messageLabel;
	private TenderProjectEntity tenderProjectEntity;
	private CallBack callBack;
	private Logger LOG = Logger.getLogger(SelectLocalFactorFileComposite.class);
	
	public void setTenderProject(TenderProjectEntity tenderProjectEntity) {
		this.tenderProjectEntity = tenderProjectEntity;
	}
	
	public void setCallBack(CallBack callBack) {
		this.callBack = callBack;
	}

	{
		LogUtils.logClass(SelectLocalFactorFileComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public SelectLocalFactorFileComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite = new GridLayout(1, false);
		composite.setLayout(gl_composite);
		
		ELabel selectEvalFactorBtn = new ELabel(composite, SWT.NONE | SWT.SELECTED | SWT.BOTTOM);
		selectEvalFactorBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectEvalFactorAction();
			}
		});
		selectEvalFactorBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/book-open_green.png"));
		selectEvalFactorBtn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		selectEvalFactorBtn.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		selectEvalFactorBtn.setText("选择指标");
		
		messageLabel = new Text(composite, SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_label = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_label.heightHint = 60;
		messageLabel.setLayoutData(gd_label);
		messageLabel.setText("         ");
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		messageLabel.setText("");
	}
	
	/**
	 * @Description: 选择指标文件
	 * @author guoyr
	 */
	private void selectEvalFactorAction(){
		messageLabel.setText("");
		String fileName = FileDialogUtils.getFileNames(SWT.SINGLE, "请选择指标文件", new String[]{"*.xml"}, new String[]{"所有投标文件(*.xml)"});
		
		if(StringUtils.isNotEmpty(fileName)){
			Document factorDoc = XmlUtils.getXmlDocument(fileName);
			List<EvaluationFactor> evalFactorList = null;
			try {
				evalFactorList = ParsingXmlUtils.parsingEvaluationFactorXML(factorDoc, tenderProjectEntity);
			} catch (Exception e) {
				LOG.error("解析指标文件失败", e);
				callBackMsg(false, "解析指标文件失败！");
			}
			if (null != evalFactorList) {
				if (evalFactorList.size() > 0) {
					List<EvaluationFactor> finalFactorList = initEvaluationFactorList(evalFactorList);
					if(null != callBack){
						callBack.callBackObject(finalFactorList);
						callBackMsg(true, "共读取"+evalFactorList.size()+"个评审指标，读取结束！");
					}
				}else {
					callBackMsg(false, "未检测到评审指标项！");
				}
			}
		}
	}
	
	/**
	 * @Description: 将下载过来的指标按要求组装成树型结构
	 * @author guoyr
	 * @param factorList
	 * @return
	 */
	private List<EvaluationFactor> initEvaluationFactorList(List<EvaluationFactor> factorList){
		// 先组织成上下级树型结构
		List<EvaluationFactor> tempEvalFactorList = new ArrayList<EvaluationFactor>();
		if(null != factorList && factorList.size() > 0){
			for(EvaluationFactor parentFactor : factorList){
				if(StringUtils.isEmpty(parentFactor.getParentId())){
					tempEvalFactorList.add(parentFactor);
				}
			}
		}
		return tempEvalFactorList;
	}
	
	private void callBackMsg(boolean bool, String msg){
		if(null != messageLabel && !messageLabel.isDisposed()){
			messageLabel.setText(messageLabel.getText()+"\r\n "+((bool?"√":"×")+ msg));
			messageLabel.setSelection(messageLabel.getText().length());
		}
	}
	
	protected void checkSubclass() {
		
	}
}
