package com.gx.obe.management.composite;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
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.Event;
import org.eclipse.swt.widgets.Listener;

import com.gx.obe.action.listener.ActionListener;
import com.gx.obe.common.widget.composite.PageNumComposite;
import com.gx.obe.common.widget.dialog.InputDialog;
import com.gx.obe.common.widget.listener.PageListener;
import com.gx.obe.common.widget.listener.SelectionTenderPackListener;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.listener.EvaluationRoomListener;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;

/**
 * @Description: 子包件项目列表界面
 * @author guoyr
 */
public class MyTenderPackageListComposite extends Composite implements EvaluationRoomListener{

	private TenderProjectService tenderProjectService;
	private ScrolledComposite scrolledComposite;
	private Composite contentComposite;
	private PageNumComposite pageFoodComposite;
	private TenderProjectEntity parentProject;
	private ActionListener topActionListener;
	private SelectionTenderPackListener selectionTenderPackListener;
	public static int PAGE_SIZE = 10;
	public static int CUR_PAGE_NO = 1;
	private String packageName;
	private GridLayout gl_contentComposite;
	private int itemHeight = 45;

	/**
	 * @param parent
	 * @param style
	 */
	public MyTenderPackageListComposite(Composite parent,TenderProjectEntity parentProject, SelectionTenderPackListener selectionTenderPackListener, int style) {
		super(parent, style);
		
		this.parentProject = parentProject;
		this.selectionTenderPackListener = selectionTenderPackListener;
//		setBackground(SWTResourceManager.getColor(240, 240, 240));
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		scrolledComposite = new ScrolledComposite(this, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		contentComposite = new Composite(scrolledComposite, SWT.NONE);
		gl_contentComposite = new GridLayout(1, false);
//		gl_contentComposite.marginWidth = 10;
//		gl_contentComposite.marginHeight = 10;
//		gl_contentComposite.verticalSpacing = 15;
		contentComposite.setLayout(gl_contentComposite);
		scrolledComposite.setContent(contentComposite);
		scrolledComposite.setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		pageFoodComposite = new PageNumComposite(this, SWT.NONE);
//		pageFoodComposite.setBackgroundMode(SWT.INHERIT_FORCE);
//		contentComposite.setBackground(SWTResourceManager.getColor(255, 255, 255));
		pageFoodComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		init();
	}
	
	/**
	 * @Description: 初始化界面数据
	 * @author guoyr
	 */
	private void init(){
		tenderProjectService = new TenderProjectService();
		packageName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUBPACKAGE, parentProject.getTenderType());
		
//		pageFoodComposite.addPageListener(new PageListener(){
//			public void changed(int pageNo, int pageSize) {
//				queryProjectPage(pageNo);
//			}
//		});
		pageFoodComposite.addPageListener(new PageListener(){
			public void changed(int pageNo, int pageSize) {
				queryProjectPage(pageNo);
			}
		});
		PAGE_SIZE = Integer.parseInt(PropertiesUtils.getProperty(ConfigConstants.PROJECT_PAGE_SIZE, "5"));
		
		queryProjectPage(CUR_PAGE_NO);
		scrolledComposite.getVerticalBar().setIncrement(10);
		scrolledComposite.forceFocus();
		scrolledComposite.addListener(SWT.Activate, new Listener() {
	        public void handleEvent(Event e) {
	            scrolledComposite.setFocus();
	        }
	    });
	}
	
	/**
	 * @Description: 页面设置
	 * @author guoyr 
	 */
	private void configPage(){
		InputDialog dialog = new InputDialog(getShell(), "请输入每页显示"+packageName+"数", PAGE_SIZE+"");
		dialog.open();
		String pageSizeStr = dialog.getInputValue();
		try {
			int pageSize = Integer.parseInt(pageSizeStr);
			if(pageSize != PAGE_SIZE && pageSize > 0){
				PAGE_SIZE = pageSize;
				queryProjectPage(1);
				PropertiesUtils.saveProperty(ConfigConstants.PROJECT_PAGE_SIZE, PAGE_SIZE+"");
			}
		} catch (NumberFormatException e) {
		}
	}
	
	
	/**
	 * @Description: 刷新投标项目列表
	 * @author guoyr
	 * @param pageNo
	 */
	private void queryProjectPage(int pageNo) {
		CUR_PAGE_NO = pageNo;
		new Thread(new Runnable(){
			public void run(){
				
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(!contentComposite.isDisposed()){
							final int existItemSize = contentComposite.getChildren().length;
							for(Control control : contentComposite.getChildren()){
								control.dispose();
							}
							itemHeight = scrolledComposite.getBounds().height - gl_contentComposite.marginHeight * 2 - gl_contentComposite.marginTop - gl_contentComposite.marginBottom;
							PAGE_SIZE = itemHeight / (45 + gl_contentComposite.verticalSpacing);
							if(existItemSize == 0 || (pageFoodComposite.getVisible())){
								itemHeight -= Math.max(pageFoodComposite.getBounds().height, 31);
								PAGE_SIZE = itemHeight / (45 + gl_contentComposite.verticalSpacing);
							} 
						}
					}
				});
				
				final com.gx.obe.web.entity.Page<TenderProjectEntity> projectPage = tenderProjectService.getSubPackagePageList(parentProject.getId(), CUR_PAGE_NO, PAGE_SIZE);
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(!contentComposite.isDisposed()){
							if(null != projectPage && null != projectPage.getRecords()){
								int index = 1;
								for(TenderProjectEntity tenderProject : projectPage.getRecords()){
									MyTenderPackageItemComposite trComposite = new MyTenderPackageItemComposite(contentComposite, SWT.NONE);
									trComposite.addSelectionTenderPackListener(selectionTenderPackListener);
									trComposite.refresh(index ++, parentProject, tenderProject);
									GridData gd_trComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
									trComposite.setLayoutData(gd_trComposite);
								}
								pageFoodComposite.refreshPages(projectPage);
							}
							boolean oldVisible = pageFoodComposite.getVisible();
							if(projectPage.getPages() > 1){
								if(!oldVisible){
									((GridData)pageFoodComposite.getLayoutData()).exclude = false;
									pageFoodComposite.setVisible(true);
								}
							}else {
								if(oldVisible){
									((GridData)pageFoodComposite.getLayoutData()).exclude = true;
									pageFoodComposite.setVisible(false);
								}
							}
							layout();
							contentComposite.layout();
							scrolledComposite.setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
							scrolledComposite.forceFocus();
						}
					}
				});
			}
		}).start();
		
	}
	
	public interface PackageOpListener{
		public void enterPackage(TenderProjectEntity tenderProjectEntity);
		public void configPageSize();
	}
	public void addTopActionListener(ActionListener actionListener){
		this.topActionListener = actionListener;
	}
	@Override
	protected void checkSubclass() {
	}
	@Override
	public void refresh(TenderProjectEntity tenderProjectEntity) {
		queryProjectPage(1);
	}
}
