package com.gx.obe.management.dialog;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.RoundBorderLineListener;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
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.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.ExpertLibrary;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.components.core.Constants;
import com.gx.obe.management.thread.DownloadExpertRemoveHasButSelectThread;
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 com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 更新专家
 * @author wangx
 */
public class UpdateExpertDialog extends EDialog {
	
	private Expert curExpert;
	private CallBack callBack;
	private UserService userService = new UserService();
	private ExpertService expertService = new ExpertService();
	private Grid expertGrid;
	private TenderProjectEntity tenderProject;
	private ExpertLibrary expertLibrary;
	private ProgressBar progressBar;
	private Text messageText;


	/**
	 * Create the dialog.
	 * 
	 * @param shell
	 * @param expertLibrary 
	 * @param user 
	 * @param callback 
	 * @param callBack 
	 */
	public UpdateExpertDialog(Shell shell, TenderProjectEntity tenderProject,ExpertLibrary expertLibrary, Expert expert,CallBack callBack) {
		super(shell);
		this.curExpert = expert;
		this.tenderProject = tenderProject;
		this.expertLibrary = expertLibrary;
		this.callBack = callBack;
		setTitle("选择更新专家");
	}
	
	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		
		expertGrid = new Grid(composite, SWT.H_SCROLL | SWT.V_SCROLL);
		expertGrid.setHeaderVisible(true);
		expertGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridColumn gridColumn = new GridColumn(expertGrid, SWT.CENTER);
		gridColumn.setText("姓名");
		gridColumn.setWidth(80);
		
		GridColumn accountColumn = new GridColumn(expertGrid, SWT.CENTER);
		accountColumn.setText("账号");
		accountColumn.setWidth(80);
		
		Composite messageComposite = new Composite(container, SWT.NONE);
		messageComposite.setLayout(new GridLayout(1, false));
		GridData gd_messageComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_messageComposite.heightHint = 80;
		messageComposite.setLayoutData(gd_messageComposite);
		
		messageText = new Text(messageComposite, SWT.READ_ONLY | SWT.V_SCROLL);
		messageText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		messageText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		progressBar = new ProgressBar(messageComposite, SWT.SMOOTH | SWT.INDETERMINATE);
		GridData gd_progressBar = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_progressBar.heightHint = 2;
		progressBar.setLayoutData(gd_progressBar);
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
		
		ImageButton confirmBtn = new ImageButton(composite_1, SWT.NONE);
		GridData gr_confirBtm = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		confirmBtn.setLayoutData(gr_confirBtm);
		confirmBtn.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		confirmBtn.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.BOLD));
		confirmBtn.setBackground(ColorConstants.SKIN_BG);
		confirmBtn.setActiveBgColor(ColorConstants.SKIN_ACTIVE_BG);
		gr_confirBtm.heightHint = 40;
		gr_confirBtm.widthHint = 279;
		confirmBtn.setActiveForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		confirmBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				updateExpetAction();
			}
		});
		confirmBtn.setText("更新");
		
		init();
		
		return container;
	}
	
	
	/**
	 * @Description: 初始化界面
	 * @author wangxiang
	 */
	private void init(){
		expertGrid.setItemHeight(Constants.ROW_HIGHT);
		List<Expert> expertList = expertService.getExpertList(tenderProject.getId(), ExpertEnum.ALL);
		Map<String,Expert> expertMap = new HashMap<String,Expert>();
		if(null != expertList && expertList.size() > 0){
			for(Expert expert : expertList){
				if(!expert.getId().equals(curExpert.getId())){
					expertMap.put(expert.getUser().getUserAccount(), expert);
				}
			}
		}
		
		new DownloadExpertRemoveHasButSelectThread(tenderProject, expertLibrary.getExpertLibAddress(),expertMap, new CallBack(){
			public void callBack(boolean flag, Object obj) {
				if(flag){
					final Expert expert = (Expert)obj;
					showMessage("专家"+expert.getUser().getUserName()+"更新完成");
					Display.getDefault().syncExec(new Runnable(){
						public void run() {
							if(null != expert){
								GridItem gridItem = new GridItem(expertGrid, SWT.NONE);
								AuthUser user = expert.getUser();
								gridItem.setText(0,user.getUserName());
								gridItem.setText(1,user.getUserAccount());
								gridItem.setData("expert",expert);
							}
						}
					});
				}else{
					showMessage((String)obj);
				}
			}
			public void callBackString(String string) {
				showMessage(string);
			}
			public void after() {
				showMessage("更新完成");
				Display.getDefault().syncExec(new Runnable(){
					public void run() {
						progressBar.setVisible(false);
					}
					
				});
			}
		}).start();
		
	}
	

	/**
	 * @Description: 获得选中的评标专家
	 * @author wangxiang
	 */
	private Expert getSelectionExpert(){
		Expert selectExpert = null;
		if(expertGrid.getSelectionCount() >0){
			selectExpert = (Expert)expertGrid.getSelection()[0].getData("expert");
		}
		return selectExpert;
	}
	
	/**
	 * @Description: 显示消息
	 * @author wangxiang
	 * @param msg
	 */
	private void showMessage(final String msg){
		Display.getDefault().syncExec(new Runnable(){
			public void run() {
				if(null != messageText && !messageText.isDisposed()){
					messageText.setText(messageText.getText() +"\r\n"+ msg);
					messageText.update();
					messageText.setSelection(messageText.getText().length());
				}
				
			}});
	}
	
	/**
	 * @Description: 更新专家
	 * @author wangxiang 
	 */
	private void updateExpetAction() {
		Expert selectExpert = getSelectionExpert();
		if(null != selectExpert){
			selectExpert.setProjectId(null != tenderProject.getParentId() ? tenderProject.getParentId() : tenderProject.getId());
			selectExpert.setTenderId(tenderProject.getId());
			String account = selectExpert.getUser().getUserAccount();
			AuthUser updateUser = userService.getUserByIdOrAccont(account, null);
			if(userService.updateExpertInfo(selectExpert, curExpert, updateUser)){
				callBack.after();
				this.close();
			}else{
				MessageDialog.openInformation(getShell(), "更新专家失败！");
			}
			
			callBack.after();
			this.close();
		}
	}
	

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