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

import java.io.File;
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.GridSelectionListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
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 org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.file.entity.BidFile;
import com.gx.obe.common.file.entity.EvaluationPoint;
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.message.Messages;
import com.gx.obe.util.utils.StringUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 显示投标人评审点
 * @author guoyr
 */
public class ListEvaluationItemShell extends Shell {
	
	private EvaluationPointListener evaluationPointListener;
	private Grid evaluationPointGrid;
	private Composite composite;
	private ImageButton bidPointTitle;
	
	/**
	 * @param shell
	 * @param evalPointListener
	 */
	public ListEvaluationItemShell(Shell shell, EvaluationPointListener evalPointListener) {
		super(shell, SWT.NO_TRIM);
		setBackgroundMode(SWT.INHERIT_FORCE);
		setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		gridLayout.verticalSpacing = 0;
		setLayout(gridLayout);
		this.evaluationPointListener = evalPointListener;
		
		Composite topComposite = new Composite(this, SWT.NONE);
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		topComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_topComposite = new GridLayout(1, false);
		gl_topComposite.verticalSpacing = 0;
		gl_topComposite.horizontalSpacing = 0;
		gl_topComposite.marginWidth = 0;
		gl_topComposite.marginHeight = 0;
		topComposite.setLayout(gl_topComposite);
		
		composite = new Composite(topComposite, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.verticalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		bidPointTitle = new ImageButton(composite, SWT.NONE);
		bidPointTitle.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		bidPointTitle.setBackground(ColorConstants.SKIN_BG);
		bidPointTitle.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		bidPointTitle.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		bidPointTitle.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/location.png"));
		bidPointTitle.setText(Messages.Button_allLocationPoint);
		
		ELink cancelLink = new ELink(composite, SWT.NONE);
		cancelLink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		cancelLink.setText(Messages.Button_close);
		ESepator pointSep = new ESepator(composite, SWT.NONE);
		pointSep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		pointSep.setBackground(ColorConstants.SKIN_BG);
		pointSep.setLineWidth(2);
		
		evaluationPointGrid = new Grid(topComposite, SWT.V_SCROLL);
		evaluationPointGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridColumn gridColumn = new GridColumn(evaluationPointGrid, SWT.NONE);
		gridColumn.setWordWrap(true);
		gridColumn.setWidth(150);
		gridColumn.setText(Messages.Name);
		
		GridItem gridItem = new GridItem(evaluationPointGrid, SWT.NONE);
		gridItem.setText("New Row");
		
		init();
		createContents();
	}

	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		// 设置拖拽
		SwtUtils.setMouseDragListener(composite);
		SwtUtils.setMouseDragListener(bidPointTitle);
		evaluationPointGrid.setItemHeight(Constants.ROW_HIGHT);
		evaluationPointGrid.setAutoWidth(true);
		evaluationPointGrid.setAutoHeight(true);
		// 添加Grid选中事件
		evaluationPointGrid.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void leftMouseButtonDown(GridItem item) {
				selectEvalPoint();
			}
		});
	}
	
	/**
	 * @Description: 刷新评审点Grid
	 * @author guoyr
	 * @param tenderId
	 * @param supplierId
	 */
	public void refreshEvalPointGrid(String tenderId, String supplierId){
		evaluationPointGrid.removeAll();
		List<EvaluationPoint> evaluationPointList = BidFile.getBidFileEvaluationPointList(FileConstants.getBidFilePath(tenderId, supplierId).concat(FileConstants.PSD_XML), null);
		int evalPointCount = 1;
		for(EvaluationPoint evaluationPoint : evaluationPointList){
			GridItem gridItem = new GridItem(evaluationPointGrid, SWT.NONE);
			gridItem.setText((evalPointCount++) +"、" +evaluationPoint.getName());
			gridItem.setData("supplierId", supplierId);
			gridItem.setData("evaluationPoint", evaluationPoint);
		}
	}

	/**
	 * @Description: 选中评审点
	 * @author guoyr
	 */
	private void selectEvalPoint(){
		if(evaluationPointGrid.getSelectionCount() > 0){
			GridItem selectGridItem = evaluationPointGrid.getSelection()[0];
			String message = (String) selectGridItem.getData("message");
			if(StringUtils.isNotEmpty(message)){
				MessageDialog.openShortMessage(message);
				return ;
			}
			String supplierId = (String) selectGridItem.getData("supplierId");
			EvaluationPoint evaluationPoint = (EvaluationPoint) selectGridItem.getData("evaluationPoint");
			if(null != evaluationPointListener && null != evaluationPoint){
				evaluationPointListener.selectEvalPoint(supplierId, evaluationPoint.getBidFile(), evaluationPoint.getBookmark(), evaluationPoint.getPageNo());
			}
		}
	}

	/**
	 * @Description: 关闭
	 * @author guoyr
	 */
	public void cancelAction() {
		getShell().close();
	}
	
	protected void createContents() {
		setText("");
		setSize(168, 300);
	}

	public interface EvaluationPointListener {
		public void selectEvalPoint(String supplierId, File bidFile, String bookMark, int pageNumber);
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
