package com.gx.obe.management.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

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.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.evaluation.EvaluationRoom;
import com.gx.obe.web.service.ProjectEvaluationRoomService;
import com.gx.obe.web.service.evaluation.EvaluationRoomService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class SetEvalBidRoomDialog extends EDialog {
	
	private EvaluationRoomService evaluationRoomService;
	private ProjectEvaluationRoomService projectEvaluationRoomService;
	private String tenderId;// 项目ID
	// 当前设置评标室ID(null表示新增，否则修改操作。)
	private String selectEvaluationAreaId;// 选中的评标区ID
	private List<String> existEvalRoomIdList = new ArrayList<String>();
	private Combo evaluationAreaCombo;
	private Grid evaluationRoomGrid;// 未分配的评标室Grid
	private Grid existEvalBidRoomGrid;// 已分配的评标室Grid
	private EButton addBtn;
	private EButton delBtn;
	private EButton addAllBtn;
	private EButton delAllBtn;
	private EButton complate_btn;
	
	{
		LogUtils.logClass(SetEvalBidRoomDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SetEvalBidRoomDialog(Shell parentShell, String tenderId) {
		super(parentShell);
		this.tenderId = tenderId;
	}

	/**
	 * 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.horizontalSpacing = 0;
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
//		composite.setBackgroundImage(ResourceManager.getPluginImage(SWTCoreActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite.setLayout(new GridLayout(3, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label = new CLabel(composite, SWT.NONE);
		label.setText("评标区：");
		
		evaluationAreaCombo = new Combo(composite, SWT.READ_ONLY);
		evaluationAreaCombo.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		evaluationAreaCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblsbd = new Label(composite, SWT.WRAP);
		lblsbd.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		lblsbd.setText("请先选择评标区，再选择添加评标室"+"！");
		
		Label label_3 = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(5, false);
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.horizontalSpacing = 0;
		gl_composite_1.marginHeight = 0;
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_2 = new Composite(composite_1, SWT.NONE);
		composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_2.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		composite_2.setLayout(new GridLayout(2, false));
		
		CLabel label_1 = new CLabel(composite_2, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_1.setText("未分配评标室");
		label_1.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/edit_input.png"));
		label_1.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		
		Label label_4 = new Label(composite_1, SWT.SEPARATOR | SWT.VERTICAL);
		label_4.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 3));
		
		Composite composite_4 = new Composite(composite_1, SWT.NONE);
		composite_4.setLayout(new GridLayout(1, false));
		composite_4.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 3));
		
		addBtn = new EButton(composite_4, SWT.NONE);
		addBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addEvalBidRoomAction();
			}
		});
		addBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		addBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/control.png"));
		addBtn.setText("添加>");
		
		delBtn = new EButton(composite_4, SWT.NONE);
		delBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delEvalBidRoomAction();
			}
		});
		delBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		delBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/control-180.png"));
		delBtn.setText("<删除");
		
		addAllBtn = new EButton(composite_4, SWT.NONE);
		addAllBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		addAllBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addAllEvalBidRoomAction();
			}
		});
		addAllBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/control-double.png"));
		addAllBtn.setText("全部添加>>");
		
		delAllBtn = new EButton(composite_4, SWT.NONE);
		delAllBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		delAllBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delAllEvalBidRoomAction();
			}
		});
		delAllBtn.setText("<<全部删除");
		
		complate_btn = new EButton(composite_4, SWT.NONE);
		complate_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		complate_btn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		complate_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				closeAction();
			}
		});
		complate_btn.setText(Messages.Button_close);
		
		Label label_5 = new Label(composite_1, SWT.SEPARATOR | SWT.VERTICAL);
		label_5.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 3));
		
		Composite composite_6 = new Composite(composite_1, SWT.NONE);
		composite_6.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_6.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		composite_6.setLayout(new GridLayout(1, false));
		
		CLabel label_2 = new CLabel(composite_6, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_2.setText("已分配评标室");
		label_2.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/edit_input.png"));
		label_2.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		
		Label label_6 = new Label(composite_1, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_6.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_7 = new Label(composite_1, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_3 = new Composite(composite_1, SWT.NONE);
		composite_3.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		evaluationRoomGrid = new Grid(composite_3, SWT.V_SCROLL);
		
		GridColumn gridColumn = new GridColumn(evaluationRoomGrid, SWT.NONE);
		gridColumn.setText("New Column");
		gridColumn.setWidth(150);
		
		Composite composite_5 = new Composite(composite_1, SWT.NONE);
		composite_5.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		existEvalBidRoomGrid = new Grid(composite_5, SWT.V_SCROLL);
		
		GridColumn gridColumn_1 = new GridColumn(existEvalBidRoomGrid, SWT.NONE);
		gridColumn_1.setText("New Column");
		gridColumn_1.setWidth(150);
		
		init();
		return container;
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		projectEvaluationRoomService = new ProjectEvaluationRoomService();
		evaluationRoomService = new EvaluationRoomService();
		evaluationRoomGrid.setItemHeight(Constants.ROW_HIGHT);
		existEvalBidRoomGrid.setItemHeight(Constants.ROW_HIGHT);
		evaluationAreaCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectEvaluationAreaAction();
			}
		});
	}
	
	/**
	 * @Description: 评标区Combo选中操作
	 * @author guoyr
	 */
	private void selectEvaluationAreaAction(){
		selectEvaluationAreaId = ObjectUtils.getObjString(WidgetUtils.getComboSelection(evaluationAreaCombo));
		getEvalBidRoomListByAreaId();
		refreshEvalBidRoom();
		refreshBtnStatus();
	}
	
	/**
	 * @Description: 已分配的评标室
	 * @author guoyr
	 */
	private void getEvalBidRoomListByAreaId(){
		existEvalBidRoomGrid.removeAll();
		List<EvaluationRoom> existEvalBidRoomList = evaluationRoomService.getProjectEvaluationRoomList(tenderId, selectEvaluationAreaId);
		if(null != existEvalBidRoomList && existEvalBidRoomList.size() > 0){
			for(EvaluationRoom evaluationRoom : existEvalBidRoomList){
				GridItem gridItem = new GridItem(existEvalBidRoomGrid, SWT.NONE);
				gridItem.setText(0, ObjectUtils.getObjString(evaluationRoom.getEvalRoomName()));
				gridItem.setData("evaluationRoom", evaluationRoom);
				existEvalRoomIdList.add(evaluationRoom.getId());
			}
			existEvalBidRoomGrid.select(0);
		}
	}
	
	/**
	 * @Description: 未分配的有效评标室
	 * @author guoyr
	 */
	private void refreshEvalBidRoom(){
		evaluationRoomGrid.removeAll();
		List<EvaluationRoom> evaluationRoomList = evaluationRoomService.getEvaluatoinRoomList(selectEvaluationAreaId);
		if(null != evaluationRoomList && evaluationRoomList.size() > 0){
			for(EvaluationRoom evaluationRoom : evaluationRoomList){
				if(null != existEvalRoomIdList && existEvalRoomIdList.contains(evaluationRoom.getId())){
					continue;
				}
				GridItem gridItem = new GridItem(evaluationRoomGrid, SWT.NONE);
				gridItem.setText(0, ObjectUtils.getObjString(evaluationRoom.getEvalRoomName()));
				gridItem.setData("evaluationRoom", evaluationRoom);
			}
			evaluationRoomGrid.select(0);
		}
	}
	
	/**
	 * @Description: 添加
	 * @author guoyr
	 */
	private void addEvalBidRoomAction(){
		if(!addBtn.getEnabled()){
			return ;
		}
		for(GridItem item : evaluationRoomGrid.getSelection()){
			EvaluationRoom  evaluationRoom = (EvaluationRoom)item.getData("evaluationRoom");
			if(null != evaluationRoom){
				
				ProjectEvaluationRoom projectEvaluationRoom = new ProjectEvaluationRoom();
				projectEvaluationRoom.setTenderId(tenderId);
				projectEvaluationRoom.setEvaluationRoomId(evaluationRoom.getId());
				if(projectEvaluationRoomService.saveOrUpdate(projectEvaluationRoom)){
					projectEvaluationRoom.setIsPrimaryRoom(ExpertEnum.LEADER_NO);
					GridItem gridItem = new GridItem(existEvalBidRoomGrid, SWT.NONE);
					gridItem.setText(item.getText());
					gridItem.setData("evaluationRoom", evaluationRoom);
					existEvalBidRoomGrid.setSelection(new GridItem[]{gridItem});
					item.dispose();
				}
				
			}
		}
		if(evaluationRoomGrid.getItemCount() > 0){
			evaluationRoomGrid.setSelection(0);
		}
		refreshBtnStatus();
	}
	
	/**
	 * @Description: 删除
	 * @author guoyr
	 */
	private void delEvalBidRoomAction(){
		if(!delBtn.getEnabled()){
			return ;
		}
		for(GridItem item : existEvalBidRoomGrid.getSelection()){
			EvaluationRoom  evaluationRoom = (EvaluationRoom)item.getData("evaluationRoom");
			if(null != evaluationRoom){
				if(projectEvaluationRoomService.deleteByTenderIdAndEvaluationRoomId(tenderId,evaluationRoom.getId()) > 0){
					GridItem gridItem = new GridItem(evaluationRoomGrid, SWT.NONE);
					gridItem.setText(item.getText());
					gridItem.setData("evaluationRoom", evaluationRoom);
					evaluationRoomGrid.setSelection(new GridItem[]{gridItem});
					item.dispose();
				}
			}
		}
		if(existEvalBidRoomGrid.getItemCount() > 0){
			existEvalBidRoomGrid.setSelection(0);
		}
		refreshBtnStatus();
	}
	
	/**
	 * @Description: 全部添加
	 * @author guoyr
	 */
	private void addAllEvalBidRoomAction(){
		if(!addAllBtn.getEnabled()){
			return ;
		}
		List<ProjectEvaluationRoom> projectEvaluationRoomList = new ArrayList<ProjectEvaluationRoom>();
		for(GridItem item : evaluationRoomGrid.getItems()){
			EvaluationRoom  evaluationRoom = (EvaluationRoom)item.getData("evaluationRoom");
			if(null != evaluationRoom){
				GridItem gridItem = new GridItem(existEvalBidRoomGrid, SWT.NONE);
				gridItem.setText(item.getText());
				gridItem.setData("evaluationRoom", evaluationRoom);
				existEvalBidRoomGrid.setSelection(new GridItem[]{gridItem});
				ProjectEvaluationRoom projectEvaluationRoom = new ProjectEvaluationRoom();
				projectEvaluationRoom.setTenderId(tenderId);
				projectEvaluationRoom.setEvaluationRoomId(evaluationRoom.getId());
				projectEvaluationRoomList.add(projectEvaluationRoom);
				projectEvaluationRoom.setIsPrimaryRoom(ExpertEnum.LEADER_NO);
				item.dispose();
			}
		}
		if(projectEvaluationRoomList.size() > 0){
			projectEvaluationRoomService.batchSave(projectEvaluationRoomList);
		}
		refreshBtnStatus();
	}
	
	/**
	 * @Description: 全部删除
	 * @author guoyr
	 */
	private void delAllEvalBidRoomAction(){
		if(!delAllBtn.getEnabled()){
			return ;
		}
		for(GridItem item : existEvalBidRoomGrid.getItems()){
			EvaluationRoom  evaluationRoom = (EvaluationRoom)item.getData("evaluationRoom");
			if(null != evaluationRoom){
				if(projectEvaluationRoomService.deleteByTenderIdAndEvaluationRoomId(tenderId, evaluationRoom.getId()) > 0){
					GridItem gridItem = new GridItem(evaluationRoomGrid, SWT.NONE);
					gridItem.setText(item.getText());
					gridItem.setData("evaluationRoom", evaluationRoom);
					evaluationRoomGrid.setSelection(new GridItem[]{gridItem});
					item.dispose();
				}
			}
		}
		refreshBtnStatus();
	}
	
	/**
	 * @Description: 刷新操作按扭状态
	 * @author guoyr
	 */
	private void refreshBtnStatus(){
		addAllBtn.setEnabled(false);
		addBtn.setEnabled(false);
		delBtn.setEnabled(false);
		delAllBtn.setEnabled(false);
		if(evaluationRoomGrid.getItemCount() > 0){
			addAllBtn.setEnabled(true);
			if(evaluationRoomGrid.getSelectionCount() > 0){
				addBtn.setEnabled(true);
			}
		}
		
		if(existEvalBidRoomGrid.getItemCount() > 0){
			delAllBtn.setEnabled(true);
			if(existEvalBidRoomGrid.getSelectionCount() > 0){
				delBtn.setEnabled(true);
			}
		}
	}
	
	/**
	 * @Description: 关闭
	 * @author guoyr
	 */
	private void closeAction(){
		getShell().close();
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(557, 446);
	}
}
