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

import java.util.LinkedHashMap;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.listener.MouseDragListener;
import org.eclipse.swt.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StackLayout;
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.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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.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.icons.IconsActivator;
import com.gx.obe.common.widget.shell.SelectionShell;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.tools.ole.DocumentFactory;
import com.gx.tools.ole.DocumentLoader;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class BidDocumentLoaderShell extends Shell implements SelectionShell.SelectionListener<BidFile> {
	
	private ImageResolver imageResolver = new ImageResolver();
	private FileCompositeLRU compositeLRU = new FileCompositeLRU();
	private StackLayout fileStackLayout = new StackLayout();
	private Composite fileComposite;
	private Composite noFileComposite;
	private CLabel fileLabel;
	private CLabel bidFileListLabel;
	private List<BidFile> fileList;
	private boolean isMax = false;
	
	/**
	 * Create the shell.
	 * 
	 * @param shell
	 */
	public BidDocumentLoaderShell(Shell shell) {
		super(shell, SWT.NO_TRIM | SWT.ON_TOP);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		setBackground(ColorConstants.SKIN_BG);
		setBackgroundMode(SWT.INHERIT_FORCE);
		setText("查看文件");
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				disposite();
			}
		});
		
		Composite composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(4, false);
		gl_composite.marginLeft = 5;
		gl_composite.marginWidth = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label = new CLabel(composite, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setText("查看文件");
		MouseDragListener mouseDragListener = new MouseDragListener(this);
		label.addListener(SWT.MouseDown, mouseDragListener);
		label.addListener(SWT.MouseUp, mouseDragListener);
		label.addListener(SWT.MouseMove, mouseDragListener);
		
		ELabel min_size_btn = new ELabel(composite, SWT.NONE | SWT.SELECTED);
		min_size_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/min_size.png"));
		min_size_btn.setMargins(5, 5, 5, 5);
		min_size_btn.setActiveColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		min_size_btn.setAlpha(10);
		min_size_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				setMinimized(true);
			}
		});
		
		ELabel max_btn = new ELabel(composite, SWT.NONE | SWT.SELECTED);
		max_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/max.png"));
		max_btn.setMargins(5, 5, 5, 5);
		max_btn.setActiveColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		max_btn.setAlpha(10);
		max_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				setMaximized(!isMax);
				isMax = !isMax;
			}
		});
		
		ELabel close_btn = new ELabel(composite, SWT.NONE | SWT.SELECTED);
		close_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/close.png"));
		close_btn.setMargins(5, 5, 5, 5);
		close_btn.setActiveColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		close_btn.setAlpha(10);
		close_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				closeAction();
			}
		});
		
		Composite composite_1 = new Composite(this, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.marginHeight = 0;
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_2 = new Composite(composite_1, SWT.NONE);
		composite_2.setLayout(new GridLayout(2, false));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		fileLabel = new CLabel(composite_2, SWT.NONE);
		fileLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		fileLabel.setText("文件名称");
		
		bidFileListLabel = new CLabel(composite_2, SWT.NONE);
		bidFileListLabel.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		bidFileListLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));
		bidFileListLabel.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		bidFileListLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/next.png"));
		bidFileListLabel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				bidFileListLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down_simp.png"));
				bidFileListLabel.getParent().layout();
				fileListAction();
			}
		});
		
		ESepator noBidFileSep = new ESepator(composite_1, SWT.HORIZONTAL);
		noBidFileSep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		noBidFileSep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		fileComposite = new Composite(composite_1, SWT.NONE);
		fileComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		fileComposite.setLayout(fileStackLayout);
		
		noFileComposite = new Composite(fileComposite, SWT.NONE);
		GridLayout gl_noBidFileComposite = new GridLayout(1, false);
		gl_noBidFileComposite.verticalSpacing = 0;
		gl_noBidFileComposite.horizontalSpacing = 0;
		gl_noBidFileComposite.marginHeight = 0;
		gl_noBidFileComposite.marginWidth = 0;
		noFileComposite.setLayout(gl_noBidFileComposite);
		
		Label noBidFileLabel = new Label(noFileComposite, SWT.NONE);
		noBidFileLabel.setForeground(SWTResourceManager.getColor(200, 200, 200));
		noBidFileLabel.setFont(SWTResourceManager.getFont("宋体", 40, SWT.BOLD));
		noBidFileLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		noBidFileLabel.setText(Messages.Tips_noBidFiles);
	}
	

	public void refresh(List<BidFile> fileList) {
		this.setVisible(true);
		this.setMinimized(false);
		this.forceActive();
		this.fileList = fileList;
		if (null == fileList || fileList.isEmpty()) {
			selection(null);
		} else {
			selection(fileList.get(0));
		}
	}
	
	/**
	 * @Description: 投标文件列表展示
	 * @author chenxw
	 */
	private void fileListAction() {
		Point point = fileLabel.toDisplay(new Point(0, 0));
		Rectangle parentRectangle = fileLabel.getBounds();
		SelectionShell<BidFile> selectionShell = new SelectionShell<BidFile>(getShell());
		selectionShell.addSelectionListener(this);
		selectionShell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				bidFileListLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/next.png"));
				bidFileListLabel.getParent().layout();
			}
		});
		selectionShell.listCache(CollectionUtils.toHashMap(fileList, BidFile::getSimpleName));
		selectionShell.setBackgroundImage(imageResolver.getImage(getShell().getBackgroundImage(), parentRectangle.width, 200));
		selectionShell.setLocation(point.x - 1, point.y + parentRectangle.height);
		selectionShell.setSize(Math.max(parentRectangle.width + 2, 200), selectionShell.computeSize(parentRectangle.width, SWT.DEFAULT).y);
		selectionShell.setAlpha(220);
		selectionShell.open();
	}
	
	public void selection(BidFile file) {
		if (null == file) {
			fileLabel.setText(Messages.Tips_noBidFiles);
			fileLabel.getParent().layout();
			fileStackLayout.topControl = noFileComposite;
		} else {
			fileLabel.setText(file.getSimpleName());
			fileLabel.getParent().layout();
			String path = file.getAbsolutePath();
			Composite composite = compositeLRU.get(path);
			if (null == composite) {
				composite = new Composite(fileComposite, SWT.NONE);
				DocumentLoader documentLoader = DocumentFactory.getDocumentLoader(path, PropertiesUtils.getProperty(ConfigConstants.PDF_ACTIVE_TYPE, null));
				documentLoader.openDocument(composite, true);
				composite.setData(documentLoader);
				composite.setLayout(new FillLayout());
				compositeLRU.put(path, composite);
			} else {
				composite.redraw();
				((DocumentLoader) composite.getData()).activate();
			}
			fileStackLayout.topControl = composite;
		}
		fileComposite.layout();
	}
	
	private void closeAction() {
		close();
	}
	
	private void disposite() {
		imageResolver.disposite();
	}
	
	class FileCompositeLRU extends LinkedHashMap<String, Composite> {
		private static final long serialVersionUID = 1L;
		
		protected boolean removeEldestEntry(java.util.Map.Entry<String, Composite> eldest) {
			if (size() > 5) {
				eldest.getValue().dispose();
				return true;
			}
			return false;
		}
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
}
