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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
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.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
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.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 选择评审视图
 * @author chenxw 
 */
public class SelectEvaluationViewShell extends Shell {
	
	private Image factor = ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/factor.png");
	private Image singleFactor = ResourceManager.getPluginImage("com.gx.obe.evaluation", "icons/singleFactor.png");
	
	private SelectEvaluationViewListener listener;
	private Listener deactivateListener = new Listener() {
		public void handleEvent(Event arg0) {
			close();
		}
	};
	private Composite composite;
	private int numColumns = 0;
	
	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public SelectEvaluationViewShell(Shell shell) {
		super(shell, SWT.NO_TRIM);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setBackgroundMode(SWT.INHERIT_FORCE);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				removeListener(SWT.Deactivate, deactivateListener);
			}
		});
		
		composite = new Composite(this, SWT.NONE);
		
		createEvaluationView("集中评审", factor, com.gx.obe.components.core.enumeration.EvaluationStepEnum.EVALUATION_TYPE_COLLECT, false);
		createEvaluationView("智能评审", singleFactor, com.gx.obe.components.core.enumeration.EvaluationStepEnum.EVALUATION_TYPE_AI, true);
		
		init();
		createContents();
	}
	
	/**
	 * @Description: 初始化
	 * @author chenxw 
	 */
	private void init() {
		GridLayout gl_composite = new GridLayout(numColumns, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		addListener(SWT.Deactivate, deactivateListener);
	}
	
	/**
	 * @Description: 创建视图按钮
	 * @author chenxw
	 * @param name
	 * @param image
	 * @param viewType
	 * @param isSep 
	 */
	private void createEvaluationView(String name, Image image, final String viewType, boolean isSep) {
		if (isSep) {
			createESepator();
		}
		
		numColumns++;
		final Composite viewComposite = new Composite(composite, SWT.NONE);
		viewComposite.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		GridLayout gl_viewComposite = new GridLayout(1, false);
		gl_viewComposite.verticalSpacing = 0;
		gl_viewComposite.horizontalSpacing = 0;
		gl_viewComposite.marginHeight = 0;
		gl_viewComposite.marginWidth = 0;
		viewComposite.setLayout(gl_viewComposite);
		viewComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		final CLabel label = new CLabel(viewComposite, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		label.setText(name);
		label.setAlignment(SWT.CENTER);
		
		CLabel label_2 = new CLabel(viewComposite, SWT.NONE);
		label_2.setImage(image);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		label_2.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				selectEvaluationViewAction(viewType);
			}
		});
		
		setBackgroundColor(viewComposite, new MouseTrackAdapter() {
			public void mouseEnter(MouseEvent arg0) {
				viewComposite.setBackground(ColorConstants.SKIN_BG);
				label.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
			}
			
			public void mouseExit(MouseEvent e) {
				viewComposite.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
				label.setForeground(SWTResourceManager.getColor(0, 0, 0));
			}
		});
	}
	
	/**
	 * @Description: 创建间隔线条
	 * @author chenxw 
	 */
	private void createESepator() {
		numColumns++;
		ESepator sep = new ESepator(composite, SWT.VERTICAL);
		sep.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		sep.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
	}
	
	/**
	 * @Description: 递归设置当前面板的鼠标进入时整个面板的背景颜色
	 * @author guoyr
	 * @param targetComposite
	 * @param listener 
	 */
	private void setBackgroundColor(Composite targetComposite, MouseTrackAdapter listener){
		targetComposite.addMouseTrackListener(listener);
		for(Control contrl : targetComposite.getChildren()){
			if(contrl instanceof Composite){
				setBackgroundColor((Composite)contrl, listener);
			}else{
				contrl.addMouseTrackListener(listener);
			}
		}
	}
	
	public void selectEvaluationViewAction(String viewType) {
		close();
		if (null != listener) {
			listener.selectEvaluationView(viewType);
		}
	}
	
	public void addSelectEvaluationViewListener(SelectEvaluationViewListener listener) {
		this.listener = listener;
	}
	
	public interface SelectEvaluationViewListener {
		void selectEvaluationView(String viewType);
	}
	
	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		Point point = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		setSize(point.x, point.y);
		SwtUtils.setCenter(getShell());
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
