package com.gx.obe.management.shell;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CloseShellThread;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
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.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.management.Lisener.AddTemplateCallBackAdapter;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 添加模板操作面板，显示模板操作类型：从模板库添加或者从项目添加
 * @author guoyr
 */
public class AddActionListShell extends Shell {

	private TenderProjectEntity tenderProjectEntity;
	private Rectangle rectangle;
	private ImageResolver imageResolver = new ImageResolver();
	private AddTemplateCallBackAdapter callBack;
	
	{
		LogUtils.logClass(AddActionListShell.class);
	}
	
	/**
	 * Create the shell.
	 * @param display
	 */
	public AddActionListShell(Shell shell, TenderProjectEntity tenderProjectEntity, Rectangle rectangle, final AddTemplateCallBackAdapter callBack) {
		super(shell, SWT.NO_TRIM |SWT.TOP);
		setBackground(SWTResourceManager.getColor(128, 128, 128));
		this.callBack = callBack;
		this.rectangle = rectangle;
		FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
		setLayout(fillLayout);
		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackground(SWTResourceManager.getColor(255, 255, 255));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		
		CLabel lblNewLabel = new CLabel(composite, SWT.SELECTED);
		GridData gd_lblNewLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblNewLabel.widthHint = 117;
		lblNewLabel.setLayoutData(gd_lblNewLabel);
		
		lblNewLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent mouseevent) {
				//TODO
				getShell().close();
				callBack.AddTemplate(false);
			}
		});
		lblNewLabel.setText("从模板库添加");
		lblNewLabel.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		lblNewLabel.setForeground(null);
		lblNewLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point.png"));
		lblNewLabel.addMouseTrackListener(new ActionMouseTrackListener(lblNewLabel));
		
		CLabel lblNewLabel_1 = new CLabel(composite,SWT.SELECTED);
		GridData gd_lblNewLabel_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblNewLabel_1.widthHint = 117;
		lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);
		lblNewLabel_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent mouseevent) {
				//TODO
				getShell().close();
				callBack.AddTemplate(true);
			}
		});
		
		lblNewLabel_1.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		lblNewLabel_1.setForeground(null);
		lblNewLabel_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point.png"));
		lblNewLabel_1.setText("从已有项目添加");
		lblNewLabel_1.addMouseTrackListener(new ActionMouseTrackListener(lblNewLabel_1));
		createContents();
	}
	
	protected void createContents() {
		setText(Messages.Button_moreOperate);
		setSize(132, 53);
		this.setMinimumSize(70, 20);
		this.pack();

	}

	@Override
	public void open() {
		this.pack();
		CloseShellThread closeShellThread = new CloseShellThread(this,rectangle.height-20);
		closeShellThread.addContainsRectangle(rectangle);
		closeShellThread.start();
		super.open();
	}

	/**
	 * @Description: 鼠标移动至菜单上的监听
	 * @author guoyr
	 * @param composite
	 * @param actionLabel
	 */
	class ActionMouseTrackListener extends MouseTrackAdapter{
		private CLabel actionLabel;
		public ActionMouseTrackListener(CLabel actionLabel){
			this.actionLabel = actionLabel;
		}
		public void mouseEnter(MouseEvent e) {
			actionLabel.setBackground(ColorConstants.SKIN_BG);
			actionLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point_green.png"));
			actionLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		}
		public void mouseExit(MouseEvent e) {
			actionLabel.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
			actionLabel.setForeground(null);
			actionLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point.png"));
		}
	}
	
	public TenderProjectEntity getTenderProject() {
		return tenderProjectEntity;
	}

	public void setTenderProject(TenderProjectEntity tenderProjectEntity) {
		this.tenderProjectEntity = tenderProjectEntity;
	}

	public void close() {
		imageResolver.disposite();
		super.close();
	}

	@Override
	protected void checkSubclass() {
	}
}
