package com.gx.obe.management.dialog;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
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.business.entity.ExpertLibrary;
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.service.expert.ExpertLibraryService;
import com.swtdesigner.ResourceManager;

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

	private ExpertLibraryService expertLibraryService;
	private String expertLibId;
	private ExpertLibrary expertLibrary;
	private Combo expertLibCombo;
	private EButton save_btn;
	private List<ExpertLibrary> expertLibraryList;
	
	{
		LogUtils.logClass(SelectExpertLibraryDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SelectExpertLibraryDialog(Shell parentShell, String expertLibId) {
		super(parentShell);
		setShellStyle(SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL);
		this.expertLibId = expertLibId;
	}

	/**
	 * 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));
		
		Label label_1 = new Label(composite_1, SWT.WRAP);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		label_1.setText("请先选择专家库！");
		
		Label sep = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep.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("专家库：");
		
		expertLibCombo = new Combo(composite, SWT.READ_ONLY);
		expertLibCombo.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));
		
		save_btn = new EButton(operateComposite, SWT.NONE);
		save_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		save_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				completeAction();
			}
		});
		GridData gd_completeBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		save_btn.setLayoutData(gd_completeBtn);
		save_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(){
		expertLibraryService = new ExpertLibraryService();
		expertLibraryList = expertLibraryService.getExpertLibraryByOrderByorUseStatus(CommonEnum.REAL_STATUS);
		if(null != expertLibraryList && expertLibraryList.size() > 0){
			int index = 0;
			for(ExpertLibrary expertLib : expertLibraryList){
				expertLibCombo.add(ObjectUtils.getObjString(expertLib.getExpertLibName()), index);
				if(null != expertLibId && expertLibId.equals(expertLib.getId())){
					expertLibCombo.select(index);
				}
				index ++;
			}
		}else {
			save_btn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 确定
	 * @author guoyr
	 */
	private void completeAction(){
		if(!save_btn.getEnabled()) {
			return ;
		}
		if(expertLibCombo.getSelectionIndex() != -1){
			expertLibrary = expertLibraryList.get(expertLibCombo.getSelectionIndex());
		}else {
			MessageDialog.openWarning(getShell(), "请先选择专家库！");
			return;
		}
		getShell().close();
	}
	
	/**
	 * @Description: 关闭
	 * @author guoyr
	 */
	private void cancelAction(){
		getShell().close();
		expertLibrary = null;
	}
	
	public ExpertLibrary getExpertLibrary() {
		return expertLibrary;
	}

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