package com.gx.obe.management.dialog;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.custom.CLabel;
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.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.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.TransactionServer;
import com.gx.obe.web.service.TransactionServerService;
import com.swtdesigner.ResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class SelectTranscationServerDialog extends EDialog {

	private TransactionServerService transactionServerService ;
	private String transServerId;
	private Combo serverCombo;
	private EButton complate_btn;

	{
		LogUtils.logClass(SelectTranscationServerDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SelectTranscationServerDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * 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_1 = new Composite(container, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_yellow_bg.png"));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
		composite_1.setLayout(new GridLayout(2, false));
		
		CLabel label_1 = new CLabel(composite_1, SWT.WRAP);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		label_1.setText("请先选择执行交易系统！");
		
		Label label_3 = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		
		CLabel label = new CLabel(composite, SWT.NONE);
		label.setText("执行交易系统：");
		
		serverCombo = new Combo(composite, SWT.READ_ONLY);
		serverCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_2 = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Composite operateComposite = new Composite(container, SWT.NONE);
		operateComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		operateComposite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		GridLayout gl_operateComposite = new GridLayout(2, false);
		operateComposite.setLayout(gl_operateComposite);
		operateComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		complate_btn = new EButton(operateComposite, SWT.NONE);
		complate_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		complate_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				completeAction();
			}
		});
		GridData gd_completeBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		complate_btn.setLayoutData(gd_completeBtn);
		complate_btn.setText(Messages.Button_ok);
		
		EButton cancel_btn = new EButton(operateComposite, SWT.NONE);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		GridData gd_cancelBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		cancel_btn.setLayoutData(gd_cancelBtn);
		cancel_btn.setText(Messages.Button_close);

		init();
		return container;
	}

	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init(){
		transactionServerService = new TransactionServerService();
		List<TransactionServer> transactionServerList = transactionServerService.getStatusServerOrderBySortNo(CommonEnum.REAL_STATUS);
		if(null != transactionServerList && transactionServerList.size() > 0){
			int index = 0;
			for(TransactionServer server : transactionServerList){
				serverCombo.add(ObjectUtils.getObjString(server.getServerName()), index);
				serverCombo.setData(ObjectUtils.getObjString(index++), server.getId());
			}
			serverCombo.select(0);
		}else {
			complate_btn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 确定
	 * @author guoyr
	 */
	private void completeAction(){
		if(!complate_btn.getEnabled()) {
			return ;
		}
		if(null != WidgetUtils.getComboSelection(serverCombo)){
			transServerId = WidgetUtils.getComboSelection(serverCombo).toString();
		}
		getShell().close();
	}
	
	/**
	 * @Description: 关闭
	 * @author guoyr
	 */
	private void cancelAction(){
		transServerId = null;
		getShell().close();
	}
	
	public String getTransServerId() {
		return transServerId;
	}

	public void setTransServerId(String transServerId) {
		this.transServerId = transServerId;
	}

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