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

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
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.Label;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.file.entity.BidFile;
import com.gx.obe.common.file.entity.CheckFileVo;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.utils.MenuFunctionUtils;
import com.gx.obe.evaluation.step.listener.CompareBidFileListener;
import com.gx.obe.evaluation.step.listener.CompareSupplierFileListener;
import com.gx.obe.message.Messages;

import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.auth.Menu;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 显示投标人评审点
 * @author guoyr
 */
public class ShowCompareSupplierListShell extends Shell {
	
	private Composite composite;
	private Composite supplierListComposite;
	private List<Supplier> compareSupplierList = new ArrayList<Supplier>();
	private CompareSupplierFileListener listener;
	private TenderProjectEntity tenderProjectEntity;
	private Map<String, BidFile> bidFileMap = new HashMap<String, BidFile>();
	private Set<String> compareSupplierIdSet = new HashSet<String>();
	private ELink compareButton;
	private CompareBidFileListener compareBidFileListener;
	private CompareFileWindow compareFileWindow;
	private EvaluationStep curStep;
	private ELink samenessCompareButton;
	private MenuFunctionUtils menuFunctionUtils = new  MenuFunctionUtils();
	private Menu menu;
	/**
	 * @param shell
	 * @param evalPointListener
	 */
	public ShowCompareSupplierListShell(Shell shell, TenderProjectEntity tenderProjectEntity, EvaluationStep curStep,  Menu menu,CompareBidFileListener compareBidFileListener, CompareSupplierFileListener listener) {
		super(shell, SWT.NO_TRIM | SWT.TOP);
		setBackgroundMode(SWT.INHERIT_FORCE);
		setBackground(ColorConstants.BORDER_LINE_COLOR);
		this.menu = menu;
		this.compareBidFileListener = compareBidFileListener;
		GridLayout gridLayout = new GridLayout(1, false);
		this.listener = listener;
		this.tenderProjectEntity = tenderProjectEntity;
		this.curStep = curStep;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		gridLayout.verticalSpacing = 0;
		setLayout(gridLayout);
		
		Composite topComposite = new Composite(this, SWT.NONE);
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		topComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_topComposite = new GridLayout(1, false);
		gl_topComposite.verticalSpacing = 0;
		gl_topComposite.horizontalSpacing = 0;
		gl_topComposite.marginWidth = 0;
		gl_topComposite.marginHeight = 0;
		topComposite.setLayout(gl_topComposite);
		
		composite = new Composite(topComposite, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.horizontalSpacing = 0;
		gl_composite.verticalSpacing = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite.setBackground(ColorConstants.SKIN_BG);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		Label messageLiabel = new Label(composite, SWT.NONE);
		messageLiabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		messageLiabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		messageLiabel.setText(Messages.Button_bidFileCompare);
		SwtUtils.setMouseDragListener(messageLiabel);
		
		ELink cancelLink = new ELink(composite, SWT.NONE);
		cancelLink.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		cancelLink.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		cancelLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/close.png"));
		cancelLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		cancelLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		cancelLink.setText("");
		
//		ESepator pointSep = new ESepator(composite, SWT.NONE);
//		pointSep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
//		pointSep.setBackground(SWTResourceManager.getColor(60, 138, 226));
//		pointSep.setLineWidth(2);
		
		supplierListComposite = new Composite(topComposite, SWT.NONE);
		supplierListComposite.setLayout(new GridLayout(1, false));
		supplierListComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
		
//		Label label = new Label(topComposite, SWT.NONE);
//		label.setForeground(ColorConstants.NOTE_TIPS_COLOR);
//		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
//		label.setText(MessageFormat.format(Messages.Tips_maxComparFilesCount, PropertiesUtils.getProperty(ConfigConstants.COMPARE_FILE_COUNT, "3")));
		
		Composite composite_2 = new Composite(topComposite, SWT.NONE);
		composite_2.setLayout(new GridLayout(4, false));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_2.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/title_bg.png"));
		
		ELink addCompareButton = new ELink(composite_2, SWT.NONE);
		addCompareButton.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		addCompareButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				listener.addCompareSupplier();
			}
		});
		
		addCompareButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		addCompareButton.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		addCompareButton.setText(Messages.Button_addCompare);
		
		compareButton = new ELink(composite_2, SWT.NONE);
		compareButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		compareButton.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		compareButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				compareAction();
			}
		});
		
		compareButton.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/enlarge.png"));
		compareButton.setText(Messages.Button_startCompare);
		compareButton.setEnabled(false);
		
		
		samenessCompareButton = new ELink(composite_2, SWT.NONE);
		samenessCompareButton.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		samenessCompareButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				samenessCompareAction();
 			}
		});
		
		samenessCompareButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		samenessCompareButton.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		samenessCompareButton.setText("雷同性分析");
		compareButton.setEnabled(false);
		menuFunctionUtils.addFunction(samenessCompareButton, "SAMENESS_ANALYSIS");

		
		ELink button_1 = new ELink(composite_2, SWT.NONE);
		button_1.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		button_1.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		button_1.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				deleteAllCompareSupplier();
			}
		});
		button_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		button_1.setText(Messages.Button_clearAll);
		
		init();
		createContents();
	}

	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		// 设置拖拽
		SwtUtils.setMouseDragListener(composite);
		menuFunctionUtils.initFunctionAuthority(menu);
	}
	
	/**
	 * @Description: 关闭
	 * @author guoyr
	 */
	public void cancelAction() {
		getShell().close();
	}
	
	protected void createContents() {
		setText("");
		setSize(250, 350);
		this.setMinimumSize(250, 400);
		this.pack();
		
		this.setAlpha(230);
//		setLocation(util.getScreenWight() - 400, 100);
//		setLocation(0, 0);
	}
	
	public void addCompareItem(Supplier compareSupplier, final BidFile file, String supplierId){
		if(compareSupplierIdSet.add(compareSupplier.getId())){
			compareSupplierList.add(compareSupplier);
			if(null != supplierId && !"".equals(supplierId.trim()) && null != file){
				bidFileMap.put(supplierId, file);
			}
			reflashSupplierTable();
		}else {
			MessageDialog.openShortMessage(MessageFormat.format(Messages.Tips_alreadyExists, compareSupplier.getSupplierName()));
		}
	}
	
	/**
	 * @Description: 
	 * @author guoyr 
	 */
	private void deleteAllCompareSupplier(){
		compareSupplierList.clear();
		bidFileMap.clear();
		compareSupplierIdSet.clear();
		reflashSupplierTable();
//		reflashSupplierList(compareSupplierList, null, null);
		listener.refreshCompareSupplierList(compareSupplierList);
	}
	/**
	 * @Description: 开始比对文件
	 * @author guoyr 
	 */
	private void compareAction(){
		if(null != compareSupplierList && compareSupplierList.size() > 1){
			if(null == compareFileWindow || compareFileWindow.shell.isDisposed()){
				compareFileWindow = new CompareFileWindow(compareBidFileListener, compareSupplierList, tenderProjectEntity, curStep);
				compareFileWindow.open();
			}else{
				compareFileWindow.shell.setMaximized(true);
				compareFileWindow.shell.setFocus();
			}
			
		}
	}
	
	private void samenessCompareAction(){
		if (null != compareSupplierList && compareSupplierList.size() > 1) {
			List<CheckFileVo> list = new ArrayList<CheckFileVo>();
			for (Supplier supplier : compareSupplierList) {
				List<BidFile> bidFileList = BidFile.getSupplierBidFileList(tenderProjectEntity.getId(),supplier.getId(), null);
				boolean isSealedBidEvaluation = curStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_SEALED_BID, Boolean.class);
				CollectionUtils.filter(bidFileList, t -> !(isSealedBidEvaluation ^ t.isNotPublicBid()));
				for (BidFile bidFile : bidFileList) {
					if (BidFile.TYPE_BIDPRICE.equals(bidFile.getFileType())) {
						continue;
					}
					list.add(new CheckFileVo(bidFile, supplier.getSupplierName()));
				}
			}

		}
	}
	
	
	/**
	 * @Description: 刷新表格数据
	 * @author guoyr 
	 */
	private void reflashSupplierTable(){
		for(Control conctrol : supplierListComposite.getChildren()){
			conctrol.dispose();
		}
		if(null != compareSupplierList && compareSupplierList.size() > 0){
			for(int i = 0 ; i < compareSupplierList.size(); i++){
				final Supplier supplier = compareSupplierList.get(i);
				
				Composite composite_3 = new Composite(supplierListComposite, SWT.NONE);
				composite_3.setLayout(new GridLayout(2, false));
				composite_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				
				Label label = new Label(composite_3, SWT.NONE | SWT.WRAP);
				label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				label.setText(supplier.getSupplierName());
				label.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
				
				ELink link = new ELink(composite_3, SWT.NONE);
				link.addMouseListener(new MouseAdapter() {
					public void mouseUp(MouseEvent e) {
						compareSupplierList.remove(supplier);
						compareSupplierIdSet.remove(supplier.getId());
						bidFileMap.remove(supplier.getId());
						reflashSupplierTable();
						listener.refreshCompareSupplierList(compareSupplierList);
					}
				});
				link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
				
				ESepator sepator = new ESepator(supplierListComposite, SWT.NONE);
				sepator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
				sepator.setBackground(ColorConstants.SEP_LIGHT_COLOR);
				sepator.setLineWidth(2);
				
			}
		}
		compareButton.setEnabled(compareSupplierList.size() > 1);
		samenessCompareButton.setEnabled(compareSupplierList.size() > 1);
		supplierListComposite.layout();
	}

	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
