package com.gx.obe.management.dialog;

import java.awt.Component;
import java.awt.Frame;
import java.util.ArrayList;
import java.util.List;

import javax.media.Player;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
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.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.tools.video.IVideo;
import com.gx.tools.video.VideoListener;
import com.gx.tools.video.util.VideoEngineUtil;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 评标现场监控
 * @author guoyr
 */
public class EvalExpertVideoWindows {
	
	private IVideo video;
	private List<Player> playerList = new ArrayList<Player>();
	protected static Shell shell;
	private static EvalExpertVideoWindows expertVideoWindows = null;
	private boolean isCleanComposite = true;
	private ScrolledComposite scrolledComposite;
	private Composite contentComposit;
	private Composite evaluationRoomControlComposite;
	private Composite machineControlComposite;
	private ELabel minLabel;
	private ELabel closeLabel;
	private CLabel titleLabel;
	private ELink hideEvalBidRoomControl;
	private ELink hideMachineControl;
	// 日志输出
	private Logger logger = Logger.getLogger(EvalExpertVideoWindows.class);
	
	{
		LogUtils.logClass(EvalExpertVideoWindows.class);
	}
	
	public static Shell getInstance(){
		if(null == expertVideoWindows){
			expertVideoWindows = new EvalExpertVideoWindows();
			expertVideoWindows.open();
		}
		return shell;
	}
	
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.NO_TRIM);
		shell.setText("评标现场监控");
		shell.setBackground(ColorConstants.SKIN_BG);
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
		shell.setSize(750, 504);
		GridLayout gl_shell = new GridLayout(1, false);
		gl_shell.verticalSpacing = 0;
		gl_shell.marginHeight = 0;
		gl_shell.marginWidth = 0;
		shell.setLayout(gl_shell);
		
		Composite composite = new Composite(shell, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		titleLabel = new CLabel(composite, SWT.NONE);
		titleLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		titleLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		titleLabel.setFont(SWTResourceManager.getFont("楷体_GB2312", 10, SWT.BOLD));
		titleLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/video.png"));
		titleLabel.setText("评标现场监控");
		
		Composite operatorComposite = new Composite(composite, SWT.NONE);
		operatorComposite.setLayout(new GridLayout(2, true));
		
		minLabel = new ELabel(operatorComposite, SWT.BOTTOM|SWT.SELECTED);
		minLabel.setActiveColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		minLabel.setRightMargin(5);
		minLabel.setLeftMargin(5);
		minLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		minLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/min.png"));
		
		closeLabel = new ELabel(operatorComposite, SWT.BOTTOM|SWT.SELECTED);
		closeLabel.setActiveColor(SWTResourceManager.getColor(SWT.COLOR_RED));
		closeLabel.setLeftMargin(5);
		closeLabel.setRightMargin(5);
		closeLabel.setAlpha(100);
		closeLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		closeLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/close.png"));
		
		Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.marginBottom = 2;
		gl_composite_1.marginWidth = 2;
		gl_composite_1.marginHeight = 0;
		composite_1.setLayout(gl_composite_1);
		
		scrolledComposite = new ScrolledComposite(composite_1, SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		contentComposit = new Composite(scrolledComposite, SWT.NONE);
		GridLayout gl_contentComposit = new GridLayout(1, false);
		gl_contentComposit.verticalSpacing = 0;
		gl_contentComposit.marginHeight = 0;
		gl_contentComposit.marginWidth = 0;
		contentComposit.setLayout(gl_contentComposit);
		contentComposit.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		contentComposit.setBackgroundMode(SWT.INHERIT_FORCE);
		
		Composite composite_3 = new Composite(contentComposit, SWT.NONE);
		composite_3.setLayout(new GridLayout(2, false));
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_3.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		composite_3.setBackgroundMode(SWT.INHERIT_FORCE);
		
		CLabel label_1 = new CLabel(composite_3, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/television.png"));
		label_1.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		label_1.setText("评标现场监控");
		
		hideEvalBidRoomControl = new ELink(composite_3, SWT.NONE);
		hideEvalBidRoomControl.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				showEvalBidRoomControl();
			}
		});
		hideEvalBidRoomControl.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_up.png"));
		hideEvalBidRoomControl.setShowSubLine(false);
		
		evaluationRoomControlComposite = new Composite(contentComposit, SWT.NONE);
		evaluationRoomControlComposite.setLayout(new GridLayout(3, true));
		evaluationRoomControlComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_2 = new Composite(evaluationRoomControlComposite, SWT.NONE);
		composite_2.setLayout(new GridLayout(1, false));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_2.setBackground(SWTResourceManager.getColor(169, 169, 169));
		
		CLabel label = new CLabel(composite_2, SWT.NONE);
		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/camera_grey.png"));
		label.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		
		Label label_3 = new Label(contentComposit, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_5 = new Composite(contentComposit, SWT.NONE);
		composite_5.setLayout(new GridLayout(4, false));
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_5.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		composite_5.setBackgroundMode(SWT.INHERIT_FORCE);
		
		CLabel label_2 = new CLabel(composite_5, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_2.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/administrator.png"));
		label_2.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		label_2.setText("评标机位监控");
		
		ELink reduceLabel = new ELink(composite_5, SWT.NONE);
		reduceLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/view_min.png"));
		reduceLabel.setText("");
		reduceLabel.setShowSubLine(false);
		reduceLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				reduceSize();
			}
		});
		
		ELink addSizeLabel = new ELink(composite_5, SWT.NONE);
		addSizeLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/view_max.png"));
		addSizeLabel.setText("");
		addSizeLabel.setShowSubLine(false);
		addSizeLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addSize();
			}
		});
		
		hideMachineControl = new ELink(composite_5, SWT.NONE);
		hideMachineControl.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				showMachineControl();
			}
		});
		hideMachineControl.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_up.png"));
		hideMachineControl.setShowSubLine(false);
		
		machineControlComposite = new Composite(contentComposit, SWT.NONE);
		machineControlComposite.setLayout(new GridLayout(3, true));
		machineControlComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Composite composite_4 = new Composite(machineControlComposite, SWT.NONE);
		composite_4.setBackground(SWTResourceManager.getColor(169, 169, 169));
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_4.setLayout(new GridLayout(1, false));
		
		CLabel label_4 = new CLabel(composite_4, SWT.NONE);
		label_4.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/camera_grey.png"));
		label_4.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NORMAL));
		label_4.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
//		label_4.setText("正在搜索专家视频信息...");
		
		scrolledComposite.setContent(contentComposit);
		scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		// 最小化
		minLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.setMinimized(true);
			}
		});
		// 关闭
		closeLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if(null != video){
					video.closeReceiver(playerList);
				}
				expertVideoWindows = null;
				shell.close();
			}
		});
//		SWTUtil.setMouseDragListener(titleLabel);
//		SWTUtil.setCircleRegion(shell, 2);
//		SWTUtil.setCenter(shell);
		shell.setMaximized(true);
		
		// 监控界面放大或缩小操作
		machineControlComposite.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				createDefaultComposite(evaluationRoomControlComposite);
				
				if(!isCleanComposite){
					int height = getCompositeHeight(machineControlComposite);
					for(Control control : machineControlComposite.getChildren()){
						Composite composite = (Composite)control;
						((GridData)composite.getLayoutData()).heightHint = height;
					}
					machineControlComposite.layout();
					contentComposit.layout();
					scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
				}else {
					createDefaultComposite(machineControlComposite);
				}
			}
		});
		
		playerList.clear();
		if(StringUtils.equals(CommonEnum.YES, PropertiesUtils.getProperty(ConfigConstants.USE_CAMERA, "00"))){
			
			int TARGET_PORT = Integer.parseInt(PropertiesUtils.getProperty(ConfigConstants.VIDEO_PORT, "8000"));
			// 获得视频监控接口
			video = VideoEngineUtil.getInstance();
			// 接收评标专家视频
			video.receiveVideo(TARGET_PORT, new VideoListener(){
				public void getPlayer(final Player player) {
					playerList.add(player);
					Display.getDefault().syncExec(new Runnable(){
						public void run() {
							showPlayerComposite(player);
						}
					});
					player.start();
				}
			});
		}
		
		// 评标室现场监控没有内容，则不显示。
		evaluationRoomControlComposite.setData("hide", "1");
		((GridData)evaluationRoomControlComposite.getLayoutData()).heightHint = 0;
		hideEvalBidRoomControl.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down.png"));
		contentComposit.layout();
	}
	
	/**
	 * @Description: 创建默认容器
	 * @author guoyr
	 * @param parent
	 * @param isHidden
	 */
	private void createDefaultComposite(Composite parent){
		for(Control control : parent.getChildren()){
			control.dispose();
		}
		
		Composite composite_2 = new Composite(parent, SWT.NONE);
		composite_2.setLayout(new GridLayout(1, false));
		GridData gd_composite_2 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_composite_2.heightHint = getCompositeHeight(parent);
		composite_2.setLayoutData(gd_composite_2);
		composite_2.setBackground(SWTResourceManager.getColor(169, 169, 169));
		
		CLabel label = new CLabel(composite_2, SWT.NONE);
		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/camera_grey.png"));
		label.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NORMAL));
		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		
		parent.layout();
		contentComposit.layout();
		scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * @Description: 获得机位视频容器宽度
	 * @author guoyr
	 * @return
	 */
	private int getCompositeHeight(Composite composite){
		int numColumns = ((GridLayout)composite.getLayout()).numColumns;
		int width = composite.getSize().x/numColumns-(numColumns*2*5);
		int height = (int)Math.floor(width/((double)4/3));
		return height;
	}
	
	/**
	 * @Description: 放大
	 * @author guoyr
	 */
	private void addSize(){
		int numColumns = ((GridLayout)machineControlComposite.getLayout()).numColumns;
		if(--numColumns < 2){
			return;
		}
		((GridLayout)machineControlComposite.getLayout()).numColumns = numColumns;
		int height = getCompositeHeight(machineControlComposite);
		for(Control control : machineControlComposite.getChildren()){
			Composite composite = (Composite)control;
			((GridData)composite.getLayoutData()).heightHint = height;
		}
		machineControlComposite.layout();
		contentComposit.layout();
		scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * @Description: 缩小
	 * @author guoyr
	 */
	private void reduceSize(){
		int numColumns = ((GridLayout)machineControlComposite.getLayout()).numColumns;
		if(++numColumns > 4){
			return;
		}
		((GridLayout)machineControlComposite.getLayout()).numColumns = numColumns;
		int height = getCompositeHeight(machineControlComposite);
		for(Control control : machineControlComposite.getChildren()){
			Composite composite = (Composite)control;
			((GridData)composite.getLayoutData()).heightHint = height;
		}
		machineControlComposite.layout();
		contentComposit.layout();
		scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * @Description: 容器显示Player对象数据
	 * @author guoyr
	 */
	private void showPlayerComposite(Player player){
		if(isCleanComposite){
			// 清除视频容器数据
			for(Control control : machineControlComposite.getChildren()){
				control.dispose();
			}
			isCleanComposite = false;
		}
		Composite composite = new Composite(machineControlComposite, SWT.EMBEDDED);
		FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
		fillLayout.marginHeight = 1;
		fillLayout.marginWidth = 1;
		composite.setBackground(SWTResourceManager.getColor(169, 169, 169));
		composite.setLayout(fillLayout);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_composite.heightHint = getCompositeHeight(machineControlComposite);
		composite.setLayoutData(gd_composite);
		
		// 取得player中的播放视频的组件(AWT Component)
		Component comp = player.getVisualComponent();
		if(null == comp){
			// 取得player中的视频播放控制条组件
			comp = player.getControlPanelComponent();
		}
		if(null != comp){
			Frame frame = SWT_AWT.new_Frame(composite);
			frame.add(comp);
			composite.layout();
			logger.info("显示Player加载的数据成功!");
		}else{
			logger.info("显示Player加载的数据失败!");
		}
		machineControlComposite.layout();
		contentComposit.layout();
		scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * @Description: 显示/隐藏评标室现场监控
	 * @author guoyr
	 */
	private void showEvalBidRoomControl(){
		if(null == evaluationRoomControlComposite.getData("hide")){
			evaluationRoomControlComposite.setData("hide", "1");
			((GridData)evaluationRoomControlComposite.getLayoutData()).heightHint = 0;
			hideEvalBidRoomControl.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down.png"));
		}else {
			evaluationRoomControlComposite.layout();
			evaluationRoomControlComposite.setData("hide", null);
			((GridData)evaluationRoomControlComposite.getLayoutData()).heightHint = evaluationRoomControlComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
			hideEvalBidRoomControl.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_up.png"));
		}
		contentComposit.layout();
		scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * @Description: 显示/隐藏评标机位现场监控
	 * @author guoyr
	 */
	private void showMachineControl(){
		if(null == machineControlComposite.getData("hide")){// 隐藏容器
			machineControlComposite.setData("hide", "1");
			((GridData)machineControlComposite.getLayoutData()).heightHint = 0;
			hideMachineControl.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down.png"));
		}else {// 显示容器(界面加载完成是显示状态)
			machineControlComposite.setData("hide", null);
			((GridData)machineControlComposite.getLayoutData()).heightHint = machineControlComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
			hideMachineControl.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_up.png"));
		}
		contentComposit.layout();
		scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			EvalExpertVideoWindows window = new EvalExpertVideoWindows();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
