
package com.gx.obe.evaluation.step.dialog;

import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.Element;
import org.eclipse.core.runtime.Platform;
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.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
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.graphics.Rectangle;
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.ColorConstants;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.evaluation.step.listener.BidRegulationsListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.BidRegulations;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.BidRegulationsService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 选择废标条款
 * @author guoyr
 */
public class SelectBidRegulationsDialog extends EDialog {
	{
		LogUtils.logClass(SelectBidRegulationsDialog.class);
	}
	private Grid bidRegulations_grid;
	private GridColumn gridColumn;
	private Composite composite_1;
	private Composite composite_7;
	private Composite composite_8;
	private TenderProjectEntity tenderProject;
	private BidRegulationsService bidRegulationsService;
	private Composite composite;
	private Composite composite_2;
	private Combo bidRegulationsCombo;
	private List<BidRegulations> bidRegulationsList;
	private String blankString = "--所有条款--";
	private Set<String> shortNameSet = new HashSet<String>();
	private EButton saveBtn;
	private Label label;
	private BidRegulationsListener listener;
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SelectBidRegulationsDialog(Shell parentShell, TenderProjectEntity tenderProject, BidRegulationsListener listener) {
		super(parentShell);
		setTitle(MessageFormat.format(Messages.Button_selectFormat, Messages.Button_votoClause));
		this.tenderProject = tenderProject;
		this.listener = listener;
	}
	

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
//		GridLayout gridLayout = (GridLayout) parent.getLayout();
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.horizontalSpacing = 0;
		gl_container.verticalSpacing = 0;
		container.setLayout(gl_container);
		
		composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		label = new Label(composite, SWT.NONE);
		label.setText("情形事项：");
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		
		bidRegulationsCombo = new Combo(composite, SWT.READ_ONLY);
		bidRegulationsCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				selectComboAction();
			}
		});
		GridData gd_combo = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_combo.widthHint = 150;
		bidRegulationsCombo.setLayoutData(gd_combo);
		
		composite_1 = new Composite(container, SWT.NONE);
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_1.widthHint = 496;
		composite_1.setLayoutData(gd_composite_1);
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.verticalSpacing = 0;
		gl_composite_1.marginHeight = 0;
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);
		

		composite_8 = new Composite(composite_1, SWT.NONE);
		composite_8.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite_8 = new GridLayout(1, false);
		gl_composite_8.verticalSpacing = 1;
		gl_composite_8.marginHeight = 1;
		gl_composite_8.marginWidth = 1;
		composite_8.setLayout(gl_composite_8);
		composite_8.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		composite_7 = new Composite(composite_8, SWT.NONE);
		composite_7.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite_7 = new GridLayout(1, false);
		gl_composite_7.marginHeight = 0;
		gl_composite_7.marginWidth = 0;
		composite_7.setLayout(gl_composite_7);
		
		bidRegulations_grid = new Grid(composite_7, SWT.V_SCROLL);
		bidRegulations_grid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		bidRegulations_grid.setHeaderVisible(true);
		
		GridColumn firstGridColumn = new GridColumn(bidRegulations_grid, SWT.CHECK | SWT.CENTER);
		firstGridColumn.setWidth(20);
		firstGridColumn.setText(Messages.Button_select);
//		firstGridColumn.setMaxmumWidth(20);
		
		GridColumn sortGridColumn = new GridColumn(bidRegulations_grid, SWT.CENTER);
		sortGridColumn.setText(Messages.Num);
		sortGridColumn.setWidth(40);
		sortGridColumn.setMaxmumWidth(60);
		
		GridColumn gridColumn_1 = new GridColumn(bidRegulations_grid, SWT.WRAP | SWT.CENTER);
		gridColumn_1.setWordWrap(true);
		gridColumn_1.setText("情形事项");
		gridColumn_1.setWidth(100);
//		gridColumn_1.setMaxmumWidth(100);
		
		gridColumn = new GridColumn(bidRegulations_grid, SWT.WRAP);
		gridColumn.setWordWrap(true);
		gridColumn.setText(Messages.Button_votoClause);
		gridColumn.setWidth(500);
		
		composite_2 = new Composite(container, SWT.NONE);
		composite_2.setLayout(new GridLayout(2, false));
		composite_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		
		saveBtn = new EButton(composite_2, SWT.NONE);
		saveBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		saveBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveAction();
			}
		});
		saveBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		saveBtn.setText(Messages.Button_ok);
		
		EButton cancel_btn = new EButton(composite_2, SWT.NONE);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				getShell().close();
			}
		});
		cancel_btn.setText(Messages.Button_close);

		init();
		return container;
	}
	
	/**
	 * @Description: 数据初始化
	 * @author guoyr 
	 */
	private void init(){
		bidRegulationsService = new BidRegulationsService();
		bidRegulations_grid.setAutoHeight(true);
		bidRegulations_grid.setWordWrapHeader(true);
		bidRegulations_grid.setItemHeight(Constants.ROW_HIGHT);
		// 添选中事件和右键菜单事件
		bidRegulationsList = bidRegulationsService.getBidRegulationsListByTenderId(null != tenderProject.getParentId() ? tenderProject.getParentId() : tenderProject.getId());
		if(null == bidRegulationsList || bidRegulationsList.size() == 0){
			bidRegulationsList = loadDefaultWasteClause();
		}
		
		setComboData();
		refresBidRegulationsTree(null);
	}
	
	/**
	 * @Description: 加载默认废标条款
	 * @author guoyr
	 * @return 
	 */
	private List<BidRegulations> loadDefaultWasteClause(){
		List<BidRegulations> bidRegulationsList = new ArrayList<BidRegulations>();
		File defaultFile = new File(Platform.getInstallLocation().getURL().getPath()+File.separator + FileConstants.DEFAULT_WASTE_CLAUSE_FOLDER+File.separator + "wasteClause.xml");
		if(null != defaultFile && defaultFile.exists()){
			Document bidRegulationsDoc = XmlUtils.getXmlDocument(defaultFile.getAbsolutePath());
			if(null != bidRegulationsDoc){
				Element rootkElement = bidRegulationsDoc.getRootElement();
				List<Element> itemList = rootkElement.elements("item");
			
				if(null != itemList && itemList.size() > 0){
					int index = 1;
					for(Element element : itemList){
						if(null != element.elementText("content")){
							BidRegulations bidRegulations = new BidRegulations();
							bidRegulations.setTenderId(null != tenderProject.getParentId() ? tenderProject.getParentId() : tenderProject.getId());
							bidRegulations.setContent(element.elementText("content"));
							bidRegulations.setShortName(element.elementText("name"));
							bidRegulations.setSortNo(index++);
							bidRegulationsList.add(bidRegulations);
						}
					}
				}
				
			}
		}
		return bidRegulationsList;
	}
	/**
	 * @Description: 选择下拉选事件
	 * @author guoyr 
	 */
	private void selectComboAction(){
		String shortName = bidRegulationsCombo.getText();
		if(blankString.equals(shortName)){
			refresBidRegulationsTree(null);
		}else{
			refresBidRegulationsTree(shortName);
		}
	}
	/**
	 * @Description: 设置下拉选的数据
	 * @author guoyr 
	 */
	private void setComboData(){
		if(null != bidRegulationsList){
			bidRegulationsCombo.add(blankString);
			for(BidRegulations bidRegulations : bidRegulationsList){
				if(shortNameSet.add(bidRegulations.getShortName().trim())){
					bidRegulationsCombo.add(bidRegulations.getShortName().trim());
				}
			}
			bidRegulationsCombo.select(0);
		}
	}
				
	/**
	 * @Description: 刷新树信息
	 * @author guoyr
	 * @param factorId 
	 */
	public void refresBidRegulationsTree(String regulationsShortName){
		bidRegulations_grid.removeAll();
		int index = 1;
		if(null != bidRegulationsList && bidRegulationsList.size() > 0){
			for(BidRegulations bidRegulations : bidRegulationsList){
				if(null != regulationsShortName && !regulationsShortName.equals(bidRegulations.getShortName().trim())){
					continue;
				}
				GridItem gridItem = new GridItem(bidRegulations_grid, SWT.WRAP);
				gridItem.setText(1, index ++ + "");
				
				gridItem.setText(2, (ObjectUtils.getObjString(bidRegulations.getShortName())));
				gridItem.setText(3, (ObjectUtils.getObjString(bidRegulations.getContent())));
				gridItem.setData("bidRegulations", bidRegulations);
			}
		}
		if(bidRegulations_grid.getSelectionCount() == 0){
			if(bidRegulations_grid.getItemCount() > 0){
				bidRegulations_grid.select(0);
			}
		}
		Rectangle rectangle = bidRegulations_grid.getBounds();
		bidRegulations_grid.scroll(0, 0, 0, 0, rectangle.width, rectangle.height, true);
		bidRegulations_grid.getVerticalBar().setSelection(bidRegulations_grid.getSelectionIndex()- 5);
		bidRegulations_grid.getVerticalBar().notifyListeners(SWT.Selection, null);
	}
	
	
	/**
	 * @Description: 保存事件
	 * @author guoyr 
	 */
	private void saveAction(){
		GridItem[] items = bidRegulations_grid.getItems();
		StringBuffer bidRegulationsContents = new StringBuffer();
		int index = 1;
		boolean hasSelection = false;
		for(GridItem item : items){
			if(item.getChecked()){
				BidRegulations bidRegulations = (BidRegulations)item.getData("bidRegulations");
				bidRegulationsContents.append(( "[" + bidRegulations.getShortName()+ "] " + bidRegulations.getContent()));
				hasSelection = true;
				if(index < items.length){
					bidRegulationsContents.append("\r\n");
					bidRegulationsContents.append("\r\n");
				}
				
			}
			index ++;
		}
		if(hasSelection){
			listener.selectBidRegulations(bidRegulationsContents.toString());
			getShell().close();
		}
	}
	@Override
	protected Point getInitialSize() {
		Point point = new Point(800, 600);
		return point;
	}
}
