package com.gx.obe.management.composite;

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.nebula.widgets.grid.listener.GridCheckedListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.SashForm;
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.ColorConstants;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.common.composite.SelectTenderProjectComposite;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.Tips;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.UserService;
import com.gx.obe.web.service.expert.ExpertService;
import org.eclipse.swt.core.utils.GridUtils;
import com.gx.utils.NowTimeUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 导入项目下其他包件的专家
 * @author guoyr
 */
public class ImportEvaluationExpertComposite extends Composite {
	
	private ExpertService expertService;
	private UserService userService ;
	private TenderProjectEntity tenderProjectEntity;
//	private List<User> userList;
	private SelectTenderProjectComposite selectTenderProjectComposite;
	private Grid expertGrid;
	private ELink selectAllBtn;
	private ELink selectBackBtn;
	private Tips messageGLabel;
	private EButton importButton;
	private List<Object> curExpertUserIdList;
	
	{
		LogUtils.logClass(ImportEvaluationExpertComposite.class);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ImportEvaluationExpertComposite(Composite parent, int style, TenderProjectEntity tenderProjectEntity) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		this.tenderProjectEntity = tenderProjectEntity;
		
		messageGLabel = new Tips(this, SWT.WRAP);
		messageGLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		messageGLabel.setText("选中的专家将导入当前项目");
		Composite parentComposite = new Composite(this, SWT.NONE);
		parentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_parentComposite = new GridLayout(1, false);
		gl_parentComposite.verticalSpacing = 0;
		gl_parentComposite.horizontalSpacing = 0;
		gl_parentComposite.marginHeight = 0;
		gl_parentComposite.marginWidth = 0;
		parentComposite.setLayout(gl_parentComposite);
		parentComposite.setBackground(ResourceManager.getColor(SWT.COLOR_WHITE));
		
		Composite composite_3 = new Composite(parentComposite, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		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.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		SashForm sashForm = new SashForm(composite_3, SWT.SMOOTH);
		sashForm.setBackground(ColorConstants.BORDER_LINE_COLOR);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		sashForm.setSashWidth(1);
		
		sashForm.setData("tenderProjectEntity", tenderProjectEntity);
		selectTenderProjectComposite = new SelectTenderProjectComposite(sashForm, SWT.NONE);
		selectTenderProjectComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		Composite composite_2 = new Composite(sashForm, SWT.NONE);
		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.horizontalSpacing = 0;
		gl_composite_2.marginWidth = 0;
		gl_composite_2.verticalSpacing = 0;
		gl_composite_2.marginHeight = 0;
		composite_2.setLayout(gl_composite_2);
		
		Composite composite_1 = new Composite(composite_2, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(3, false);
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		
		ELabel titleGLabel = new ELabel(composite_1, SWT.NONE);
		titleGLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		titleGLabel.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		titleGLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/administrator.png"));
		titleGLabel.setText("评标专家");
		titleGLabel.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		selectAllBtn = new ELink(composite_1, SWT.NONE);
		selectAllBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectExpertAction(1);
			}
		});
		selectAllBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/select_all.png"));
		selectAllBtn.setText(Messages.Button_selectAll);
		
		selectBackBtn = new ELink(composite_1, SWT.NONE);
		selectBackBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectExpertAction(-1);
			}
		});
		selectBackBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/disselect_all.png"));
		selectBackBtn.setText(Messages.Button_selectInvert);
		
		ESepator sepator = new ESepator(composite_2, SWT.NONE);
		sepator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		sepator.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		expertGrid = new Grid(composite_2,  SWT.V_SCROLL);
		expertGrid.setHeaderVisible(false);
		expertGrid.setAutoWidth(true);
		expertGrid.setItemHeight(Constants.ROW_HIGHT);
		expertGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridColumn gridColumn = new GridColumn(expertGrid, SWT.CHECK);
		gridColumn.setText("专家名称");
		gridColumn.setWidth(150);
		
		GridItem gridItem = new GridItem(expertGrid, SWT.NONE);
		gridItem.setText("New Row");
		sashForm.setWeights(new int[] {6, 4});
		
		
		Composite composite = new Composite(parentComposite, SWT.NONE);
//		composite.setBackgroundMode(SWT.INHERIT_FORCE);
//		composite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		importButton = new EButton(composite, SWT.NONE);
		importButton.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/import.png"));
		GridData gd_importButton = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		importButton.setLayoutData(gd_importButton);
		importButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				importExpertAction();
			}
		});
		importButton.setText(Messages.Button_import);
		
		EButton cancel_btn = new EButton(composite, SWT.NONE);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		GridData gd_cancelBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		cancel_btn.setLayoutData(gd_cancelBtn);
		cancel_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		cancel_btn.setText(Messages.Button_close);
		
		init();
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		expertService = new ExpertService();
		messageGLabel.setText("请选择要参与 ["+tenderProjectEntity.getTenderNo()+"]"+tenderProjectEntity.getTenderName()+" 评标的专家！");
 
		curExpertUserIdList = expertService.getUserIdByTenderId(tenderProjectEntity.getId());
		// 获得该项目下的专家
//		userList = userService.getExpertByTenderId(tenderProjectEntity.getId());
		if(null == curExpertUserIdList){
			curExpertUserIdList = new ArrayList<Object>();
		}
		expertGrid.addListener(SWT.Selection, new GridCheckedListener(true));
		expertGrid.removeAll();
		// 获得选中项目信息（回调操作）
		selectTenderProjectComposite.setCallBack(new CallBack(){
			@Override
			public void callBackObject(Object obj) {
				TenderProjectEntity selectTenderProject = (TenderProjectEntity)obj;
				refreshExpertList(selectTenderProject);
				
			}
		});
	}
	
	/**
	 * @Description: 刷新专家列表
	 * @author guoyr
	 * @param tenderId
	 */
	private void refreshExpertList(TenderProjectEntity selectTenderProject){
		selectAllBtn.setEnabled(false);
		selectBackBtn.setEnabled(false);
		importButton.setEnabled(false);
		expertGrid.removeAll();
		List<AuthUser> userList = null;
		List<Expert> expertList = null;
		boolean isCurrentTenderProject = false;
		int checkCount = 0;
		if(null != selectTenderProject){
			if(selectTenderProject.getId().equals(tenderProjectEntity.getId())){
				isCurrentTenderProject = true;
			}
			
			if(null != selectTenderProject.getParentId()){
				expertList = expertService.getExpertList(selectTenderProject.getId(), null);
			}else {
				if(null != selectTenderProject.getIsPack() && selectTenderProject.getIsPack().equals(CommonEnum.YES)){
					if(null == userService){
						userService = new UserService();
					}
					userList = userService.getExpertByProjectId(selectTenderProject.getId());
				}else {
					expertList = expertService.getExpertList(selectTenderProject.getId(), null);
				}
			}
			if(null != expertList){
				for(Expert expert : expertList){
					AuthUser user = expert.getUser();
					if(null != user){
						GridItem gridItem = new GridItem(expertGrid, SWT.NONE);
						gridItem.setText(0, user.getUserName());
						gridItem.setData("user", user);
						gridItem.setData("expert", expert);
						if(null != expert.getIsLeader() &&expert.getIsLeader().equals(ExpertEnum.LEADER_YES)){
							gridItem.setData("isLeader", true);
						}
						if(!isCurrentTenderProject){
							if(curExpertUserIdList.contains(user.getId())){
								gridItem.setCheckVisible(0, false);
								gridItem.setText(0, "[已存在]"+user.getUserName());
							}else {
								checkCount ++;
							}
						}
					}
				}
			}else if(null != userList){
				for(AuthUser user : userList){
					if(null == user)continue;
					GridItem gridItem = new GridItem(expertGrid, SWT.NONE);
					gridItem.setText(0, user.getUserName());
					gridItem.setData("user", user);
					if(!isCurrentTenderProject){
						if(curExpertUserIdList.contains(user.getId())){
							gridItem.setCheckVisible(0, false);
							gridItem.setText(0, "[已存在]"+user.getUserName());
						}else {
							checkCount ++;
						}
					}
				}
			}
			if(!isCurrentTenderProject && checkCount > 0){
				selectAllBtn.setEnabled(true);
				selectBackBtn.setEnabled(true);
				importButton.setEnabled(true);
			}
		}
	}
	
	/**
	 * @Description: 全选或反选
	 * @author guoyr
	 * @param selectType
	 */
	private void selectExpertAction(int selectType){
		for(GridItem gridItem : expertGrid.getItems()){
			if(gridItem.getCheckVisible((0))){
				if(selectType == 1){
					gridItem.setChecked(0, true);
				}else{
					gridItem.setChecked(0, !gridItem.getChecked(0));
				}
			}
		}
	}
	
	/**
	 * @Description: 导入
	 * @author guoyr
	 */
	private void importExpertAction(){
		if(importButton.isEnabled()){
			boolean isExistExpertLeader = expertService.isExistExpertLeader(tenderProjectEntity.getId());
			List<Expert> importExpertList = new ArrayList<Expert>();
			for(GridItem gridItem : GridUtils.getRealCheckedItems(expertGrid, 0)){
				AuthUser user = (AuthUser)gridItem.getData("user");
				Expert expert = (Expert)gridItem.getData("expert");
				boolean isLeader = null != gridItem.getData("isLeader")? true : false;
				
				Expert _expert = new Expert();
				_expert.setId(UuidUtils.getUUID());
				_expert.setUserId(user.getId());
				_expert.setTenderId(tenderProjectEntity.getId());
				if(null != tenderProjectEntity.getParentId() && !"".equals(tenderProjectEntity.getParentId())){
					_expert.setProjectId(tenderProjectEntity.getParentId());
				}else{
					_expert.setProjectId(tenderProjectEntity.getId());
				}
				if(isExistExpertLeader){
					_expert.setIsLeader(ExpertEnum.LEADER_NO);
				}else if(isLeader){
					_expert.setIsLeader(ExpertEnum.LEADER_YES);
					isExistExpertLeader = true;
				}else{
					_expert.setIsLeader(ExpertEnum.LEADER_NO);
				}
				if(null != expert){
					_expert.setEvalBidType(expert.getEvalBidType());
					_expert.setExpertType(expert.getExpertType());
				}
				_expert.setSignType(null);
				_expert.setCreateTime(NowTimeUtils.getNowDate());
				_expert.setCreateUserId(Constants.USER.getId());
				importExpertList.add(_expert);
			}
			
			if(null != importExpertList && importExpertList.size() > 0){
				//先删除项目下的已有专家
				expertService.deleteAllExperts(tenderProjectEntity.getId());
				expertService.batchSave(importExpertList);
				cancelAction();
			}else {
				MessageDialog.openInformation(getShell(), "请先选择要导入的专家！");
			}
		}
	}
	
	private void cancelAction(){
		getShell().close();
	}
	
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
