package com.gx.obe.management.shell;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.web.entity.price.BidPrice;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 选择分项报价
 * @author guoyr 
 */
public class BidPriceSelectShell extends Shell {

	private Composite composite;
	
	private BidPriceSelection bidPriceSelection;
	private List<BidPrice> bidPriceList;

	private ScrolledComposite scrolledComposite;

	private CLabel title;
	{
		LogUtils.logClass(BidPriceSelectShell.class);
	}
	
	/**
	 * Create the shell.
	 * @param display
	 */
	public BidPriceSelectShell(Shell shell, List<BidPrice> bidPriceList) {
		super(shell, SWT.NO_TRIM |SWT.TOP);
		this.bidPriceList = bidPriceList;
		setBackground(ColorConstants.SKIN_BG);
		setBackgroundMode(SWT.INHERIT_FORCE);
		FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
		fillLayout.marginHeight = 1;
		fillLayout.marginWidth = 1;
		setLayout(fillLayout);
		this.setAlpha(230);
//		SWTUtil.setCircleRegion(this, 2);
		
		scrolledComposite = new ScrolledComposite(this, SWT.V_SCROLL);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		composite = new Composite(scrolledComposite, SWT.NONE);
		composite.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		composite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		composite.setLayout(gl_composite);
		
		title = new CLabel(composite, SWT.NONE);
		title.setLeftMargin(5);
		title.setRightMargin(5);
		title.setTopMargin(5);
		title.setBottomMargin(5);
		title.setBackground(ColorConstants.SKIN_BG);
		title.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		title.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		title.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		title.setText(Messages.BidPrice);
		scrolledComposite.setContent(composite);
		scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		init();
		createContents();
	}
	
	public void addBidPriceSelectionListener(BidPriceSelection bidPriceSelection){
		this.bidPriceSelection = bidPriceSelection;
	}
	/**
	 * 界面数据初始化
	 */
	private void init(){
		SwtUtils.setMouseDragListener(title);
		if(null != bidPriceList){
			for(BidPrice bidPrice : bidPriceList){
				createBidPrice(bidPrice);
			}
			composite.layout();
			scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		}
		addListener(SWT.Deactivate, new Listener(){
			public void handleEvent(Event arg0) {
				close();
			}
		});
	}

	/**
	 * 创建按钮入口
	 * @return
	 */
	private CLabel createItem(){
		ESepator label_1 = new ESepator(composite, SWT.NONE);
		label_1.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		CLabel itemLabel = new CLabel(composite, SWT.SELECTED);
		itemLabel.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		itemLabel.setLeftMargin(5);
		itemLabel.setRightMargin(5);
		itemLabel.setTopMargin(5);
		itemLabel.setBottomMargin(5);
		itemLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		itemLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		itemLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point.png"));
		itemLabel.addMouseTrackListener(new ActionMouseTrackListener(itemLabel));
		return itemLabel;
	}

	class ActionMouseTrackListener extends MouseTrackAdapter{
		private CLabel actionLabel;
		public ActionMouseTrackListener(CLabel actionLabel){
			this.actionLabel = actionLabel;
		}
		public void mouseEnter(MouseEvent e) {
			actionLabel.setBackground(ColorConstants.SKIN_BG);
//			actionLabel.setBackground(SWTResourceManager.getColor(255, 255, 255));
			actionLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point_green.png"));
			actionLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		}
		public void mouseExit(MouseEvent e) {
			actionLabel.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
			actionLabel.setForeground(null);
			actionLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point.png"));
		}
	}

	private void createBidPrice(final BidPrice bidPrice){
		CLabel itemLabel = createItem();
		itemLabel.setText(bidPrice.getBidPriceName());
		itemLabel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				getShell().close();
				if(null != bidPriceSelection){
					bidPriceSelection.select(bidPrice.getBidPriceCode(), bidPrice.getBidPriceName());
				}
			}
		});
	}
	
	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText(Messages.BidPrice);
		setSize(197, 128);
		this.setMinimumSize(100, 20);
		this.pack();
		if(this.getBounds().height > 250) {
			this.setSize(this.getBounds().width, 250);
			composite.layout();
			scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		}

	}

	public interface BidPriceSelection{
		public void select(String bidPriceCode, String bidPriceName);
	}
	public void close() {
		super.close();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
