package com.gx.obe.management.composite;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.LoadingAdatper;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import com.gx.obe.ColorConstants;
import com.gx.obe.action.bean.Action;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.shell.ProgressMessageDialog;
import com.gx.obe.common.widget.thread.SyncThread;
import com.gx.obe.common.widget.utils.MenuFunctionUtils;
import com.gx.obe.components.core.Constants;
import com.gx.obe.meeting.utils.VideoMeetingUtils;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class EnterVideoMeetingComposite extends Composite {

	private TenderProjectEntity tenderProject;
	private ImageButton evalLabel;
	private ImageButton openLabel;
	private Composite composite;
	private Action projectAction;
	private MenuFunctionUtils menuFunctionUtils = new  MenuFunctionUtils();
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public EnterVideoMeetingComposite(Composite parent, Action projectAction, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));
		this.projectAction = projectAction;
		tenderProject = projectAction.getTenderProject();
		
		composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.horizontalSpacing = 30;
		composite.setLayout(gl_composite);
		
		openLabel = createVideoButton(Messages.OpeningMeeting);
		openLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		
		openLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				startOpenMeeting();
			}
		});
		menuFunctionUtils.addFunction(openLabel, "OPENING_MEETING");
		evalLabel = createVideoButton(Messages.EvaluationMeeting);
		evalLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
		evalLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				startEvalMeeting();
			}
		});
		menuFunctionUtils.addFunction(evalLabel, "EVALUATION_MEETING");

		init();
		
	}
	
	private void init() {
		openLabel.setEnabled(false);
		evalLabel.setEnabled(false);
		menuFunctionUtils.initFunctionAuthority(projectAction.getMenu());
		new SyncThread(new SyncThread.Runnable(){
			public void run() {
				TenderProjectService tenderProjectService = new TenderProjectService();
				tenderProject = tenderProjectService.getById(tenderProject.getId());
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if(!openLabel.isDisposed()) {
							if(StringUtils.isNotEmpty(tenderProject.getBidMeeting())) {
								openLabel.setEnabled(true);
							}else {
								openLabel.setSubText(Messages.Start_no);
							}
						}
						if(!evalLabel.isDisposed()) {
							if(StringUtils.isNotEmpty(tenderProject.getEvaluationMeeting())) {
								evalLabel.setEnabled(true);
							}else {
								evalLabel.setSubText(Messages.Start_no);
							}
						}
					}
				});
			}
		}).start();
	}

	private ImageButton createVideoButton(String text) {
		ImageButton imageButton = new ImageButton(composite, SWT.BOTTOM);
		imageButton.setText(text);
		imageButton.setFont(SWTResourceManager.getFont("微软雅黑", 15, SWT.BOLD));
		imageButton.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/camera_grey.png"));
		imageButton.setArcWidth(10);
//		imageButton.setBorderColor(ColorConstants.BORDER_LINE_COLOR);
		imageButton.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
		imageButton.setActiveSurroundColor(SWTResourceManager.getColor(77, 163, 19));// 橙色
		imageButton.setSurroundAlpha(100);
		imageButton.setSurroundMargin(20);
//		imageButton.setActiveBorderColor(SWTResourceManager.getColor(60, 138, 226));
		imageButton.setActiveForeground(SWTResourceManager.getColor(100, 100, 100));
//		imageButton.setActiveBgColor(SWTResourceManager.getColor(205, 225, 205));
		imageButton.setAlpha(120);
		imageButton.setImageText(text);
//		imageButton.setSubImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/videoOpen.png"));
//		imageButton.setSubImageHorAlign(SWT.RIGHT);
//		imageButton.setSubImageVerAlign(SWT.BOTTOM);
		imageButton.setLeftMargin(10);
		imageButton.setRightMargin(10);
		imageButton.setSubTextSurroundAlpha(180);
		imageButton.setSubTextVerAlign(SWT.TOP);
		imageButton.setSubTextHorAlign(SWT.RIGHT);
		imageButton.setSubTextTopMargin(2);
		imageButton.setSubTextRightMargin(2);
		imageButton.setSubTextFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		imageButton.setForeground(ColorConstants.FONT_BLACK_COLOR);
		imageButton.setActiveForeground(SWTResourceManager.getColor(77, 163, 19));
//		imageButton.setMinWidth(180);
//		imageButton.setMinHeight(180);
		return imageButton;
	}
	/**
	 * 启动视频会议
	 */
	private void startOpenMeeting(){
		if(!openLabel.getEnabled())return;
		if(!MessageDialog.openConfirm(getShell(), Messages.Tips_enterOpeningMeeting)) {
			return;
		}
		openLabel.setEnabled(false);
		final ProgressMessageDialog messageProgress = new ProgressMessageDialog(getShell(), true, -1, new LoadingAdatper() {
			public void finish(boolean flag) {
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if(!openLabel.isDisposed()) {
							openLabel.setEnabled(true);
						}
					}
				});
			}
			
		});
		new Thread(new Runnable(){
			public void run() {
				VideoMeetingUtils.enterMeeting(tenderProject, Constants.USER, VideoMeetingUtils.ROLE_MANAGER, true, messageProgress);
			}
		}).start();
		messageProgress.setTitle(Messages.Tips_starting);
		messageProgress.setOperateVisible(false);
		messageProgress.open();
	}
	
	
	/**
	 * @Description: 启动视频服务：默认房主为FX_00015
	 * @author huangz
	 */
	private void startEvalMeeting(){
		if(!evalLabel.getEnabled())return;
		if(!MessageDialog.openConfirm(getShell(), Messages.Tips_enterEvaluationMeeting)) {
			return;
		}
		evalLabel.setEnabled(false);
		final ProgressMessageDialog messageProgress = new ProgressMessageDialog(getShell(), true, -1, new LoadingAdatper() {
			public void finish(boolean flag) {
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if(!evalLabel.isDisposed()) {
							evalLabel.setEnabled(true);
						}
					}
				});
			}
			
		});
		
		new Thread(new Runnable(){
			public void run() {
				VideoMeetingUtils.enterMeeting(tenderProject, Constants.USER, VideoMeetingUtils.ROLE_MANAGER, false, messageProgress);
			}
		}).start();
		messageProgress.setTitle(Messages.Tips_starting);
		messageProgress.setOperateVisible(false);
		messageProgress.open();
	}
	

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

}
