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.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
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.Label;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.dialog.ProgressDialog;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.expert.ExpertService;
import org.eclipse.swt.core.utils.GridUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class ApplyToOtherTenderPackExpertDialog extends EDialog {

	private Grid grid;
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectService tenderProjectService ;
	private ExpertService expertService;
	private EButton applyToBtn;
	private Label tipsLabel;
	private String tenderPackName = "包件";
	private Map<String, GridItem> tenderPackItemMap = new HashMap<String, GridItem>();

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public ApplyToOtherTenderPackExpertDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity) {
		super(parentShell);
		this.tenderProjectEntity = tenderProjectEntity;
	}

	/**
	 * 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);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite opComposite = new Composite(composite, SWT.NONE);
		opComposite.setLayout(new GridLayout(3, false));
		opComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, false, 1, 1));
		
		ELink checkAllLink = new ELink(opComposite, SWT.NONE);
		checkAllLink.setText(Messages.Button_selectAll);
		checkAllLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/select_all.png"));
		checkAllLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				GridUtils.checkAll(grid, 0, true);
			}
		});
		
		ELink checkBackLink = new ELink(opComposite, SWT.NONE);
		checkBackLink.setText(Messages.Button_selectInvert);
		checkBackLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/switch.png"));
		checkBackLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				GridUtils.checkAllBack(grid, 0);
			}
		});
		
		ELink cancelLink = new ELink(opComposite, SWT.NONE);
		cancelLink.setText(Messages.Button_cancel);
		cancelLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/disselect_all.png"));
		cancelLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				GridUtils.checkAll(grid, 0, false);
			}
		});
		
		grid = new Grid(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		grid.setHeaderVisible(true);
		grid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridColumn gridColumn_1 = new GridColumn(grid, SWT.CHECK);
		gridColumn_1.setText("编号");
		gridColumn_1.setWidth(100);
		gridColumn_1.setWordWrap(true);
		
		GridColumn gridColumn_2 = new GridColumn(grid, SWT.NONE);
		gridColumn_2.setText(Messages.Name);
		gridColumn_2.setWidth(150);
		gridColumn_2.setWordWrap(true);
		
		
		GridColumn gridColumn_3 = new GridColumn(grid, SWT.CENTER);
		gridColumn_3.setText("专家个数");
		gridColumn_3.setWidth(110);
		gridColumn_3.setMaxmumWidth(110);
		
		GridColumn gridColumn = new GridColumn(grid, SWT.CENTER);
		gridColumn.setText("是否已评审");
		gridColumn.setWidth(90);
		gridColumn.setMaxmumWidth(90);
		
		ESepator label = new ESepator(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_1.setLayout(new GridLayout(3, false));
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		
		tipsLabel = new Label(composite_1, SWT.NONE);
		tipsLabel.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		tipsLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tipsLabel.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		tipsLabel.setText("");
		
		applyToBtn = new EButton(composite_1, SWT.NONE);
//		applyToBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		applyToBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				applyToAction();
			}
		});
		GridData gd_button = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_button.widthHint = 90;
		applyToBtn.setLayoutData(gd_button);
		applyToBtn.setText(Messages.Button_confirm);
		applyToBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		
		EButton button = new EButton(composite_1, SWT.NONE);
		GridData gd_close_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_close_button.widthHint = 90;
		button.setLayoutData(gd_close_button);
		button.setText(Messages.Button_close);
		button.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		button.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				getShell().close();
			}
		});

		init();
		return container;
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr 
	 */
	private void init(){
		expertService = new ExpertService();
		tenderProjectService = new TenderProjectService();
		tenderPackName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUBPACKAGE, tenderProjectEntity.getTenderType());
		this.setTitle("批量复制评标专家");
		tipsLabel.setText("将当前"+tenderPackName+"的评标专家复制到选中的" + tenderPackName + "。");
		grid.setAutoHeight(true);
		grid.setAutoWidth(true);
		grid.setItemHeight(Constants.ROW_HIGHT);
		getTenderPackList();
	}


	/**
	 * @Description: 获得包件的评标信息
	 * @author guoyr 
	 */
	private void getTenderPackList(){
		List<Map<String, Object>> tenderPackList = tenderProjectService.getTenderPackExpertInfoList(tenderProjectEntity.getParentId(), tenderProjectEntity.getId());
		if(null != tenderPackList && tenderPackList.size() > 0){
			for(Map<String, Object> tenderPackMap : tenderPackList){
				GridItem gridItem = new GridItem(grid , SWT.NONE);
				String tenderId =  ObjectUtils.getObjString(tenderPackMap.get("TENDER_ID"));
				gridItem.setData("tenderPack", tenderPackMap);
				gridItem.setText(0, ObjectUtils.getObjString(tenderPackMap.get("TENDER_NO")));
				gridItem.setText(1, ObjectUtils.getObjString(tenderPackMap.get("TENDER_NAME")));
				int expertCount = ObjectUtils.getIntValue(tenderPackMap.get("EXPERT_COUNT"), 0);
				int evaluationCount = ObjectUtils.getIntValue(tenderPackMap.get("EVALUATION_COUNT"), 0);
				gridItem.setText(2, expertCount +"");
				gridItem.setText(3, evaluationCount > 0 ? "已评审" : "未评审");
				gridItem.setChecked(0, true);
				if(evaluationCount > 0){
					gridItem.setCheckVisible(0, false);
					gridItem.setChecked(0, false);
				}
				tenderPackItemMap.put(tenderId, gridItem);
			}
		}else {
			applyToBtn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 应用
	 * @author guoyr 
	 */
	private void applyToAction(){
		final List<Map<String, Object>> tenderPackList = GridUtils.getCheckedValues(grid, 0, "tenderPack", true);
		if(tenderPackList.size() > 0){
			final ProgressDialog progressDialog = new ProgressDialog(getShell(), "正在复制到其他" + tenderPackName+"...");
			new Thread(new Runnable(){
				public void run(){
					List<Expert> expertList = expertService.getExpertList(tenderProjectEntity.getId(), ExpertEnum.ALL);
					if(null != expertList && expertList.size() > 0){
						
						for(Map<String, Object> tenderPackMap : tenderPackList){
							String tenderId =  ObjectUtils.getObjString(tenderPackMap.get("TENDER_ID"));
							int expertCount = ObjectUtils.getIntValue(tenderPackMap.get("EXPERT_COUNT"), 0);
							if(expertCount > 0){
								expertService.deleteAllExperts(tenderId);
							}
							for(Expert expert : expertList){
								expert.setId(UuidUtils.getUUID());
								expert.setTenderId(tenderId);
								expert.setSignTime(null);
								expert.setSignType(null);
								expert.setSignResult(null);
							}
							expertCount = expertService.batchSave(expertList);
							setComplate(tenderId, expertCount, expertCount == expertList.size());
						}
					}
				
					Display.getDefault().syncExec(new Runnable(){
						public void run(){
							if(null != progressDialog){
								progressDialog.close();
							}
						}
					});
				}
			}).start();
			progressDialog.open();
		}else {
			MessageDialog.openShortMessage("请至少选择一个" + tenderPackName + "！");
			return ;
		}
	
	}
	
	/**
	 * @Description: 设置应用完成情况
	 * @author guoyr
	 * @param tenderId
	 * @param expertCount
	 * @param flag 
	 */
	private void setComplate(final String tenderId, final int expertCount, final boolean flag){
		Display.getDefault().syncExec(new Runnable(){
			public void run(){
				GridItem gridItem = tenderPackItemMap.get(tenderId);
				if(null != gridItem && !gridItem.isDisposed()){
					gridItem.setText(2, expertCount +"");
					if(flag){
						gridItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/complate.png"));
						gridItem.setCheckVisible(0, false);
						gridItem.setChecked(0, false);
					}else {
						gridItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/error_circle.png"));
					}
				}
			}
		});
	}

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