package com.gx.obe.management.dialog;

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
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.Label;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.composite.SelectTenderProjectComposite;
import com.gx.obe.management.composite.EvaluationFactorTreeComposite;
import com.gx.obe.management.composite.SelectLocalFactorFileComposite;
import com.gx.obe.message.Messages;
import com.gx.obe.thread.CreateEvaluationStepThread;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class ImportFactorFromTenderProjectDialog extends EDialog {
	
	private Composite composite;
	private Composite contentComposite;
	private TenderProjectEntity tenderProjectEntity;
	private Label messageLabel;
	private EButton importBtn;
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private List<EvaluationFactor> evaluationFactorList = new ArrayList<EvaluationFactor>();
	private CallBack callBack;
	private CTabFolder tabFolder;
	private EvaluationFactorTreeComposite factorTree;
	private ImageResolver imageResolver = new ImageResolver();
	
	{
		LogUtils.logClass(ImportFactorFromTenderProjectDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public ImportFactorFromTenderProjectDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity) {
		super(parentShell);
		setTitle("导入指标");
		this.tenderProjectEntity = tenderProjectEntity;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		container.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		Composite composite_1 = new Composite(container, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.horizontalSpacing = 0;
		gl_composite_1.marginHeight = 0;
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);
		
		contentComposite = new Composite(composite_1, SWT.NONE);
		GridLayout gl_contentComposite = new GridLayout(3, false);
		gl_contentComposite.verticalSpacing = 0;
		gl_contentComposite.horizontalSpacing = 0;
		gl_contentComposite.marginHeight = 0;
		gl_contentComposite.marginWidth = 0;
		contentComposite.setLayout(gl_contentComposite);
		contentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		
		tabFolder = new CTabFolder(contentComposite, SWT.FLAT);
		GridData gd_tabFolder = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_tabFolder.widthHint = 200;
		tabFolder.setLayoutData(gd_tabFolder);
		tabFolder.setSelectionBackground(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		tabFolder.setUnselectedImageVisible(true);
		
		CTabItem tabItem_1 = new CTabItem(tabFolder, SWT.NONE);
		tabItem_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/drive_user.png"));
		tabItem_1.setText("本地导入");
		
		Composite composite_2 = new Composite(tabFolder, SWT.NONE);
		tabItem_1.setControl(composite_2);
		composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
		tabItem.setText("项目库导入");
		tabItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/archives.png"));
		composite = new Composite(tabFolder, SWT.NONE);
		tabItem.setControl(composite);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		composite.setData("tenderProjectEntity", tenderProjectEntity);
		
		ESepator label = new ESepator(contentComposite, SWT.SEPARATOR | SWT.VERTICAL);
		label.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		label.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		factorTree = new EvaluationFactorTreeComposite(contentComposite, SWT.NONE);
		GridData gd_factorTree = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_factorTree.widthHint = 300;
		factorTree.setLayoutData(gd_factorTree);
		
		ESepator label_1 = new ESepator(composite_1, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_1.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite operateComposite = new Composite(composite_1, SWT.NONE);
		operateComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		operateComposite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		operateComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		GridLayout gl_operateComposite = new GridLayout(3, false);
		operateComposite.setLayout(gl_operateComposite);
		
		messageLabel = new Label(operateComposite, SWT.WRAP);
		messageLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		messageLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		messageLabel.setText(" ");
		
		importBtn = new EButton(operateComposite, SWT.NONE);
		importBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				importFactorAction();
			}
		});
		GridData gd_importBtn = new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1);
		importBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/import.png"));
		importBtn.setLayoutData(gd_importBtn);
		importBtn.setText(Messages.Button_import);
		
		EButton close_btn = new EButton(operateComposite, SWT.NONE);
		close_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		close_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				getShell().close();
			}
		});
		GridData gd_button_1 = new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1);
		close_btn.setLayoutData(gd_button_1);
		close_btn.setText(Messages.Button_close);

		init();
		return container;
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		if(null == tenderProjectEntity){
			return;
		}
		tabFolder.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectTabItemAction();
			}
		});
		tabFolder.setSelection(0);
		selectTabItemAction();
		messageLabel.setText("为["+tenderProjectEntity.getTenderNo()+"]"+tenderProjectEntity.getTenderName()+"导入指标");
	}

	/**
	 * @Description: 选中tab标签页事件
	 * @author guoyr
	 */
	
	private void selectTabItemAction(){
		evaluationFactorList.clear();
		final CTabItem selectTabItem = tabFolder.getSelection(); 
		Control tabItemContent = selectTabItem.getControl();
		Composite parentComposite = null;
		if(null != tabItemContent && !tabItemContent.isDisposed()){
			parentComposite = (Composite)tabItemContent;
		}else {
			parentComposite = new Composite(tabFolder, SWT.NONE);
			parentComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
			selectTabItem.setControl(parentComposite);
		}
		parentComposite.setData("tenderProjectEntity", tenderProjectEntity);
		
		if(tabFolder.getSelectionIndex() == 0){// 本地指标
			if(null == selectTabItem.getData("load")){
				SelectLocalFactorFileComposite selectLocalFactorFileComposite = new SelectLocalFactorFileComposite(parentComposite, SWT.NONE);
				selectLocalFactorFileComposite.setTenderProject(tenderProjectEntity);
				selectLocalFactorFileComposite.setCallBack(new CallBack(){
					@SuppressWarnings("unchecked")
					public void callBackObject(Object obj) {
						evaluationFactorList = (List<EvaluationFactor>) obj;
						refreshFactorTree(null, false);
					}
				});
			}else {
				refreshFactorTree(null, false);
			}
		}else if(tabFolder.getSelectionIndex() == 1){// 项目库
			if(null == selectTabItem.getData("load")){
				SelectTenderProjectComposite selectTenderProjectComposite = new SelectTenderProjectComposite(parentComposite, SWT.NONE);
				selectTenderProjectComposite.setCallBack(new CallBack(){
					@Override
					public void callBackObject(Object obj) {
						TenderProjectEntity selectTenderProject = (TenderProjectEntity)obj;
						String tenderId = null;
						if(null != selectTenderProject){
							tenderId = selectTenderProject.getId();
							selectTabItem.setData("tenderId", tenderId);
						}
						refreshFactorTree(tenderId, true);
					}
				});
			}else {
				refreshFactorTree(ObjectUtils.getObjString(selectTabItem.getData("tenderId")), true);
			}
		}
		
		selectTabItem.setData("load", "load");
		parentComposite.layout();
	}
	
	private void refreshFactorTree(String id, boolean isValue){
		if(isValue){
			evaluationFactorList = getEvalFactorList(id);
		}
		int factorCount = factorTree.refreshFactorTree(evaluationFactorList);
		if(factorCount > 0){
			importBtn.setEnabled(true);
		}else {
			importBtn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 获得全部指标(符合性评审指标、打分评审指标和价格指标)
	 * @author guoyr
	 * @param tenderId
	 * @param isFactorCategory
	 * @return
	 */
	private List<EvaluationFactor> getEvalFactorList(String tenderId){
		List<EvaluationFactor> evalFactorList = new ArrayList<EvaluationFactor>();
		// 符合性评审指标集合
		List<EvaluationFactor> complianceFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderId, EvaluationFactorEnum.AUDIT_COMPLIANCE);
		if(null != complianceFactorList && complianceFactorList.size() > 0){
			evalFactorList.addAll(complianceFactorList);
		}
		// 打分评审指标集合
		List<EvaluationFactor> detailFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderId, EvaluationFactorEnum.AUDIT_SCORE);
		if(null != detailFactorList && detailFactorList.size() > 0){
			evalFactorList.addAll(detailFactorList);
		}
		// 信用评审指标集合
		List<EvaluationFactor> creditFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderId, EvaluationFactorEnum.AUDIT_CREDIT);
		if(null != creditFactorList && creditFactorList.size() > 0){
			evalFactorList.addAll(creditFactorList);
		}
		// 价格指标
		List<EvaluationFactor> priceFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderId, EvaluationFactorEnum.AUDIT_PRICE);
		if(null != priceFactorList){
			evalFactorList.addAll(priceFactorList);
		}
		List<EvaluationFactor> billFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderId, EvaluationFactorEnum.AUDIT_BILL);
		if(null != billFactorList){
			evalFactorList.addAll(billFactorList);
		}
		return evalFactorList;
	}
	
	/**
	 * @Description: 导入
	 * @author guoyr
	 */
	private void importFactorAction(){
		if(!importBtn.getEnabled()){
			return ;
		}
		if(MessageDialog.openConfirm(getShell(), "导入的指标会覆盖已有的指标， 是否继续？")){
			//删除之前的指标
			evaluationFactorService.delAllEvaluationFactor(tenderProjectEntity.getId());
			
			// 这里获得的指标已经是树型结构的
			List<EvaluationFactor> complianceFactorList = factorTree.getEvaluationFactorListByType(EvaluationFactorEnum.AUDIT_COMPLIANCE);
			List<EvaluationFactor> detailFactorList = factorTree.getEvaluationFactorListByType(EvaluationFactorEnum.AUDIT_SCORE);
			List<EvaluationFactor> creditFactorList = factorTree.getEvaluationFactorListByType(EvaluationFactorEnum.AUDIT_CREDIT);
			if(null != creditFactorList && creditFactorList.size() > 0 && null != detailFactorList){
				detailFactorList.addAll(creditFactorList);
			}
			List<EvaluationFactor> priceFactorList = factorTree.getEvaluationFactorListByType(EvaluationFactorEnum.AUDIT_PRICE);
			List<EvaluationFactor> billFactorList = factorTree.getEvaluationFactorListByType(EvaluationFactorEnum.AUDIT_BILL);
			int importCount = (null != complianceFactorList?complianceFactorList.size():0) + (null != detailFactorList?detailFactorList.size():0) + (null != priceFactorList?priceFactorList.size():0) + (null != billFactorList?billFactorList.size():0) ;
			if(importCount == 0){
				MessageDialog.openWarning(getShell(), "请选择要导入的指标！");
				return ;
			}
			boolean importFlag = evaluationFactorService.importEvaluationFactor(tenderProjectEntity.getId(), complianceFactorList, detailFactorList, priceFactorList, billFactorList);
			if(importFlag){
				CreateEvaluationStepThread createEvaluationStepThread = new CreateEvaluationStepThread(tenderProjectEntity, null, new CallBack(){
					public void callBack(final boolean flag, final Object message) {
					}
				});
				createEvaluationStepThread.start();
				if(null != callBack){
					callBack.callBack();
				}
				getShell().close();
			}else {
				MessageDialog.openWarning(getShell(), "为["+tenderProjectEntity.getTenderNo()+"]"+tenderProjectEntity.getTenderName()+"导入指标失败！");
			}
		}
	}
	
	public CallBack getCallBack() {
		return callBack;
	}
	
	public void setCallBack(CallBack callBack) {
		this.callBack = callBack;
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(684, 475);
	}
}
