package com.gx.obe.management.composite;

import java.util.List;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridColumnGroup;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.nebula.widgets.grid.listener.GridSelectionListener;
import org.eclipse.swt.core.widgets.ELink;
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.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.business.entity.ProjectEvaluationRoom;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.management.dialog.SetEvalBidRoomDialog;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.evaluation.EvaluationRoom;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.ProjectEvaluationRoomService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 评标室管理
 * @author guoyr
 */
public class EvaluationRoomManagerComposite extends Composite {

	private ProjectEvaluationRoomService projectEvaluationRoomService ;
	private String tenderId;
	private ProjectEvaluationRoom curEvaluationRoom;
	private Grid evaluationRoom;
	private ELink add_evaluation;
	private ELink del_evaluation;
	private ELink config_main;
	private ImageResolver imageResolver = new ImageResolver();
	
	{
		LogUtils.logClass(EvaluationRoomManagerComposite.class);
	}
	public EvaluationRoomManagerComposite(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		setLayout(gridLayout);
		tenderId = null != parent.getData("tenderProjectEntity") ? ((TenderProjectEntity) parent.getData("tenderProjectEntity")).getId() : "";
		
		Composite composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		composite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/bg_searchBar.png"));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setBackground(SWTResourceManager.getColor(255, 255, 255));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_2.setLayout(new GridLayout(3, true));
		
		add_evaluation = new ELink(composite_2, SWT.NONE);
		add_evaluation.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		add_evaluation.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		add_evaluation.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addEvaluationRoom();
			}
		});
		add_evaluation.setText("管理评标室");
		
		del_evaluation = new ELink(composite_2, SWT.NONE);
		del_evaluation.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		del_evaluation.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		del_evaluation.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				deleteEvaluationRoom();
			}
		});
		del_evaluation.setText("删除评标室");
		
		config_main = new ELink(composite_2, SWT.NONE);
		config_main.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		config_main.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				setMainArea();
			}
		});
		config_main.setText("设置主评标室");
		
		Composite composite_1 = new Composite(this, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.marginHeight = 1;
		gl_composite_1.marginWidth = 1;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_1.setBackground(SWTResourceManager.getColor(211, 211, 211));
		
		Composite composite_3 = new Composite(composite_1, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.marginHeight = 1;
		gl_composite_3.marginWidth = 1;
		composite_3.setLayout(gl_composite_3);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		evaluationRoom = new Grid(composite_3, SWT.H_SCROLL | SWT.V_SCROLL);
		evaluationRoom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		evaluationRoom.setHeaderVisible(true);
		
		GridColumn gridColumn = new GridColumn(evaluationRoom, SWT.CENTER);
		gridColumn.setText("编号");
		gridColumn.setMaxmumWidth(50);
		gridColumn.setMinimumWidth(40);
		
		GridColumn gridColumn_1 = new GridColumn(evaluationRoom, SWT.NONE);
		gridColumn_1.setText("评标室名称");
		gridColumn_1.setMinimumWidth(100);
		gridColumn_1.setMaxmumWidth(200);
		gridColumn_1.setWordWrap(true);
		
		GridColumn gridColumn_2 = new GridColumn(evaluationRoom, SWT.CENTER);
		gridColumn_2.setText("评标室类型");
		gridColumn_2.setWidth(80);
		gridColumn_2.setMaxmumWidth(80);
		
		GridColumn gridColumn_3 = new GridColumn(evaluationRoom, SWT.CENTER);
		gridColumn_3.setText("评标区名称");
		gridColumn_3.setMaxmumWidth(250);
		gridColumn_3.setMinimumWidth(100);
		gridColumn_3.setWordWrap(true);
		
		GridColumn gridColumn_4 = new GridColumn(evaluationRoom, SWT.NONE);
		gridColumn_4.setText("评标区地址");
		gridColumn_4.setMinimumWidth(150);
		gridColumn_4.setWordWrap(true);
		
		GridColumnGroup gridColumnGroup_1 = new GridColumnGroup(evaluationRoom, SWT.NONE);
		gridColumnGroup_1.setText("门禁系统");

		GridColumn gridColumn_5 = new GridColumn(gridColumnGroup_1, SWT.CENTER);
		gridColumn_5.setText("IP");
		gridColumn_5.setWidth(120);
		gridColumn_5.setMaxmumWidth(120);
		
		GridColumn gridColumn_6 = new GridColumn(gridColumnGroup_1, SWT.CENTER);
		gridColumn_6.setText("端口");
		gridColumn_6.setWidth(50);
		gridColumn_6.setMaxmumWidth(60);
		
		GridItem gridItem = new GridItem(evaluationRoom, SWT.NONE);
		gridItem.setText("");
		
		GridColumn gridColumnNull = new GridColumn(evaluationRoom, SWT.NONE);
		gridColumnNull.setText("");
		gridColumnNull.setMinimumWidth(150);
		gridColumnNull.setWordWrap(true);
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		projectEvaluationRoomService = new ProjectEvaluationRoomService();
		evaluationRoom.setItemHeight(Constants.ROW_HIGHT);
		evaluationRoom.setAutoHeight(true);
		evaluationRoom.setAutoWidth(true);
		refreshEvaluationRoom();
		// 设置评标室
		evaluationRoom.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void leftMouseButtonDown(GridItem item) {
				selectEvaluationAction();
			}
		});
	}
	
	/**
	 * @Description: 刷新评标室列表
	 * @author guoyr
	 */
	private void refreshEvaluationRoom(){
		evaluationRoom.removeAll();
		List<ProjectEvaluationRoom> projectEvaluationRoomList = projectEvaluationRoomService.getProjectEvalRoomList(tenderId);
		if(null != projectEvaluationRoomList && projectEvaluationRoomList.size() > 0){
			int index = 1;
			for(ProjectEvaluationRoom projectEvaluationRoom : projectEvaluationRoomList){
				GridItem gridItem = new GridItem(evaluationRoom, SWT.NONE);
				gridItem.setText(0, ObjectUtils.getObjString(index));
				setEvaluationRoomInfo(gridItem, projectEvaluationRoom);
				
				}
				index++;
		}
	}
	
	/**
	 * @Description: 设置评标室信息
	 * @author guoyr
	 * @param gridItem
	 * @param evaluationRoom
	 */
	private void setEvaluationRoomInfo(GridItem gridItem, ProjectEvaluationRoom projectEvaluationRoom){
		// 评标室
		EvaluationRoom evaluationRoom = projectEvaluationRoom.getEvalBidRoom();
		if(null != evaluationRoom){
			gridItem.setText(1, ObjectUtils.getObjString(evaluationRoom.getEvalRoomName()));
			gridItem.setText(2, ObjectUtils.equals(evaluationRoom.getEvalRoomType(), CommonEnum.NO) ? "异地" : "本地");
			if(StringUtils.equals(ExpertEnum.LEADER_YES, projectEvaluationRoom.getIsPrimaryRoom())){
				gridItem.setImage(4, imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/video.png"));
			}else{
				gridItem.setImage(4, null);
			}
		}
		gridItem.setData("projectEvaluationRoom", evaluationRoom);
	}
	
	/**
	 * @Description: 选中事件
	 * @author guoyr
	 */
	private void selectEvaluationAction(){
		curEvaluationRoom = null;
		if(null != evaluationRoom && !evaluationRoom.isDisposed() && evaluationRoom.getSelectionCount() > 0){
			curEvaluationRoom = (ProjectEvaluationRoom) evaluationRoom.getSelection()[0].getData("projectEvaluationRoom");
		}
		del_evaluation.setEnabled(false);
		config_main.setEnabled(false);
		if(null != curEvaluationRoom){
			del_evaluation.setEnabled(true);
			if(ObjectUtils.equals(curEvaluationRoom.getIsPrimaryRoom(), ExpertEnum.LEADER_NO)){
				config_main.setEnabled(true);
			}
		}
	}
	
	/**
	 * @Description: 新增
	 * @author guoyr
	 */
	private void addEvaluationRoom(){
		if(!add_evaluation.getEnabled()){
			return ;
		}
		SetEvalBidRoomDialog dialog = new SetEvalBidRoomDialog(getShell(), tenderId);
		dialog.setTitle("管理评标室");
		dialog.open();
		refreshEvaluationRoom();
	}
	
	/**
	 * @Description: 删除
	 * @author guoyr
	 */
	private void deleteEvaluationRoom(){
		if(!del_evaluation.getEnabled() || null == curEvaluationRoom){
			return ;
		}
		if(MessageDialog.openConfirm(getShell(), "确认要删除当前评标室吗？")){
			if(projectEvaluationRoomService.deleteById(curEvaluationRoom.getId())){
				GridItem gridItem = evaluationRoom.getSelection()[0];
				gridItem.dispose();
				int selectIndex = evaluationRoom.getSelectionIndex();
				if(evaluationRoom.getItemCount() > 0){
					if(selectIndex <= evaluationRoom.getItemCount()){
						evaluationRoom.setSelection(evaluationRoom.getItemCount() - 1);
					}else {
						evaluationRoom.setSelection(selectIndex);
					}
					selectIndex = 1;
					for(GridItem item : evaluationRoom.getItems()){
						item.setText(0, ObjectUtils.getObjString(selectIndex++));
					}
					refreshEvaluationRoom();
				}
				selectEvaluationAction();
			}
		}
	}
	
	/**
	 * @Description: 设置主评区
	 * @author guoyr
	 */
	private void setMainArea(){
		if(!config_main.getEnabled()){
			return ;
		}
		if(null != curEvaluationRoom && ObjectUtils.equals(curEvaluationRoom.getIsPrimaryRoom(), ExpertEnum.LEADER_NO)){
			GridItem selectItem = evaluationRoom.getSelection()[0];
			// 先将原来的主评室撤销
			for(GridItem gridItem : evaluationRoom.getItems()){
				ProjectEvaluationRoom projectEvaluationRoom = (ProjectEvaluationRoom)gridItem.getData("projectEvaluationRoom");
				if(null != projectEvaluationRoom && ObjectUtils.equals(projectEvaluationRoom.getIsPrimaryRoom(), ExpertEnum.LEADER_YES)){
					projectEvaluationRoom.setIsPrimaryRoom(ExpertEnum.LEADER_NO);
					if(projectEvaluationRoomService.updateAssignProperty(projectEvaluationRoom, new String[]{"id","isPrimaryRoom"})){
						gridItem.setData("projectEvaluationRoom", projectEvaluationRoom);
						gridItem.setImage(1, null);
						break;
					}
				}
			}
			// 设置当前为主评室
			curEvaluationRoom.setIsPrimaryRoom(ExpertEnum.LEADER_YES);
			if(projectEvaluationRoomService.updateAssignProperty(curEvaluationRoom, new String[]{"id","isPrimaryRoom"})){
				selectItem.setData("projectEvaluationRoom", curEvaluationRoom);
				selectItem.setImage(1, imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/video.png"));
				config_main.setEnabled(false);
			}
		}
	}
	
	@Override
	protected void checkSubclass() {
	}
}
