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

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.custom.SashForm;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.listener.CompareBidFileListener;
import com.gx.obe.util.utils.ScreenUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.tools.composite.FileOpenComposite;
import com.gx.tools.listener.LocationBidFileListener;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class CompareFileWindow {
	private CompareBidFileListener compareBidFileListener;
	private List<Supplier> compareSupplierList;
	protected Shell shell;
	private SashForm sashForm;
	private TenderProjectEntity tenderProjectEntity;
	private ScreenUtils  util = new ScreenUtils();
	private int screenHight;
	private Composite topComposite;
	private Composite composite;
	private ELink label_2;
	private ImageButton selectAllSupplierBtn;
	private boolean autoLocation = true;
	private ESepator sepator;
	Map<String, LocationBidFileListener> locationBidFileListenerMap = new HashMap<String, LocationBidFileListener>();
	private EvaluationStep curStep;
//	private Composite composite_1;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			CompareFileWindow window = new CompareFileWindow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public CompareFileWindow(){
		
	}
	public CompareFileWindow(CompareBidFileListener compareBidFileListener, List<Supplier> compareSupplierList, TenderProjectEntity tenderProjectEntity, EvaluationStep curStep){
		this.compareBidFileListener = compareBidFileListener;
		this.compareSupplierList = compareSupplierList;
		this.tenderProjectEntity = tenderProjectEntity;
		this.curStep = curStep;  
	}
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		
		shell = new Shell(SWT.NO_TRIM);
		if(PropertiesUtils.getProperty(ConfigConstants.EVAL_FULL_SCREEN, "00").equals(CommonEnum.YES)) {
			screenHight = util.getScreenHight();
		}else {
			screenHight = util.getScreenHight() -util.getTaskBarHight();
		}
//		shell.getDisplay().getSystemTray().
		shell.setText("标书对比");
		shell.setSize(util.getScreenWight(),screenHight);
		shell.setLocation(0, 0);
//		shell.setLayout(new GridLayout(1, false));
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
		shell.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		shell.setLayout(gridLayout);
		shell.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/evaluation-ico.png"));
		
		topComposite = new Composite(shell, SWT.NONE);
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 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);
		topComposite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/main_title_bg.png"));
		topComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		composite = new Composite(topComposite, SWT.NONE);
		GridLayout gl_composite = new GridLayout(3, false);
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label messageLiabel = new Label(composite, SWT.NONE);
		messageLiabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		messageLiabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		messageLiabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		messageLiabel.setText("标书对比");
		
		label_2 = new ELink(composite, SWT.NONE);
		label_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.setMinimized(true);
			}
		});
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/min_size.png"));
		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, false, false, 1, 1));
		cancelLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});

		Composite tool_composite = new Composite(shell, SWT.NONE);
		tool_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		tool_composite.setLayout(new GridLayout(1, false));
		tool_composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		selectAllSupplierBtn = new ImageButton(tool_composite, SWT.NONE);
		selectAllSupplierBtn.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		selectAllSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/checked.png"));
		selectAllSupplierBtn.setText("自动定位");
		
		sepator = new ESepator(shell, SWT.NONE);
		sepator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		sepator.setBackground(ColorConstants.BORDER_LINE_COLOR);
		selectAllSupplierBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				autoLocationAction(!autoLocation);
			}
		});
		
		
		sashForm = new SashForm(shell, SWT.NONE);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		init();
	}
	
	/**
	 * @Description: 初始化数据
	 * @author guoyr 
	 */
	private void init(){
		EvaluationFactor factor = compareBidFileListener.getSelectEvaluationFactor();
		locationBidFileListenerMap.clear();
		if(null != compareSupplierList && compareSupplierList.size() > 0){
			for(Supplier supplier : compareSupplierList){
				FileOpenComposite fileOpenComposite = new FileOpenComposite(sashForm, SWT.NONE, tenderProjectEntity, supplier, curStep);
				fileOpenComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
				locationBidFileListenerMap.put(supplier.getId(), fileOpenComposite);
				fileOpenComposite.selectSupplierAndFactorAction(factor);
			}
		}
		if(locationBidFileListenerMap.size() > 0){
			compareBidFileListener.initListener(locationBidFileListenerMap);
		}
	}
	/**
	 * @Description: 是否锁定
	 * @author guoyr 
	 */
	private void autoLocationAction(boolean lock){
		autoLocation = lock;
		if(null != locationBidFileListenerMap && locationBidFileListenerMap.size() > 0){
			Set<String> keySet = locationBidFileListenerMap.keySet();
			Iterator<String> iter = keySet.iterator();
			while(iter.hasNext()){
				String supplierId = iter.next();
				LocationBidFileListener locationBidFileListener = locationBidFileListenerMap.get(supplierId);
				if(null != locationBidFileListener){
					locationBidFileListener.setAutoLocation(lock);
				}
			}
		}
		if(lock){
			selectAllSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/checked.png"));
		}else {
			selectAllSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/unchecked.png"));
		}
	}
	
	/**
	 * @Description: 关闭
	 * @author guoyr
	 */
	public void cancelAction() {
		compareBidFileListener.disponsed();
		shell.close();
	}
}
