package com.gx.obe.management.shell;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.EButton;
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.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
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.components.core.enumeration.CommonEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class EvaluationStepOperatorShell extends Shell {

	private EButton preNodeLabel;
	private EButton editNodeLabel;
	private EButton delNodeLabel;
	private EButton nextNodeLabel;
	private EvaluationStepListener evaluationStepListener;
	private EvaluationStep evaluationStep;
	private Listener deactivateListener;

	{
		LogUtils.logClass(EvaluationStepOperatorShell.class);
	}
	
	/**
	 * Create the shell.
	 * @param display
	 */
	public EvaluationStepOperatorShell(Shell shell, EvaluationStep evaluationStep) {
		super(shell, SWT.NO_TRIM);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				removeListener(SWT.Deactivate, deactivateListener);
			}
		});
		this.evaluationStep = evaluationStep;
		setBackground(ColorConstants.SKIN_BG);
		SwtUtils.setCircleRegion(this, 12);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);

		Composite composite = new Composite(this, SWT.NONE);
		SwtUtils.setCircleRegion(composite, 12);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.spacing = 1;
		rl_composite.marginBottom = 0;
		rl_composite.marginTop = 0;
		rl_composite.marginRight = 0;
		rl_composite.marginLeft = 0;
		composite.setLayout(rl_composite);
		preNodeLabel = new EButton(composite, SWT.NONE);
		preNodeLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				movePreAction();
			}
		});
		preNodeLabel.setText(Messages.MoveForward);
		preNodeLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/pre.png"));
		editNodeLabel = new EButton(composite, SWT.NONE);
		editNodeLabel.setText(Messages.Button_modify);
		editNodeLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/edit.png"));
		editNodeLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				editStepAction();
			}
		});
		delNodeLabel = new EButton(composite, SWT.NONE);
		delNodeLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		delNodeLabel.setText(Messages.Button_delete);
		delNodeLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delStepAction();
			}
		});
		nextNodeLabel = new EButton(composite, SWT.NONE);
		nextNodeLabel.setText(Messages.MoveBackwards);
		nextNodeLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/next.png"));
		nextNodeLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				moveNextAction();
			}
		});
		createContents();
		init();
	}

	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		if(ObjectUtils.equals(evaluationStep.getStepStatus(), CommonEnum.IN_PROGRESS) || ObjectUtils.equals(evaluationStep.getStepStatus(), CommonEnum.COMPLETE)){
			editNodeLabel.setText(Messages.Button_view);
			editNodeLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/view.png"));
		}
		setButtonCss(preNodeLabel);
		setButtonCss(editNodeLabel);
		setButtonCss(delNodeLabel);
		setButtonCss(nextNodeLabel);
		deactivateListener = new Listener(){
			public void handleEvent(Event arg0) {
				close();
			}
		};
		
		addListener(SWT.Deactivate, deactivateListener);
	}
	private void setButtonCss(EButton ebutton){
		ebutton.setBorderColor(null);
		ebutton.setBackgroundColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		ebutton.setActiveForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		ebutton.setActiveBackgroundColor(SWTResourceManager.getColor(255, 153, 0));
		ebutton.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		ebutton.setForeground(ColorConstants.NOTE_TIPS_COLOR);
	}
	
	/**
	 * @Description: 向前移
	 * @author guoyr
	 */
	private void movePreAction(){
		if(preNodeLabel.getEnabled()){
			evaluationStepListener.movePre(evaluationStep);
		}
		if(!this.isDisposed()){
			this.dispose();
		}
	}
	
	/**
	 * @Description: 修改
	 * @author guoyr
	 */
	private void editStepAction(){
		if(!this.isDisposed()){
			this.dispose();
		}
		if(editNodeLabel.getEnabled()){
			evaluationStepListener.edit(evaluationStep);
		}
	}
	
	/**
	 * @Description: 删除
	 * @author guoyr
	 */
	private void delStepAction(){
		if(delNodeLabel.getEnabled()){
			evaluationStepListener.delete(evaluationStep);
		}
		if(!this.isDisposed()){
			this.dispose();
		}
	}
	
	/**
	 * @Description: 向后移
	 * @author guoyr
	 */
	private void moveNextAction(){
		if(nextNodeLabel.getEnabled()){
			evaluationStepListener.moveNext(evaluationStep);
		}
		if(!this.isDisposed()){
			this.dispose();
		}
	}

	/**
	 * @Description: 是否允许向前移
	 * @author guoyr
	 * @param visible
	 */
	public void setPreBtnEnable(boolean visible) {
		preNodeLabel.setEnabled(visible);
	}

	/**
	 * @Description: 是否允许向后移
	 * @author guoyr
	 * @param visible
	 */
	public void setNextBtnEnable(boolean visible) {
		nextNodeLabel.setEnabled(visible);
	}

	/**
	 * @Description: 是否允许修改
	 * @author guoyr
	 * @param visible
	 */
	public void setEditBtnEnable(boolean visible) {
		editNodeLabel.setEnabled(visible);
	}

	/**
	 * @Description: 是否可删除
	 * @author guoyr
	 * @param visible
	 */
	public void setDelBtnEnable(boolean visible) {
		delNodeLabel.setEnabled(visible);
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("");
		setSize(221, 42);

	}

	public void addEvaluationStepListener(EvaluationStepListener evaluationStepListener){
		this.evaluationStepListener = evaluationStepListener;
	}
	
	public interface EvaluationStepListener {
		
		//向前移动
		public void movePre(EvaluationStep evaluationStep);
		// 向后移动
		public void moveNext(EvaluationStep evaluationStep);
		// 修改
		public void edit(EvaluationStep evaluationStep);
		// 删除
		public void delete(EvaluationStep evaluationStep);
	}
	@Override
	protected void checkSubclass() {
	}

}
