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.EDialog;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
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.graphics.Point;
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.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
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 EvalExpertVideoDialog extends EDialog {
	
	private IVideo video;
	private List<Player> playerList = new ArrayList<Player>();
	private TenderProjectEntity tenderProjectEntity;
	private ScrolledComposite scrolledComposite;
	private Composite evaluationRoomControlComposite;
	private Composite machineControlComposite;
	private Composite contentComposit;
	private ELink hideEvalBidRoomControl;
	private ELink hideMachineControl;
	// 日志输出
	private Logger logger = Logger.getLogger(EvalExpertVideoDialog.class);
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public EvalExpertVideoDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity) {
		super(parentShell);
		this.tenderProjectEntity = tenderProjectEntity;
	}
	
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		
		scrolledComposite = new ScrolledComposite(container, 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_composite = new GridLayout(1, false);
		gl_composite.horizontalSpacing = 0;
		gl_composite.verticalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		contentComposit.setLayout(gl_composite);
		
		Composite composite_2 = new Composite(contentComposit, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_2.setLayout(new GridLayout(3, false));
		composite_2.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
		
		CLabel label_1 = new CLabel(composite_2, 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_2, 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.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		evaluationRoomControlComposite.setLayout(new GridLayout(1, false));
		
		ESepator label = new ESepator(contentComposit, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		Composite composite_3 = new Composite(contentComposit, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_3.setLayout(new GridLayout(3, false));
		composite_3.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		composite_3.setBackgroundMode(SWT.INHERIT_FORCE);
		
		CLabel label_2 = new CLabel(composite_3, 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("评标机位现场监控");
		
		hideMachineControl = new ELink(composite_3, 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(2, true));
		machineControlComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_1 = new Composite(machineControlComposite, SWT.EMBEDDED);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_composite_1.heightHint = 226;
		composite_1.setLayoutData(gd_composite_1);
		
		Composite composite = new Composite(machineControlComposite, SWT.EMBEDDED);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		scrolledComposite.setContent(contentComposit);
		scrolledComposite.setMinSize(contentComposit.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		init();
		return container;
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		playerList.clear();
		// 清除视频容器数据
		for(Control control : machineControlComposite.getChildren()){
			control.dispose();
		}
		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();
			}
		});
		// 监控界面放大或缩小操作
		machineControlComposite.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				int height =  getCompositeWidthByMachine();
				for(Control control : machineControlComposite.getChildren()){
					Composite composite = (Composite)control;
					((GridData)composite.getLayoutData()).heightHint = height;
				}
				machineControlComposite.layout();
				contentComposit.layout();
			}
		});
		// 评标室现场监控没有内容，则不显示。
		if(evaluationRoomControlComposite.getChildren().length <= 0){
			evaluationRoomControlComposite.setData("hide", "1");
			((GridData)evaluationRoomControlComposite.getLayoutData()).heightHint = 0;
			hideEvalBidRoomControl.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down.png"));
		}
	}
	
	/**
	 * @Description: 获得机位视频容器宽度
	 * @author guoyr
	 * @return
	 */
	private int getCompositeWidthByMachine(){
		int width = machineControlComposite.getSize().x/2-20;
		int height = (int)Math.floor(width/((double)4/3));
		return height;
	}
	
	/**
	 * @Description: 容器显示Player对象数据
	 * @author guoyr
	 */
	private void showPlayerComposite(Player player){
		Composite composite = new Composite(machineControlComposite, SWT.EMBEDDED);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_composite.heightHint = getCompositeWidthByMachine();
		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));
	}
	
	/**
	 * @Description: 销毁
	 * @author guoyr
	 */
	@Override
	public boolean close() {
		if(null != video){
			video.closeReceiver(playerList);
		}
		return super.close();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(670, 470);
	}
}
