package com.gx.obe.management.shell;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.widgets.ESepator;
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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.management.Lisener.MoreOperationListener;
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;

/**
 * 更多操作步骤界面
 * @author guoyrhong
 */
public class MoreOperationListShell extends Shell {

	private TenderProjectEntity tenderProjectEntity;
	private Composite composite;
	private Rectangle rectangle;
	private ImageResolver imageResolver = new ImageResolver();
	private MoreOperationListener listener;
	private boolean isClear;
	private Listener deactivateListener;
	{
		LogUtils.logClass(MoreOperationListShell.class);
	}
	
	/**
	 * Create the shell.
	 * @param display
	 */
	public MoreOperationListShell(Shell shell, boolean isClear) {
		super(shell, SWT.NO_TRIM |SWT.TOP);
		setBackground(ColorConstants.SKIN_BG);
		setBackgroundMode(SWT.INHERIT_FORCE);
		FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
		fillLayout.marginHeight = 1;
		fillLayout.marginWidth = 1;
		setLayout(fillLayout);
		this.setAlpha(230);
		this.isClear = isClear;
//		SWTUtil.setCircleRegion(this, 2);
		composite = new Composite(this, SWT.NONE);
		composite.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		composite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		composite.setLayout(gl_composite);
		
		CLabel label = new CLabel(composite, SWT.NONE);
		label.setLeftMargin(5);
		label.setRightMargin(5);
		label.setTopMargin(5);
		label.setBottomMargin(5);
		label.setBackground(ColorConstants.SKIN_BG);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label.setText(Messages.Button_moreOperate);
		
		init();
		createContents();
	}
	
	/**
	 * 界面数据初始化
	 */
	private void init(){
		clearStepAction();
		deactivateListener = new Listener(){
			public void handleEvent(Event arg0) {
				close();
			}
		};
		addListener(SWT.Deactivate, deactivateListener);
	}

	/**
	 * 创建按钮入口
	 * @return
	 */
	private CLabel createItem(){
		ESepator label_1 = new ESepator(composite, SWT.NONE);
		label_1.setBackground(SWTResourceManager.getColor(227, 233, 238));
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		CLabel itemLabel = new CLabel(composite, SWT.SELECTED);
		itemLabel.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		itemLabel.setLeftMargin(5);
		itemLabel.setRightMargin(5);
		itemLabel.setTopMargin(5);
		itemLabel.setBottomMargin(5);
		itemLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		itemLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		itemLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point.png"));
		itemLabel.addMouseTrackListener(new ActionMouseTrackListener(itemLabel));
		return itemLabel;
	}
	public void setDate(Rectangle rectangle){
		this.rectangle = rectangle;
	}
	public void addProjectOpListener(MoreOperationListener listener){
		this.listener = listener;
	}

	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.setBackground(SWTResourceManager.getColor(255, 255, 255));
			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"));
		}
	}


	/**
	 * 清空评标步骤按钮 
	 */
	private void clearStepAction(){
		CLabel itemLabel = createItem();
		itemLabel.setText("清空评标步骤");
		if(!isClear){
			itemLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
			itemLabel.setEnabled(false);
		}
		itemLabel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				getShell().close();
				if(null != listener){
					listener.clearEvalNodeActionListener();
				}
			}
		});
	}
	
	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText(Messages.Button_moreOperate);
		setSize(197, 128);
		this.setMinimumSize(100, 20);
		this.pack();

	}

	@Override
	public void open() {
		this.pack();
		super.open();
	}

	public TenderProjectEntity getTenderProject() {
		return tenderProjectEntity;
	}

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

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

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

}
